【发布时间】: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')
【问题讨论】: