如何使用 lme 和 lmer 回归具有所需预测变量和因变量的协变量列表以及 return a table 系数和 p 值
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
我有一个包含大量变量的数据集。在数据集中,我有一个预测变量和一个要调查的结果变量。我想找到对结果变量有显着影响的协变量,或者预测变量和协变量之间对结果变量有显着交互作用的协变量。
因此,如果能够使用所需的因变量预测变量依次回归所有协变量并创建 table 协变量及其各自的 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')
当您在 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(.)
我有一个包含大量变量的数据集。在数据集中,我有一个预测变量和一个要调查的结果变量。我想找到对结果变量有显着影响的协变量,或者预测变量和协变量之间对结果变量有显着交互作用的协变量。
因此,如果能够使用所需的因变量预测变量依次回归所有协变量并创建 table 协变量及其各自的 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')
当您在 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(.)