I am struggling with launching the lmer
function on a data set comprising a list of lists. I have tried converting the list as a data frame too, but it returns an error
lapply(list, function(x) lapply(x, function(x) as.data.frame(x)))
lapply(list, function(x) lapply(x, function(x)
lmer(x[,4:7] ~ var + (1| id), x)))
Error in model.frame.default(data = x, drop.unused.levels = TRUE, formula = x[, : not valid type (list) for the variable 'x'
Can anyone suggest something for running lmer
through lapply
?
library(tidyverse)
library(readxl)
require(writexl)
write_xlsx(mtcars, 'PQ_ele_com.xlsx')
dir = c('com', 'set', 'rit')
init = c('PQ', 'MG')
inpst = c('_ele_', '_mel_', '_col_')
for (i in dir) {
for (j in init) {
for (k in inpst){
write_xlsx(mtcars, paste0(j, k, i, '.xlsx'))
}
}
}
files = list.files(recursive= FALSE, full.names= FALSE)
init = c('PQ', 'MG')
dir = c('com', 'set', 'rit')
list3 = NULL
for (j in init){
for (k in dir){
df=c()
for (i in files){
if(str_detect(i, pattern = j) & str_detect(i, pattern = k)) {
df=rbind(df,read_excel(i))}
}
list3[[j]][[k]] = df
}
}
Let's suppose I would like to fit lmer
model through each sublists. For example:
lapply(list3, function(x) lapply(x,
function(x) lmer(x[,3:7] ~ vs + (1| cyl), x)))
It returns the error reported. Do you think it is possible to coerce it somehow?
You can't stick a matrix on the left-hand side of a formula and expect lmer
to fit a series of models using each of the columns as the response in turn. (This works for lm()
, but only because of some special structure in the way that linear models are fitted and interpreted.)
So, you need another lapply()
or loop at the bottom level to iterate over columns/response variables. Code below is one way to do it. To be honest, I might use for
loops instead here, they're easier to debug than nested lapply()
functions ...
res <-
lapply(list3,
function(x) { lapply(x,
function(y) { lapply(names(y)[3:7],
function(n) {
## browser()
form <- reformulate(c("vs", "(1|cyl)"), response = n)
lmer(form, data = y)
})
})
})