在生态统计分析中,很多老师和同学已经习惯在 R 中使用 rdacca.hp 。但当变量变多、置换次数增加时,运行时间往往会迅速拉长。
以 doubs 案例为例,在同类 RDA 分析流程下,我们的测试结果是:
这意味着: 同样一套分析思路,换到 Python 后,运行时间明显缩短。
下面就以 doubs 数据为例,具体看看这套流程如何从 R 迁移到 Python。
首先要回答的,并不是“Python 能不能做”,而是:为什么还要做 Python 版?
原因并不是“R 不好”,而主要有以下几点。
对于 permu.hp() 这类涉及大量置换的分析,运行时间往往会随着变量数和置换次数增加而显著变长。当研究中需要多次重复分析、反复调整变量组合,或者进行批量计算时,运行效率就会成为一个非常实际的问题。
而在当前 doubs 案例的测试中,同样设置下,Python 版本运行时间约为 5 分钟,而 R 版本约为 44 分钟。这说明:对于这类计算量较大的置换分析任务,Python 版本不仅可以完成同样的流程,而且在当前案例中也表现出了更高的效率。
现在不少数据前处理工作已经放在 Python 里完成,例如:
如果前面的工作已经在 Python 中完成,那么继续在 Python 中做层次分解与置换检验,会比较自然。
对生态学用户来说,最现实的情况并不是“以后只用 Python”,而是:
换句话说,Python 版本的意义,不是为了替代 R,而是为了在需要时提供一种可切换、可补充、可扩展的工作流。
以 doubs 数据为例,R 中常见代码如下:
#install rdacca.hp from CRAN or github and load it #install.packages('rdacca.hp')library(rdacca.hp)#sample data in ade4 packagelibrary(ade4)data(doubs) # Fish species as response variablesspe <- doubs$fish# Environmental factors as explanatory variablesenv <- doubs$env#Remove empty site 8 without speciesspe <- spe[-8,]env <- env[-8,]# Apart from being a spatial position variable rather than # environmental variables, remove the 'dfs' variable (the distance# from the source) from the 'env' data frameenv <- env[, -1]# Hellinger-transformation of the species dataset for RDA to deal with the 'double zero' problemspe.hel <- decostand(spe, "hellinger")(spe.hp <- rdacca.hp(spe.hel,env, method="RDA", type = "adjR2"))permu.hp(spe.hel,env)system.time(permu.hp(spe.hel,env))
这一流程非常典型:
doubs 数据中的鱼类矩阵和环境矩阵env 中的 dfs 变量RDA + adjR2 框架下进行层次分解与置换检验如果已经熟悉这套写法,那么迁移到 Python 时,整体思路其实并没有改变。
对于平时几乎没怎么接触过 Python 的生态学读者来说,其实不需要一下子理解太多。 只要把下面几个步骤弄清楚,基本就可以完成一次对应分析:
dfs 变量rdacca_hp()permu_hp()下面就按这个顺序一步一步来看。
如果下载了源码,可在项目根目录执行:
pip install .如果直接从 PyPI 安装,可运行:
pip install rdacca_hp对于主要使用 R 的读者来说,可以把这一步简单理解为:先把 Python 版本的分析工具装好。
在 Python 中,先导入需要的库和函数:
import timeimport numpy as npimport pandas as pdfrom rdacca_hp import rdacca_hp, permu_hp
这里:
pandas 用于读入和处理表格数据numpy 用于数值运算rdacca_hp 是主分析函数permu_hp 对应 R 中的 permu.hp()如果之前没怎么接触过 Python,这一步可以简单理解为:先把后面要用到的工具加载进来。
由于 doubs 是 R 中 ade4 包里的示例数据,因此在 Python 中通常需要先把 R 数据导出成 CSV。 例如:
doubs_fish.csvdoubs_env.csv然后在 Python 中读入:
spe = pd.read_csv("doubs_fish.csv", index_col=0)env = pd.read_csv("doubs_env.csv", index_col=0)spe = spe.apply(pd.to_numeric, errors="raise")env = env.apply(pd.to_numeric, errors="raise")
这一点和 R 不同: 在 R 中可以直接 data(doubs),而在 Python 中通常是先把数据保存为文件,再读入为 DataFrame。
dfs 变量这一步与 R 中的处理是一一对应的。
在 R 中:
spe <- spe[-8,]env <- env[-8,]env <- env[, -1]在 Python 中可以写成:
spe = spe.drop(spe.index[7])env = env.drop(env.index[7])env = env.drop(columns=["dfs"], errors="raise")这里需要注意一点: R 是从 1 开始计数,而 Python 是从 0 开始计数,所以第 8 行在 Python 中对应的是位置 7。
在 R 中,这一步常写成:
decostand(spe, "hellinger")在 Python 中,可以显式写成:
defhellinger_transform(df: pd.DataFrame) -> pd.DataFrame: df = df.apply(pd.to_numeric, errors="raise") row_sums = df.sum(axis=1) rel = df.div(row_sums.replace(0, np.nan), axis=0).fillna(0.0)return np.sqrt(rel)spe_hel = hellinger_transform(spe)
也就是说,本质上仍然是同样的 Hellinger 转换,只是在 Python 中通常会把这一步单独写出来。
如果只从分析逻辑来看,其实没有变化:
decostand()在 R 中,这一步对应:
(spe.hp <- rdacca.hp(spe.hel,env, method="RDA", type = "adjR2"))在 Python 中,可以写成:
spe_hp = rdacca_hp( dv=spe_hel, iv=env, method="RDA", type="adjR2", scale=False, var_part=True,)
然后查看结果:
print(spe_hp.total_explained_variation)print(spe_hp.hier_part)print(spe_hp.var_part)这样就完成了与 R 中 rdacca.hp(...) 相对应的一次分析。
在 R 中,这一步对应:
permu.hp(spe.hel,env)在 Python 中可以写成:
perm_result = permu_hp( dv=spe_hel, iv=env, method="RDA", type="adjR2", permutations=1000, scale=False, verbose=False,)
然后查看结果:
print(perm_result)这样就完成了与 R 中 permu.hp() 相对应的一次分析。
如果希望像 R 里的 system.time(...) 一样记录运行时间,在 Python 中可以使用 time.perf_counter()。
例如:
t1 = time.perf_counter()spe_hel = hellinger_transform(spe)t2 = time.perf_counter()t3 = time.perf_counter()spe_hp = rdacca_hp( dv=spe_hel, iv=env, method="RDA", type="adjR2", scale=False, var_part=True,)t4 = time.perf_counter()t5 = time.perf_counter()perm_result = permu_hp( dv=spe_hel, iv=env, method="RDA", type="adjR2", permutations=1000, scale=False, verbose=False,)t6 = time.perf_counter()print(f"Hellinger transform time: {t2 - t1:.6f} seconds")print(f"rdacca_hp time: {t4 - t3:.6f} seconds")print(f"permu_hp time: {t6 - t5:.6f} seconds")
在当前测试中,doubs 案例的总体耗时表现为:
也就是说,在这个案例下,Python 版本不仅可以复现对应分析流程,而且在运行时间上也表现出了明显优势。
将上面的步骤合起来,可以写成如下完整示例:
import timeimport numpy as npimport pandas as pdfrom rdacca_hp import rdacca_hp, permu_hpdefhellinger_transform(df: pd.DataFrame) -> pd.DataFrame:""" Hellinger transformation: sqrt(x_ij / row_sum_i) """ df = df.apply(pd.to_numeric, errors="raise") row_sums = df.sum(axis=1) rel = df.div(row_sums.replace(0, np.nan), axis=0).fillna(0.0)return np.sqrt(rel)total_start = time.perf_counter()spe = pd.read_csv("doubs_fish.csv", index_col=0)env = pd.read_csv("doubs_env.csv", index_col=0)spe = spe.apply(pd.to_numeric, errors="raise")env = env.apply(pd.to_numeric, errors="raise")spe = spe.drop(spe.index[7])env = env.drop(env.index[7])env = env.drop(columns=["dfs"], errors="raise")ifnot spe.index.equals(env.index):raise ValueError("Row indices of spe and env do not match after preprocessing.")t1 = time.perf_counter()spe_hel = hellinger_transform(spe)t2 = time.perf_counter()t3 = time.perf_counter()spe_hp = rdacca_hp( dv=spe_hel, iv=env, method="RDA", type="adjR2", scale=False, var_part=True,)t4 = time.perf_counter()print("=== rdacca_hp result ===")print("Total explained variation:")print(spe_hp.total_explained_variation)print("\nHierarchical partitioning:")print(spe_hp.hier_part)print("\nVariation partitioning:")print(spe_hp.var_part)t5 = time.perf_counter()perm_result = permu_hp( dv=spe_hel, iv=env, method="RDA", type="adjR2", permutations=1000, scale=False, verbose=False,)t6 = time.perf_counter()print("\n=== permutation test result ===")print(perm_result)total_end = time.perf_counter()print("\n=== runtime ===")print(f"Hellinger transform time: {t2 - t1:.6f} seconds")print(f"rdacca_hp time: {t4 - t3:.6f} seconds")print(f"permu_hp time: {t6 - t5:.6f} seconds")print(f"Total runtime: {total_end - total_start:.6f} seconds")
R 用户通常习惯查看:
spe.hppermu.hp(...) 的输出结果在 Python 中,对应的是:
spe_hp.total_explained_variationspe_hp.hier_partspe_hp.var_partperm_result也就是说,Python 返回的是一个结果对象。 如果后续还要:
那么对象化结果会比较方便。
总体模式应当一致,但置换检验得到的 p 值不必逐位完全相同。
这是因为置换检验本质上属于 Monte Carlo 过程,不同平台下的随机序列和数值细节可能存在差异,因此出现轻微偏差是正常的。
实际使用中,更值得关注的通常是:
换句话说,更重要的是整体分析结论,而不是要求每一个小数位都完全一致。
如果已经熟悉 rdacca.hp 的 R 工作流,那么迁移到 Python 时,最关键的其实就是以下几步:
doubs 数据doubs_fish.csv 和 doubs_env.csvdfs 变量rdacca_hp() 完成层次分解与变异分解permu_hp() 完成置换检验因此,Python 版本提供的不只是另一种写法。对于已经在 Python 中做数据清洗、批量处理和自动化分析的用户来说,这会更加方便一些;而对主要使用 R 的读者来说,也可以把它视为一个在特定场景下可调用的补充方案。
rdacca_hp0.1.1pip install rdacca_hphttps://github.com/peony-peo/rdacca_hp