Merlin R 包 - str2lang(x) 中的错误::<text>1:30:意外符号



我在使用merlin包时遇到错误代码的问题,我无法找出问题是什么。我知道其他人已经得到了这个错误信息,但我仍然不知道它到底是什么意思。下面是一个示例数据框架,其结构与我的实际数据类似:

dat = as.data.frame(list(fish = as.factor(c(rep("a",6),rep("b",6),rep("c",6),rep("d",6))),
value = as.numeric(c(1,3,7,7,6,7,2,4,8,7,7,6,5,8,10,11,12,10,3,7,9,9,8,9)),
time = as.numeric(rep(1:6,4)),
location = as.factor(c(rep("0",6),rep("1",6)))))

dat
str(dat)

library(ggplot2)
ggplot(dat, aes(x=time, y=value, group=fish, col=fish)) +
geom_line()

在这里,实验中有4条鱼(a-d),在每个时间点(6个时间点),对每条鱼测量一个值。位置是一个虚拟变量,它告诉我们鱼来自哪里。在这里,鱼"one_answers";b"来自地点"0"当鱼&;c&;和";d"来自位置"1"

我的目标是检查价值如何随时间变化,以及位置是否是一个重要因素(如果有重要的时间x位置交互作用)。我最初像这样拟合数据的受限三次样条模型,效果很好:

mod1 <- ols(value ~ rcs(time, 3) * location, data = dat, x = TRUE, y = TRUE)

然而,由于在一段时间内对每条鱼进行了重复测量,因此该模型是自相关的。为了解决这个问题,我试图将鱼作为随机变量添加到模型中(如果这不是处理自相关的最佳方法,请纠正我)。我发现我可以将随机变量与merlin包合并到三次样条模型中,如下所示:

library(merlin)
mod2 <- mlrcs(formula = value ~ rcs(time, 3) + location, random  = ~ 1 | fish, data = dat)
然而,这给了我以下错误:
Error in str2lang(x) : <text>:1:22: unexpected symbol
1: value ~rcs(time, 3)  location
^

当我删除"+ location"像这样:

mod3 <- mlrcs(formula = value ~ rcs(time, 3), random  = ~ 1 | fish, data = dat)

我似乎也不能包括交互术语:

mod4 <- mlrcs(formula = value ~ rcs(time, 3) * location, random  = ~ 1 | fish, data = dat)

当我得到以下错误信息时:

Error in rcs(gml, time, 3) * location : 
non-numeric argument to binary operator

关于这些错误信息意味着什么,我做错了什么想法?

查看其源代码,mlrcs的主要工作是解释您提供的公式并将其传递给merlin::merlin函数。但为了正确解析,mlrcs的公式要求指定截距。您的公式不包括截距,因此您会看到错误消息。这个行为应该添加到'?mlrc的帮助文件。

另外,'location'变量应该是带有0和1的数字。通过这些调整,您的代码符合模型。

dat = as.data.frame(list(fish = as.factor(c(rep("a",6),rep("b",6),rep("c",6),rep("d",6))),
value = as.numeric(c(1,3,7,7,6,7,2,4,8,7,7,6,5,8,10,11,12,10,3,7,9,9,8,9)),
time = as.numeric(rep(1:6,4)),
location = c(rep(0,6),rep(1,6)))) # adjust
library(merlin)
# formula with intercept
mod2 <- mlrcs(formula = value ~  1 + rcs(time, 3) + location, random  = ~ 1 |fish, data = dat)
> summary(mod2)
Restricted cubic splines model
Log likelihood = -30.32352
Estimate Std. Error       z Pr(>|z|) [95% Conf.  Interval]
rcs():1         1.6839262  0.1331443  12.647   0.0000  1.4229681  1.9448843
rcs():2         1.2721426  0.1331443   9.555   0.0000  1.0111846  1.5331006
rcs():3         0.0005585  0.1331444   0.004   0.9967 -0.2603997  0.2615167
location       -3.7102135  0.3665079 -10.123   0.0000 -4.4285559 -2.9918711
_cons           9.2959865  0.2531929  36.715   0.0000  8.7997376  9.7922355
log_sd(resid.) -0.4272951  0.1440364  -2.967   0.0030 -0.7096012 -0.1449890
log_sd(M1)      0.5468268  0.0827878   6.605   0.0000  0.3845657  0.7090879

相关内容

最新更新