我尝试用数据编码线性混合效应模型,对6个变量进行48次观察,生存作为对父母环境的响应,后代环境作为固定效应,种群作为随机效应。我有三个人口编码为两个变量。
编辑:这是我原始数据的一个子集。
> dput(head(survivaldata,4))
structure(list(pop3 = c(0L, 0L, 0L, 0L), pop5 = c(1L, 1L, 1L,
1L), parent.env = c(0L, 0L, 0L, 0L), env = c(1L, 1L, 1L, 1L),
survived = c(33L, 29L, 29L, 50L), died = c(17L, 21L, 21L,
0L)), .Names = c("pop3", "pop5", "parent.env", "env", "survived",
"died"), spec = structure(list(cols = structure(list(pop3 = structure(list(), class = c("collector_integer",
"collector")), pop5 = structure(list(), class = c("collector_integer",
"collector")), parent.env = structure(list(), class = c("collector_integer",
"collector")), env = structure(list(), class = c("collector_integer",
"collector")), survived = structure(list(), class = c("collector_integer",
"collector")), died = structure(list(), class = c("collector_integer",
"collector"))), .Names = c("pop3", "pop5", "parent.env", "env",
"survived", "died")), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"), row.names = c(NA,
4L), class = c("tbl_df", "tbl", "data.frame"))
当我首先将变量分配给对象时:
> survival <- cbind(survived,died)
> population <- cbind(pop3,pop5)
然后使用以下语法运行模型:
> m1 <- glmer(survival ~ parent.env + env + parent.env:env + (1|population), family = binomial)
我收到此错误:
Error in `[[<-.data.frame`(`*tmp*`, i, value = c(1L, 1L, 1L, 1L, 1L, 1L, :
replacement has 96 rows, data has 48
但是,当我不首先分配对象并运行这样的模型时:
> m1 <- glmer(cbind(survived,died) ~ parent.env + env + parent.env:env + (1|cbind(pop3,pop5)), family = binomial)
我得到了这个错误:
Error: (p <- ncol(X)) == ncol(Y) is not TRUE
我为这些错误查找的所有解决方案都涉及正确计算数据中的NA,但我的数据集没有任何NA。我是R和GLMM的新手,所以任何帮助都将不胜感激。感谢。
编辑:我根据建议运行了以下代码:
> survivaldata$population <- with(survivaldata,paste0(pop3,pop5))
> m1 <- glmer(survival ~ parent.env + env + parent.env:env + (1|survivaldata$population), family = binomial)
现在我得到了这个错误:
Error in model.frame.default(drop.unused.levels = TRUE, formula = survival ~ :
variable lengths differ (found for 'parent.env')
编辑:没关系,我只是不正确地定义了生存。问题现在解决了,谢谢。