[英]Using stargazer with a list of lm objects created by lapply-ing over a split data.frame
I'm trying to create a stargazer table for a set of regressions, where I ran each regression on a subset of my data. 我正在尝试为一组回归创建观星表,在该表中我对数据的一个子集运行了每个回归。 The natural way to do this, I would think, is to use
split
to create a list of data.frames from my data, create a list of lm objects by using lapply
on the list of data.frames, and then feed that list to stargazer
. 我认为,执行此操作的自然方法是使用
split
从我的数据创建data.frames列表,通过对data.frames列表使用lapply
创建lm对象的列表,然后将该列表提供给stargazer
。 For example, 例如,
library(MASS)
library(stargazer)
data(Boston)
# This doesn't work
by.river <- split(Boston, Boston$chas)
fit <- lapply(by.river, lm, formula = crim ~ indus)
stargazer(fit, type = "text")
# % Error: Unrecognized object type.
# % Error: Unrecognized object type.
If I divide them up manually, this works fine: 如果我将它们手动分割,效果很好:
# This works
fit2 <- vector(mode = "list", length = 2)
fit2[[1]] <- lm(crim ~ indus, data = Boston, subset = (chas == 0))
fit2[[2]] <- lm(crim ~ indus, data = Boston, subset = (chas == 1))
stargazer(fit2, type = "text")
But with my real data, the thing I'm splitting by has several values, and I would rather not split them all up by hand. 但是根据我的真实数据,我要拆分的东西具有多个值,我宁愿不手工拆分它们。 Any ideas why I'm getting the "% Error: Unrecognized object type."
为什么我得到“%错误:无法识别的对象类型”的任何想法。 error?
错误?
There is an easy workaround, hinted at by BondedDust and suggested by careful perusal of the help for lapply
. 有一个简单的解决方法,由BondedDust暗示,并通过仔细阅读
lapply
的帮助来lapply
。
fit <- lapply(by.river, function(dd)lm(crim ~ indus,data=dd))
stargazer(fit, type = "text")
fit[[1]]$call
#lm(formula = crim ~ indus, data = dd)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.