LMER: Error in model.frame.default… variable lengths differ (Without NA's)

狂风中的少年 提交于 2019-12-24 07:49:26

问题


I've looked through a couple of other questions similar to mine, and the one most relevant wasn't answered, while the others all came down to missing data and data lengths.

I am trying to model compositional dissmiliarity after a disturbance through time. I sampled at discrete timepoints: 0, 3, 6, and 9 months. I am interested in the fixed effect of time on recovery, but at each point, season and other environmental variables changed and led to differing variability between 0, 3, 6, and 9 months, as reflected in the residuals in a normal linear model.

To incorporate the random variance differences, while still including the fixed effect, I ran this model using nlme:

mod1 <- lme(Resp ~ Temp + Time, random = (~1 | Dummy), 
           weights = varIdent(form = ~1 | Time.f), data = recovery)

where Resp is my dissimilarity; Temp is a disturbance intensity (temperature), Time is 0, 3, 6, or 9 months; Dummy is a dummy random intercept (here just a vector filled with "D"); and Time.f is Time converted to a factor. I have 60 data points, 15 in each of the sampling times.

This ran fine. I want to extract conditional-R2 values, however, so tried it in lme4 using the lmer function:

mod2 <- lmer(Resp ~ Temp + Time + (1 | Dummy), weights = varIdent(form = ~1 | Time.f), data = recovery)

To run lmer, the dummy variables needs at least two categories, so I changed the first value to a "C" for the sake of trying to run it (see data below). I get the error:

Error in model.frame.default(data = recovery, weights = varIdent(form = ~1 | : variable lengths differ (found for '(weights)')

I'm new to variance structures and I'm not sure if it's a problem in the fundamental structure of this model. In this question, Alain Zuur implied that it is incorrect to use varIdent with binomial models, but I'm running this as a Gaussian (though I've also considered a Gamma).

Here is my data:

recovery <- data.frame(Resp = c(0.451342292, 0.518351745, 0.469268304,
 0.472750101, 0.498935251, 0.508081111, 0.490904624, 0.446764424, 0.536142651, 
0.707955998, 0.403299657, 0.495046237, 0.630677117, 0.630420366, 0.511555117, 
0.460977339, 0.313873121, 0.442930551, 0.504933216, 0.266894217, 0.461051448, 
0.406211728, 0.500511806, 0.561122944, 0.490329006, 0.565621645, 0.612263178, 
0.380895252, 0.358544132, 0.40361619, 0.296853964, 0.364686954, 0.299307408, 
0.349446067, 0.288854758, 0.292265106, 0.37207794, 0.300760264, 0.387906769, 
0.376203541, 0.433080559, 0.371204216, 0.341724067, 0.357920013, 0.396109361, 
0.372228087, 0.282725524, 0.317789872, 0.244435419, 0.277035603, 0.272620508, 
0.378162153, 0.267645891, 0.306363131, 0.353826191, 0.230613947, 0.445865256, 
0.321665269, 0.324131469, 0.266275828), Temp = c(25.4, 22.9, 22.5, 20.5, 29, 
25.2, 40.5, 23.9, 28.1, 33.2, 43, 34.5, 38.5, 42.4, 31.3, 27, 28.6, 22.4, 24.2, 
21.4, 22.6, 36.6, 28.8, 24.9, 28.6, 41.2, 21.5, 33, 39.8, 30.8, 22.2, 29.5, 
25.9, 26.6, 22.8, 27.7, 24.9, 22.9, 27.6, 29, 26.7, 29.4, 43.5, 28.8, 23.1, 
26.1, 25.7, 23.4, 29.4, 29.6, 23.7, 26.6, 31.7, 28.3, 28.5, 20.5, 23, 35.3, 
42.5, 26.8), Time = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9), Dummy = c("C", "D", "D", "D", 
"D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", 
"D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", 
"D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", 
"D", "D", "D", "D", "D", "D", "D", "D"), Time.f = c("a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b",
 "b", "b", "b", "b", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c", "c", 
"c", "c", "c", "c", "c", "c", "c", "c", "c", "d", "d", "d", "d", "d", "d", "d", 
"d", "d", "d", "d", "d", "d", "d", "d"))

Like I said, I'm new to this and to posting. Any advice or suggestions would be wonderful!

来源:https://stackoverflow.com/questions/40475614/lmer-error-in-model-frame-default-variable-lengths-differ-without-nas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!