运行`lapply`函数,对列表的列表使用`lmer`函数。

huangapple go评论56阅读模式
英文:

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, &#39;PQ_ele_com.xlsx&#39;)
        
dir = c(&#39;com&#39;, &#39;set&#39;, &#39;rit&#39;)
init = c(&#39;PQ&#39;, &#39;MG&#39;)
inpst = c(&#39;_ele_&#39;, &#39;_mel_&#39;, &#39;_col_&#39;)
           
for (i in dir) {
   for (j in init) {
      for (k in inpst){
        write_xlsx(mtcars, paste0(j, k, i, &#39;.xlsx&#39;)) 
      }
   }
}
    
files = list.files(recursive= FALSE, full.names= FALSE)
     
init = c(&#39;PQ&#39;, &#39;MG&#39;)
dir = c(&#39;com&#39;, &#39;set&#39;, &#39;rit&#39;)
     
list3 = NULL
for (j in init){
   for (k in dir){
      df=c()
      for (i in files){
           if(str_detect(i, pattern = j) &amp; 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 &lt;- 
lapply(list3, 
    function(x) { lapply(x, 
        function(y) { lapply(names(y)[3:7],
            function(n) {
               ## browser()
               form &lt;- reformulate(c(&quot;vs&quot;, &quot;(1|cyl)&quot;), response = n)
               lmer(form, data = y) 
            })
        })
    })

huangapple
  • 本文由 发表于 2023年4月17日 18:44:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定