简体   繁体   English

将 purrr 派生列表输入到 r 中的 tab_model

[英]Input purrr derived list into tab_model in r

I would like to create a table of linear models with all independent variables at the top, and all dependent variables on the left side.我想创建一个线性模型表,顶部所有自变量,左侧所有因变量。 This can be done using the tab_model function of the sjPlot package: https://cran.r-project.org/web/packages/sjPlot/vignettes/tab_model_estimates.html . This can be done using the tab_model function of the sjPlot package: https://cran.r-project.org/web/packages/sjPlot/vignettes/tab_model_estimates.html .

While the documentation specifies how to get a multi-model table from inputting each model one-by-one (m1, m2, m3 etc), what I have is a purr-derived list of linear models.虽然文档指定了如何通过逐个输入每个 model(m1、m2、m3 等)来获取多模型表,但我所拥有的是由 purr 派生的线性模型列表。 I searched the documentation and online forums but can't find anything on how to do this.我搜索了文档和在线论坛,但找不到任何有关如何执行此操作的信息。 This is an example:这是一个例子:

library(sjPlot)
library(sjmisc)
library(sjlabelled)
library(tidyverse)

df <- data.frame(study_id = c(1:20),
                 leptin = runif(20),
                 insulin = runif(20),
                 gene1 = runif(20),
                 gene2 = runif(20),
                 age = runif(20, min = 20, max = 45),
                 sex = sample(c(0,1), size = 20, replace = TRUE))

model_function_covariates_for_table <- function(x,y) {
  M1 <- paste0(x, "~", y, "+ age + sex")
  M1_fit <- lm(M1, data = df)
  return(M1_fit)
}

bioactives <- names(df)[c(2:3)]
genes <- names(df)[c(4:5)]

bioactives <- purrr::set_names(bioactives)
genes <- purrr::set_names(genes)

# Works
m <- model_function_covariates_for_table("leptin", "gene1")

tab_model(m)

# Doesn't work
m <- map(bioactives,
         ~map(genes, model_function_covariates_for_table, y = .x))

tab_model(m)

Error message:错误信息:

Error in if (fam.info$is_linear) transform <- NULL else transform <- "exp" : 
  argument is of length zero
In addition: Warning message:
Could not access model information.

Thanks in advance for any advice.提前感谢您的任何建议。

Here we need map2 as we are passing the corresponding elements of 'bioactives' and 'genes' into the function model_function_covariates_for_table在这里,我们需要map2 ,因为我们将“生物活性”和“基因”的相应元素传递到 function model_function_covariates_for_table

library(purrr)
map2(bioactives, genes, ~ model_function_covariates_for_table(.x, .y))

-output -输出

$leptin

Call:
lm(formula = M1, data = df)

Coefficients:
(Intercept)        gene1          age          sex  
   0.692454    -0.200365    -0.001285     0.044467  


$insulin

Call:
lm(formula = M1, data = df)

Coefficients:
(Intercept)        gene2          age          sex  
  0.7475482   -0.1388409   -0.0006337   -0.1919895  

The tab_model will be applied on each of those elements tab_model将应用于这些元素中的每一个

out <- map2(bioactives, genes, ~ 
         model_function_covariates_for_table(.x, .y) %>% 
          tab_model)

out[[1]]

在此处输入图像描述

out[[2]]

在此处输入图像描述


In base R , the corresponding function is Mapbase R中,对应的 function 是Map

Map(model_function_covariates_for_table, bioactives, genes)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM