【问题标题】:How to regress a list of covariates with a desired predictor and dependent variable and return a table of coefficients and p-values using lme and lmer如何使用所需的预测变量和因变量回归协变量列表并使用 lme 和 lmer 返回系数和 p 值表
【发布时间】:2022-01-13 21:31:43
【问题描述】:

我有一个包含大量变量的数据集。在数据集中,我有一个要调查的预测变量和一个结果变量。我想找到对结果变量有显着影响或预测变量和协变量对结果变量有显着交互作用的协变量。

因此,能够方便地使用因变量上的所需预测变量依次回归所有协变量,并创建一个表格,列出协变量及其各自 p 值的影响和交互影响。

我想做这样的事情:

library(dplyr)

# Generating sample data
set.seed(5)
df <- data.frame(matrix(round(abs(2*rnorm(100*100)), digits = 0), ncol=100))

# Selecting covariates
covar <- names(df)[! names(df) %in% c("X1", "X2")]

# Running the lm function over the list of covariates. I should get the covariate coefficients from each regression, but I get an error when I try run this step.

coeff <- lapply(covar, function(x){ 
# Retrive coefficient matrix
    summary(lm(X1 ~ X2 + x + X2*x, df))$coefficients %>% 
# Coerce into dataframe and filter for covariates and interaction effects
    as.data.frame(.) %>%
    filter(row.names(.) %in% grep(x, rownames(.), value = 
    TRUE))}) %>%
# Finally I want to join all data frames into one
    bind_rows(.)

我可以在语法方面使用一些帮助。尝试运行该函数时出现以下错误:

Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'summary': variable lengths differ (found for 'x')

【问题讨论】:

    标签: r dplyr lapply lm


    【解决方案1】:

    当您在function 中使用x(in lapply) 时,最好将paste 用于模型公式,而不是仅仅指定它的公式。

    lapply(covar, function(x){ 
      modd <- paste0("X1 ~ X2 +", x, "+ X2 *", x)
      summary(lm(modd, df))$coefficients %>% 
        as.data.frame(.) %>%
        filter(row.names(.) %in% grep(x, rownames(.), value = 
                                        TRUE))}) %>%
      bind_rows(.)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 1970-01-01
      • 1970-01-01
      • 2018-11-01
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多