r - R中的第四角算法

标签 r algorithm function matrix package

这是一道关于R中fourthcorner算法的题,它的目的是衡量三个不同的表之间的关系:一个n x m表(表R)的m个环境变量(列)在n个站点(行),一个n x p表( n 个位点(行)的 p 个丰度(列)的表 L),以及 p 个物种(行)的 s 个性状(列)的 p x s 表(表 Q)。

fourthcorner 函数在包 ade4 中。

我的所有三个数据框都是二进制的(0 和 1 分别表示是否存在变量、某个地点的物种或特征)。我试过使用"is"和“否”而不是 0 和 1,但没有成功。

以下是我正在使用的格式的一些示例矩阵:

tabQ

Trait1  Trait2  Trait3  Trait4
Sp1 0   1   0   0
Sp2 0   1   0   0
Sp3 1   0   1   0
Sp4 1   0   1   0
Sp5 0   1   0   0
Sp6 0   1   0   0
Sp7 0   0   0   1
Sp8 0   0   0   1

tabR

EnV1    EnV2    EnV3    EnV4
Site1   1   1   1   1
Site2   1   1   0   1
Site3   0   1   0   1
Site4   1   1   1   1
Site5   1   1   0   1
Site6   0   1   0   0
Site7   0   1   0   1
Site8   0   1   0   1
Site9   1   1   1   1
Site10  1   1   0   1
Site11  1   1   1   1
Site12  0   1   0   0
Site13  1   1   0   1
Site14  1   1   0   1
Site15  0   1   0   1
Site16  1   1   0   1
Site17  0   1   0   1
Site18  1   1   1   1
Site19  1   1   0   1
Site20  1   1   0   1

标签

Sp1 Sp2 Sp3 Sp4 Sp5 Sp6 Sp7 Sp8
Site1   1   1   0   0   0   0   0   0
Site2   1   1   0   0   0   0   0   0
Site3   1   1   0   0   0   0   0   0
Site4   1   0   0   0   0   0   0   1
Site5   1   1   0   0   0   0   0   0
Site6   1   0   0   0   1   0   0   0
Site7   1   0   0   0   0   0   0   0
Site8   0   0   0   0   1   0   0   0
Site9   1   0   0   0   0   0   0   0
Site10  1   1   0   0   0   0   0   0
Site11  0   0   1   1   0   0   0   0
Site12  0   0   0   0   0   1   0   0
Site13  1   0   0   0   0   0   0   0
Site14  0   0   0   0   1   0   0   0
Site15  1   1   0   0   0   0   0   0
Site16  1   1   0   0   0   0   0   0
Site17  1   0   0   0   0   0   0   0
Site18  0   0   1   0   0   0   0   0
Site19  1   0   0   0   0   0   0   0
Site20  1   1   0   0   0   0   1   0

我将这些数据帧从文本文件读入 R,并指定第一列是行名。

这是我尝试在我的矩阵上使用 fourthcorner 函数时遇到的错误:

fourth1=fourthcorner(tabR,tabL,tabQ,nrepet=1)

Error in apply(sim, 2, function(x) length(na.omit(x))) : dim(X) must have a positive length

我不明白问题出在哪里,是格式问题吗?如果是这样,我应该重新格式化其中一个矩阵吗?是哪一个造成了麻烦?或者我不能为此功能使用二进制特征和环境变量吗?换句话说,我是不是可以通过改一段代码来解决这个问题,还是说这道题不能用这个函数?

作为额外的信息,我确实给函数的作者发了邮件,但不幸的是我没有完全理解他的回复,可能是因为我的 R 技能还有很多不足之处。如果有帮助,这是他的回复:

Q could contain quantitative or qualitative traits. In R, qualitative traits should be coded as factors to obtain adapted statistics (i.e. chi2 or eta2). If you code qualitative variables as dummy variables, they would be considered as quantitative.

非常感谢您的所有见解。

最佳答案

我注意到您的示例仅失败 nrepet等于一,所以如果你可以使用任何其他正数,你应该没问题。

但是,如果您确实需要 nrepet=1 , 你应该联系 ade4 的作者并要求他/她修复 fourthcorner功能代码。我追查错误发现fourthcorner电话 as.krandtestsim = res$tabD[-1,]其中 res$tabD是一个矩阵 nrepet+1行。当nrepet =1 并从两行矩阵中删除一行,R自动将生成的单行矩阵转换为向量,但是 as.krandtest函数期望 sim成为一个矩阵,从而引发错误。

这是您的输入数据,以防其他人想回答您的问题:

tabR

structure(list(EnV1 = c(1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 
1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L), EnV2 = c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), EnV3 = c(1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L), EnV4 = c(1L, 1L, 1L, 1L, 1L, 
0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("EnV1", 
"EnV2", "EnV3", "EnV4"), row.names = c("Site1", "Site2", "Site3", 
"Site4", "Site5", "Site6", "Site7", "Site8", "Site9", "Site10", 
"Site11", "Site12", "Site13", "Site14", "Site15", "Site16", "Site17", 
"Site18", "Site19", "Site20"), class = "data.frame")

标签

structure(list(Sp1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 
0L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L), Sp2 = c(1L, 1L, 1L, 
0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 
1L), Sp3 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L), Sp4 = c(0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    Sp5 = c(0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
    1L, 0L, 0L, 0L, 0L, 0L, 0L), Sp6 = c(0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    ), Sp7 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L), Sp8 = c(0L, 0L, 0L, 1L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L)), .Names = c("Sp1", "Sp2", "Sp3", "Sp4", "Sp5", "Sp6", 
"Sp7", "Sp8"), row.names = c("Site1", "Site2", "Site3", "Site4", 
"Site5", "Site6", "Site7", "Site8", "Site9", "Site10", "Site11", 
"Site12", "Site13", "Site14", "Site15", "Site16", "Site17", "Site18", 
"Site19", "Site20"), class = "data.frame")

tabQ

structure(list(Trait1 = c(0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L), Trait2 = c(1L, 
1L, 0L, 0L, 1L, 1L, 0L, 0L), Trait3 = c(0L, 0L, 1L, 1L, 0L, 0L, 
0L, 0L), Trait4 = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L)), .Names = c("Trait1", 
"Trait2", "Trait3", "Trait4"), row.names = c("Sp1", "Sp2", "Sp3", 
"Sp4", "Sp5", "Sp6", "Sp7", "Sp8"), class = "data.frame")

关于r - R中的第四角算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27905963/

相关文章:

python - 二叉树中的最大和路径

python - 打印字符串并在类中执行函数 [Python]

r - 扩展为1分钟,计算平均值

RMarkdown - 使用 DT 创建列标题

c# - 创建 ATM 算法

java - 给定一个数 N,找到最小的偶数 E 使得 E > N

python - 我收到消息 : "Bad import syntax:" when I try to import a python function inside my gnuradio flowgraph

c# - 获取对象调用层次结构

r - 在R中绘制距离和方位

r - 如何在 ggplot2 中使用用户定义颜色的六边形分箱来绘制点