用不同的因变量重复回归
Repeat regression with varying dependent variable
我在 Stack 和 google 中搜索了解决方案,none 找到了解决我问题的方法。
我有大约 40 个因变量,我的目标是获得调整均值 (lsmeans)。在考虑了一些协变量之后,我需要对 A 组和 B 组进行调整。我的最终对象应该是一个数据框,其中包含 A 组和 B 组的所有 40 个因变量的预测均值。
这是我试过的,没有成功:
# Examplified here with 2 outcome variables
outcome1 <- c(2, 4, 6, 8, 10, 12, 14, 16)
outcome2 <- c(1, 2, 3, 4, 5, 6, 7, 8)
var1 <- c("a", "a", "a", "a", "b", "b", "b", "b")
var2 <- c(10, 11, 12, 9, 14, 9, 5, 8)
var3 <- c(100, 101, 120, 90, 140, 90, 50, 80)
df <- data.frame(outcome1, outcome2, var1, var2, var3)
dependents <- c(outcome1, outcome2)
library(lsmeans) #install.packages("lsmeans")
results <- list()
for (i in seq_along(dependents) {
fit <- lm(i ~ var1 + var2 + var3, data= df)
summary <- summary(lsmeans(fit, "var1"))
summary$outcome <- i
results[i] <- summary
}
有一些错别字之类的,但我想这就是你想要的:
# Examplified here with 2 outcome variables
outcome1 <- c(2, 4, 6, 8, 10, 12, 14, 16)
outcome2 <- c(1, 2, 3, 4, 5, 6, 7, 8)
var1 <- c("a", "a", "a", "a", "b", "b", "b", "b")
var2 <- c(10, 11, 12, 9, 14, 9, 5, 8)
var3 <- c(100, 101, 120, 90, 140, 90, 50, 80)
df <- data.frame(outcome1, outcome2, var1, var2, var3)
dependents <- c("outcome1", "outcome2")
library(lsmeans) #install.packages("lsmeans")
results <- list()
for (i in seq_along(dependents)) {
eq <- paste(dependents[i],"~ var1 + var2 + var3")
fit <- lm(as.formula(eq), data= df)
summary <- summary(lsmeans(fit, "var1"))
summary$outcome <- i
results[[i]] <- summary
}
这是另一个使用 lapply
的选项。
dependents <- c('outcome1', 'outcome2')
lst <- lapply(dependents, function(x) {
fit <- lm(paste(x,'~', 'var1+var2+var3'), data=df)
summary(lsmeans(fit, 'var1', data=df))})
Map(cbind, lst, outcome = seq_along(dependents))
在更现代的 R 中,the lazyeval package 提供了更好的公式处理函数。
这是我的代码版本:
#load libs
library(tidyverse)
library(lazyeval)
library(lsmeans)
#make data
df = tibble(
y1 = c(2, 4, 6, 8, 10, 12, 14, 16),
y2 = c(1, 2, 3, 4, 5, 6, 7, 8),
var1 = c("a", "a", "a", "a", "b", "b", "b", "b"),
var2 = c(10, 11, 12, 9, 14, 9, 5, 8),
var3 = c(100, 101, 120, 90, 140, 90, 50, 80)
)
#outcomes
outcomes = c("y1", "y2")
#fit
results <- list()
for (i in seq_along(outcomes)) {
#make a formula
f = i ~ var1 + var2 + var3
#set outcome, must be a symbol explicitly
f_lhs(f) = as.symbol(outcomes[i])
#fit
fit <- lm(f, data = df)
#save
summary <- summary(lsmeans(fit, "var1"))
results[[i]] = summary
}
#set outcome names
names(results) = outcomes
#print results
results
最后一行打印:
$y1
var1 lsmean SE df lower.CL upper.CL
a 5.5 1.38 4 1.68 9.32
b 12.5 1.38 4 8.68 16.32
Confidence level used: 0.95
$y2
var1 lsmean SE df lower.CL upper.CL
a 2.75 0.688 4 0.84 4.66
b 6.25 0.688 4 4.34 8.16
Confidence level used: 0.95
通常,使用字符串并在拟合前转换为公式会更容易。我在这里使用公式来完成。
我在 Stack 和 google 中搜索了解决方案,none 找到了解决我问题的方法。
我有大约 40 个因变量,我的目标是获得调整均值 (lsmeans)。在考虑了一些协变量之后,我需要对 A 组和 B 组进行调整。我的最终对象应该是一个数据框,其中包含 A 组和 B 组的所有 40 个因变量的预测均值。
这是我试过的,没有成功:
# Examplified here with 2 outcome variables
outcome1 <- c(2, 4, 6, 8, 10, 12, 14, 16)
outcome2 <- c(1, 2, 3, 4, 5, 6, 7, 8)
var1 <- c("a", "a", "a", "a", "b", "b", "b", "b")
var2 <- c(10, 11, 12, 9, 14, 9, 5, 8)
var3 <- c(100, 101, 120, 90, 140, 90, 50, 80)
df <- data.frame(outcome1, outcome2, var1, var2, var3)
dependents <- c(outcome1, outcome2)
library(lsmeans) #install.packages("lsmeans")
results <- list()
for (i in seq_along(dependents) {
fit <- lm(i ~ var1 + var2 + var3, data= df)
summary <- summary(lsmeans(fit, "var1"))
summary$outcome <- i
results[i] <- summary
}
有一些错别字之类的,但我想这就是你想要的:
# Examplified here with 2 outcome variables
outcome1 <- c(2, 4, 6, 8, 10, 12, 14, 16)
outcome2 <- c(1, 2, 3, 4, 5, 6, 7, 8)
var1 <- c("a", "a", "a", "a", "b", "b", "b", "b")
var2 <- c(10, 11, 12, 9, 14, 9, 5, 8)
var3 <- c(100, 101, 120, 90, 140, 90, 50, 80)
df <- data.frame(outcome1, outcome2, var1, var2, var3)
dependents <- c("outcome1", "outcome2")
library(lsmeans) #install.packages("lsmeans")
results <- list()
for (i in seq_along(dependents)) {
eq <- paste(dependents[i],"~ var1 + var2 + var3")
fit <- lm(as.formula(eq), data= df)
summary <- summary(lsmeans(fit, "var1"))
summary$outcome <- i
results[[i]] <- summary
}
这是另一个使用 lapply
的选项。
dependents <- c('outcome1', 'outcome2')
lst <- lapply(dependents, function(x) {
fit <- lm(paste(x,'~', 'var1+var2+var3'), data=df)
summary(lsmeans(fit, 'var1', data=df))})
Map(cbind, lst, outcome = seq_along(dependents))
在更现代的 R 中,the lazyeval package 提供了更好的公式处理函数。
这是我的代码版本:
#load libs
library(tidyverse)
library(lazyeval)
library(lsmeans)
#make data
df = tibble(
y1 = c(2, 4, 6, 8, 10, 12, 14, 16),
y2 = c(1, 2, 3, 4, 5, 6, 7, 8),
var1 = c("a", "a", "a", "a", "b", "b", "b", "b"),
var2 = c(10, 11, 12, 9, 14, 9, 5, 8),
var3 = c(100, 101, 120, 90, 140, 90, 50, 80)
)
#outcomes
outcomes = c("y1", "y2")
#fit
results <- list()
for (i in seq_along(outcomes)) {
#make a formula
f = i ~ var1 + var2 + var3
#set outcome, must be a symbol explicitly
f_lhs(f) = as.symbol(outcomes[i])
#fit
fit <- lm(f, data = df)
#save
summary <- summary(lsmeans(fit, "var1"))
results[[i]] = summary
}
#set outcome names
names(results) = outcomes
#print results
results
最后一行打印:
$y1
var1 lsmean SE df lower.CL upper.CL
a 5.5 1.38 4 1.68 9.32
b 12.5 1.38 4 8.68 16.32
Confidence level used: 0.95
$y2
var1 lsmean SE df lower.CL upper.CL
a 2.75 0.688 4 0.84 4.66
b 6.25 0.688 4 4.34 8.16
Confidence level used: 0.95
通常,使用字符串并在拟合前转换为公式会更容易。我在这里使用公式来完成。