我的df像这样:
ind1 <- rnorm(99)
ind2 <- rnorm(99)
ind3 <- rnorm(99)
ind4 <- rnorm(99)
ind5 <- rnorm(99)
dep <- rnorm(99, mean=ind1)
group <- rep(c("A", "B", "C"), each=33)
df <- data.frame(dep, group, ind1, ind2, ind3, ind4, ind5)
此函数结合了不同类型的回归方程。
functions <- function(x, y) {
eq1 <- lm(dep ~ x + y)
eq2 <- lm(dep ~ I(x*y))
eq3 <- lm(log(dep) ~ I(log(x+1)^2) + I(log(y+1)^2))
list <- list(eq1, eq2, eq3)
names(list) <- paste0("mod", 1:3)
return(list)
}
然后按如下所示将此函数按组应用于ind1和ind2变量。
out <- lapply(split(df, df$group), function(x) functions(x$ind1, x$ind2))
lapply(out, summary)
但是有一个错误提示变量长度不同(为'x'找到)?那我该如何解决这个错误呢?
谢谢!
答案 0 :(得分:2)
在这里,问题在于公式需要列名而不是值
functions <- function(dat, x, y) {
form1 <- paste0("dep ~ ", x, " + ", y)
form2 <- paste0("dep ~ ", "I(", x, "*", y, ")")
form3 <- paste0("log(dep) ~", "I(log(", x, "+1)^2) + I(log(", y, "+1)^2)")
eq1 <- lm(form1, data = dat)
eq2 <- lm(form2, data = dat)
eq3 <- lm(form3, data = dat)
list1 <- list(eq1, eq2, eq3)
names(list1) <- paste0("mod", 1:3)
return(list1)
}
out <- lapply(split(df, df$group), function(x) functions(x, "ind1", "ind2"))
另外,由于输出是嵌套的list
,我们可能需要进入内部list
才能提取summary
lapply(out, function(x) lapply(x, summary))