在 R 中一次输出每个变量的回归统计

Output Regression statistics for each variable one at a time in R

我有一个看起来像这样的数据框。名称和列数将不一致(有时 'C' 不存在,其他时候“D'、'E'、'F' 可能存在,等等)。唯一一致的变量永远是Y,我要逆着Y倒退。

# name and number of columns varies...so need flexible process
Y <- c(4, 4, 3, 4, 3, 2, 3, 2, 2, 3, 4, 4, 3, 4, 8, 6, 5, 4, 3, 6)
A <- c(1, 2, 1, 2, 3, 2, 1, 1, 1, 2, 1, 4, 3, 1, 2, 2, 1, 2, 4, 8)
B <- c(5, 6, 6, 5, 3, 7, 2, 1, 1, 2, 7, 4, 7, 8, 5, 7, 6, 6, 4, 7)
C <- c(9, 1, 2, 2, 1, 4, 5, 6, 7, 8, 89, 9, 7, 6, 5, 6, 8, 9 , 67, 6)
YABC <- data.frame(Y, A, B, C)

我想遍历每个变量并收集回归模型的输出。

此过程创建所需的输出,但仅针对此特定迭代。

model_A <- lm(Y ~ A, YABC)

ID <- 'A'
rsq <- summary(model_A)$r.squared
adj_rsq <- summary(model_A)$adj.r.squared
sig <- summary(model_A)$sigma

datA <- data.frame(ID, rsq, adj_rsq, sig)

model_B <- lm(Y ~ B, YABC)

ID <- 'B'
rsq <- summary(model_B)$r.squared
adj_rsq <- summary(model_B)$adj.r.squared
sig <- summary(model_B)$sigma

datB <- data.frame(ID, rsq, adj_rsq, sig)

model_C <- lm(Y ~ C, YABC)

ID <- 'C'
rsq <- summary(model_C)$r.squared
adj_rsq <- summary(model_C)$adj.r.squared
sig <- summary(model_C)$sigma

datC <- data.frame(ID, rsq, adj_rsq, sig)

output <- rbind(datA, datB, datC)

我如何将其包装在一个循环或一些其他进程中,以解释列的不同数量和名称?这是我的尝试...是的,我知道这是不对的,只是我在概念化我想要的那种能力。

# initialize data frame
output__ <- data.frame(ID__ = as.character(),
                     rsq__ = as.numeric(),
                     adj_rsq__ = as.numeric(),
                     sig__ = as.numeric())

# loop through A, then B, then C
for(i in A:C) {
  model_[i] <- lm(Y ~ [i], YABC)

  ID <- '[i]'
  rsq <- summary(model_[i])$r.squared
  adj_rsq <- summary(model_[i])$adj.r.squared
  sig <- summary(model_[i])$sigma
  data__temp <- (ID__, rsq__, adj_rsq__, sig__)
  data__ <- rbind(data__, data__temp)
}

使用@BigDataScientist 方法...这是我采用的解决方案。

# initialize data frame
data__ <- data.frame(ID__ = as.character(),
                     rsq__ = as.numeric(),
                     adj_rsq__ = as.numeric(),
                     sig__ = as.numeric())

# loop through A, then B, then C
for(char in names(YABC)[-1]){
  model <- lm(as.formula(paste("Y ~ ", char)), YABC)
  ID__ <- paste(char)
  rsq__ <- summary(model)$r.squared
  adj_rsq__ <- summary(model)$adj.r.squared
  sig__ <- summary(model)$sigma
  data__temp <- data.frame(ID__, rsq__, adj_rsq__, sig__)
  data__ <- rbind(data__, data__temp)

}

如评论中所写:?as.formula() 是一种解决方案。 你会这样做:

model = list()
for(char in names(YABC)[-1]) {
  model[[char]] <- lm(as.formula(paste("Y ~ ", char)), YABC)
}
model

这是一个使用 *apply 的解决方案:

Y <- c(4, 4, 3, 4, 3, 2, 3, 2, 2, 3, 4, 4, 3, 4, 8, 6, 5, 4, 3, 6)
A <- c(1, 2, 1, 2, 3, 2, 1, 1, 1, 2, 1, 4, 3, 1, 2, 2, 1, 2, 4, 8)
B <- c(5, 6, 6, 5, 3, 7, 2, 1, 1, 2, 7, 4, 7, 8, 5, 7, 6, 6, 4, 7)
C <- c(9, 1, 2, 2, 1, 4, 5, 6, 7, 8, 89, 9, 7, 6, 5, 6, 8, 9 , 67, 6)
YABC <- data.frame(Y, A, B, C)

names <- colnames(YABC[-1])

formulae <- sapply(names,function(x)as.formula(paste('Y~',x)))

lapply(formulae, function(x) lm(x, data = YABC))

当然你也可以调用总结:

lapply(formulae, function(x) summary(lm(x, data = YABC)))

如果要从特定模型中提取变量,请执行以下操作:

results <- lapply(formulae, function(x) lm(x, data = YABC))
results$A$coefficients

使用 A 作为解释变量给出模型的系数

这就是我做这种建模的方式。以下示例假设我改变不同的结果,并针对给定的一组协变量进行不同的曝光。

我首先定义我想要测试的结果和暴露(我认为在流行病学方面,但你可以扩展)。

outcomes <- c("a","b","c","d")

exposures <- c("exp1","exp2","exp3")

假设这些向量中指定的每个元素作为列名存在于您的数据集中(以及下面“~”后列出的协变量)。

final_lm_data <- data.frame() #initialize empty dataframe to hold results
for (j in 1:length(exposures){
  for (i in 1:length(outcomes){
    mylm <- lm(formula(paste(outcomes[i], "~", "continuous.cov.1 + 
        continuous.cov.2 + factor(categorical.variable.1)", "+",
                             exposure[j])), data=mydata)

    coefficent.table <- as.data.frame(coef(summary(mylm)))

    mylm_data <- as.data.frame(cbind(ctable,Variable = rownames(ctable),
                                     Outcome = outcomes[i],
                                     Exposure = exposures[j],
                                     Model_N = paste(length(mylm$residuals))))
    names(mylm_data)[4] <- "Pvalue"  # renaming the "Pr(>|t|)"
    rownames(mylm_data) <- NULL # important because we are creating stacked output dataset
    final_lm_data <- rbind(final_lm_data,mylm_data)
  }
}

这将为您提供一个 final_lm_data,其中包含您的估计值、std.errors、tstatistics、模型中每个变量的 pvalue,并且还会跟踪结果和曝光的迭代(第一次和模型的最后一个元素)。最后,它在删除缺失值的数据记录后使用了 N。您可以修改 mylm_data 创建以从模型中捕获更多信息(例如 rsq 等)。

最后,如果协变量也不同于 运行 运行,我不确定如何自动化该部分。