英文:
Run lapply with lmer function on list of list
问题
我在使用包含列表的数据集上启动`lmer`函数时遇到问题。我尝试将列表转换为数据框,但仍然返回错误
```r
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)))
> 在 model.frame.default(data = x, drop.unused.levels = TRUE, formula = x[,4:7] ~ var + (1| id), x) 中的变量 'x' 的类型(列表)无效。
有人能建议如何通过lapply
运行lmer
吗?
reprex
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
}
}
假设我想通过每个子列表拟合lmer
模型。例如:
lapply(list3, function(x) lapply(x,
function(x) lmer(x[,3:7] ~ vs + (1| cyl), x)))
它返回了报告的错误。你认为有可能以某种方式强制转换它吗?
<details>
<summary>英文:</summary>
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
```r
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
?
reprex
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?
答案1
得分: 2
不能将矩阵粘贴到公式的左侧,并期望 lmer
使用每列作为响应逐个拟合一系列模型。(这在 lm()
中是可行的,但仅仅是因为线性模型在拟合和解释方式上有一些特殊结构。)
所以,你需要在底层使用另一个 lapply()
或循环来遍历列/响应变量。下面的代码是一种方法。老实说,我可能会在这里使用 for
循环,它们比嵌套的 lapply()
函数更容易调试。
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)
})
})
})
英文:
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)
})
})
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论