首页 > 解决方案 > 包 ecodist 中的功能 MRM 的尺寸不正确错误

问题描述

在 Ecodist 包中使用 MRM 功能时,出现以下错误:

Error in xj[i, , drop = FALSE] : incorrect number of dimensions

无论我做什么,我都会收到此错误,我什至可以通过文档中的示例代码得到它:

data(graze)

  # Abundance of this grass is related to forest cover but not location
  MRM(dist(LOAR10) ~ dist(sitelocation) + dist(forestpct), data=graze, nperm=10)

我不知道发生了什么事。我尝试过其他计算机并得到相同的错误,所以它甚至不限于我的机器(Windows 10,完全更新)。

最好的,

标签: rdimensions

解决方案


感谢 Torsten Biemann 向我指出这一点。我不定期检查 stackoverflow,但随时欢迎您通过 ecodist 维护者地址给我发送电子邮件或在https://github.com/phiala/ecodist打开问题

如上所述,该示例在干净的 R 会话中可以正常工作,但如果加载了 spdep,则会失败。我还没有弄清楚冲突,但问题在于使用公式的机制中距离对象到矢量的隐含强制。如果您明确地这样做,该命令将正常工作。我会做一个补丁,首先在上面的 github 上,并在测试后发送给 CRAN。

# R --vanilla --no-save

library(ecodist)
data(graze)

# Works
set.seed(1234)
MRM(dist(LOAR10) ~ dist(sitelocation) + dist(forestpct), data=graze, nperm=10)


$coef
                   dist(LOAR10) pval
Int                   6.9372046  1.0
dist(sitelocation)   -0.4840631  0.6
dist(forestpct)       0.1456083  0.1

$r.squared
        R2       pval
0.04927212 0.10000000

$F.test
       F   F.pval
31.66549  0.10000


library(spdep)

# Fails
MRM(dist(LOAR10) ~ dist(sitelocation) + dist(forestpct), data=graze, nperm=10)


Error in xj[i, , drop = FALSE] : incorrect number of dimensions


# Explicit conversion to vector

graze.d <- with(graze, data.frame(LOAR10 = as.vector(dist(LOAR10)), sitelocation = as.vector(dist(sitelocation)), forestpct = as.vector(dist(forestpct))))

# Works
set.seed(1234)
MRM(LOAR10 ~ sitelocation + forestpct, data=graze.d, nperm=10)


$coef
                 LOAR10 pval
Int           6.9372046  1.0
sitelocation -0.4840631  0.6
forestpct     0.1456083  0.1

$r.squared
        R2       pval
0.04927212 0.10000000

$F.test
       F   F.pval
31.66549  0.10000

推荐阅读