笔记内容:
- R vegan包envfit的output及其计算
- python实现:见github
- 注意
R vegan包envfit的output及其计算
vegan::envfit
用法见R使用笔记: scatterplot with confidence ellipses;envfit的实现及释意,在R中其实只用envfit(mds_point, env, permutation=999)
就可以得到envfit的结果。
library(vegan)
data(iris)
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
iris_data = iris[,1:4]
iris_env = iris[,1:5] # 包括一个factor,4个vector
ord = data.frame(cmdscale(dist(iris_data),k=2))
colnames(ord) = c('X1','X2') # ord为pcoa的坐标
envfit_result = envfit(ord,iris_env,permutations = 100)
envfit_result # envfit的结果:
***VECTORS
X1 X2 r2 Pr(>r)
Sepal.Length 0.48219 0.87607 0.9579 0.009901 **
Sepal.Width -0.11499 0.99337 0.8400 0.009901 **
Petal.Length 0.98013 -0.19836 0.9981 0.009901 **
Petal.Width 0.97852 -0.20615 0.9366 0.009901 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Permutation: free
Number of permutations: 100
***FACTORS:
Centroids:
X1 X2
Speciessetosa -2.6424 0.1909
Speciesversicolor 0.5332 -0.2455
Speciesvirginica 2.1092 0.0547
Goodness of fit:
r2 Pr(>r)
Species 0.8868 0.009901 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Permutation: free
Number of permutations: 100
input包括每个样本的降维坐标(或者你把整个降维结果cmdscale()
传给envfit也可以),环境变量表以及permutation的次数。permutation参数用于P值的计算。
结果分为vector和factor两个部分,其中vector结果是对数值型的环境变量分析。在envfit_result中结构如下所示。
$arrows
是各vector的坐标;
$r
为goodness of fit, 即该环境变量的样本点在linear model上拟合的程度。
$pvals
为经过n次permutation计算,得到n个r_shuffle值,大于等于真实r值的个数/n.
arrows的计算:
r2(goodness of fit / coefficient of determination)的计算:
对factor而言:
$centroids
为各factor中各level的坐标
$r
为goodness of fit, 各level只有一个r值
$pvals
permutation得到的P值
计算:
SSW和SSB的计算及ANOVA相关见这个连接
python实现
python代码见github https://github.com/CS0000/envfit_py
要用到这些模块:
import pandas as pd
from sklearn.datasets import load_iris
from skbio.stats.ordination import pcoa
from math import sqrt
from scipy.spatial.distance import squareform, pdist
from sklearn.preprocessing import scale
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
import numpy as np
见github中的envfit2py.py
脚本,在if __name__ == '__main__':
里面load进数据,调用函数就可以了。
注意
- anvova table的计算
- R envfit的源代码索引:
envfit.default.R:envfit函数code,调用了vectorfit和factorfit。
vectorfit.R
factorfit.R
centroids.cca.R : 在factorfit.R中计算centroids
goffactor.c:用到的C代码:可以通过.Call
和vegan:::
来调用 - ...用python写一遍envfit的本意是希望节省时间,减少内存的占用。但是这个脚本就节省时间而言一点也不乐观,甚至比R还慢...=_=。后面考虑用多线程,但如果多线程也只能勉强达到R的用的时间,那还有什么意义...? 优化之路道阻且长。
- Python和R的output还是存在一些差别,并不能保证完全一毛一样。似乎因为R对linear model的底层算法和python不太一样,所以会有差异。介于linear model在R扒到底都是C写的了,我完全看不懂,所以...=_=后面如果要用到了,还会再优化的。
- 这里只实现了envfit最最最基本的功能,envfit似乎还可以给croodination设置weight,我在这里没有设置。
-
vegan包文档中对envfit的描述:
Function envfit finds vectors or factor averages of environmental variables... If X is a data.frame, envfit uses factorfit for factor variables and vectorfit for other variables. If X is a matrix or a vector, envfit uses only vectorfit. Alternatively, the model can be defined a simplified model formula, where the left hand side must be an ordination result object or a matrix of ordination scores, and right hand side lists the environmental variables.
Functions vectorfit and factorfit can be called directly. Function vectorfit finds directions in the ordination space towards which the environmental vectors change most rapidly and
to which they have maximal correlations with the ordination configuration. Function factorfit finds averages of ordination scores for factor levels. Function factorfit treats ordered and unordered factors similarly.
If permutations > 0, the ‘significance’ of fitted vectors or factors is assessed using permutation of environmental variables. The goodness of fit statistic is squared correlation coefficient (r2). For factors this is defined as r2 = 1 − ssw/sst, where ssw and sst are within-group and total sums of squares.