Summary of the lme4 model in function (lmerTest)

If you have the following data

d = data.frame(out=rnorm(10), explain=rnorm(10), age=rnorm(10), sex=sample(c("M", "F"), size=10, replace=T), group=rep(c(1:5), 2)) f = as.formula("out ~ explain + age + sex + (1|group)") 

and wants to install a linear model using lme4, you can do

 require(lme4) require(lmerTest) m = lmer(f, d) s = summary(m) 

This works, well .... But if the model is installed in another function, for example

 gglm = function(form, data){ lm = lmer(form, data=data) return(lm) } m2 = gglm(f, d) s2 = summary(m2) 

I get an error message.

 summary from lme4 is returned some computational error has occurred in lmerTest 

Apparently, this is due to the fact that the model was fitted using an object called data , which is not displayed in the outer area. Therefore, if I do data = d , I get the same result as before. However, if I do

 data = data.frame(out=rnorm(10), explain=rnorm(10), age=rnorm(10), sex=sample(c("M", "F"), size=10, replace=T), group=rep(c(1:5), 2)) 

and get different data, the result of the summary is incorrect.

This does not seem to be the best way to do this, and I think it is easy to make mistakes. Normal lm and its summary function do not have this problem. Isn't there a way to make lmerTest less error prone?

+5
source share
1 answer

The do.call trick works (but this is a workaround, of course).

 gglm = function(form, data){ lm = do.call(lmer, list(formula=form, data=data)) return(lm) } m2 = gglm(f, d) s2 = summary(m2) 
+2
source

Source: https://habr.com/ru/post/1209366/


All Articles