我有几个模型,例如下面的示例,我以整齐的格式将估计值,标准误差,p值,r2等作为data.frames,但是我没有原始模型对象(运行了分析在另一台机器上)。
require(broom)
model <- lm(mpg ~ hp + cyl, mtcars)
tidy_model <- tidy(model)
glance_model <- glance(model)
# tidy_model
# # A tibble: 3 x 5
# term estimate std.error statistic p.value
# <chr> <dbl> <dbl> <dbl> <dbl>
# 1 (Intercept) 36.9 2.19 16.8 1.62e-16
# 2 hp -0.0191 0.0150 -1.27 2.13e- 1
# 3 cyl -2.26 0.576 -3.93 4.80e- 4
# glance(model)
# # A tibble: 1 x 11
# r.squared adj.r.squared sigma ...
# * <dbl> <dbl> <dbl> ...
# 1 0.760 0.743 3.06 ...
存在多个软件包(例如stargazer
或texreg
),这些软件包将一个或多个模型对象(lm
,glm
等)转换为格式正确的回归表并排,请参见下面的texreg
示例:
require(texreg)
screenreg(list(model1, model1)
# =================================
# Model 1 Model 2
# ---------------------------------
# (Intercept) 34.66 *** 34.66 ***
# (2.55) (2.55)
# cyl -1.59 * -1.59 *
# (0.71) (0.71)
# disp -0.02 -0.02
# (0.01) (0.01)
# ---------------------------------
# R^2 0.76 0.76
# Adj. R^2 0.74 0.74
# Num. obs. 32 32
# RMSE 3.06 3.06
# =================================
# *** p < 0.001, ** p < 0.01, * p < 0.05
是否有类似的程序包,它使用由broom
产生的整洁估计结果作为输入而不是模型对象来生成诸如上述示例的表格?
答案 0 :(得分:6)
是否有类似的程序包,使用扫帚产生的整洁估计结果作为输入
据我所知,stargazer
允许您使用自定义输入来生成回归表。这使我们可以创建“假” shell表,并用整齐的表中的值填充。使用您的示例
# create fake models
dat <- lapply(tidy_model$term, function(...) rnorm(10))
dat <- as.data.frame(setNames(dat, c("mpg", tidy_model$term[-1])))
f <- as.formula(paste("mpg ~", paste(tidy_model$term[-1], collapse = " + ")))
fit <- lm(f, dat)
# set up model statistics
fit_stats <- data.frame(labels = names(glance_model),
mod1 = round(unlist(glance_model), 3),
mod2 = round(unlist(glance_model), 3),
row.names = NULL,
stringsAsFactors = FALSE)
然后我们可以将这些值输入stargazer
:
图书馆(观景台)
stargazer(fit, fit, type = "text",
coef = list(tidy_model$estimate, tidy_model$estimate),
se = list(tidy_model$std.error, tidy_model$std.error),
add.lines = lapply(1:nrow(fit_stats), function(i) unlist(fit_stats[i, ])),
omit.table.layout = "s"
)
# ==========================================
# Dependent variable:
# ----------------------------
# mpg
# (1) (2)
# ------------------------------------------
# hp -0.019 -0.019
# (0.015) (0.015)
# cyl -2.265*** -2.265***
# (0.576) (0.576)
# Constant 36.908*** 36.908***
# (2.191) (2.191)
# ------------------------------------------
# r.squared 0.741 0.741
# adj.r.squared 0.723 0.723
# sigma 3.173 3.173
# statistic 41.422 41.422
# p.value 0 0
# df 3 3
# logLik -80.781 -80.781
# AIC 169.562 169.562
# BIC 175.425 175.425
# deviance 291.975 291.975
# df.residual 29 29
# ==========================================
# Note: *p<0.1; **p<0.05; ***p<0.01
答案 1 :(得分:3)
受this的启发,我又对texreg
进行了研究,除先前的方法外,还为texreg
定义了另一种提取方法,这是一种更本地化的方法答案:
extract_broom <- function(tidy_model, glance_model) {
# get estimates/standard errors from tidy
coef <- tidy_model$estimate
coef.names <- as.character(tidy_model$term)
se <- tidy_model$std.error
pvalues <- tidy_model$p.value
# get goodness-of-fit statistics from glance
glance_transposed <- as_tibble(cbind(name = names(glance_model), t(glance_model)))
gof.names <- as.character(glance_transposed$name)
gof <- as.double(glance_transposed$value)
gof.decimal <- gof %% 1 > 0
tr_object <- texreg::createTexreg(coef.names = coef.names,
coef = coef,
se = se,
pvalues = pvalues,
gof.names = gof.names,
gof = gof,
gof.decimal = gof.decimal)
return(tr_object)
}
这将产生以下输出:
texreg_model <- extract_broom(tidy_model, glance_model)
screenreg(list(texreg_model, texreg_model))
# =====================================
# Model 1 Model 2
# -------------------------------------
# (Intercept) 36.91 *** 36.91 ***
# (2.19) (2.19)
# hp -0.02 -0.02
# (0.02) (0.02)
# cyl -2.26 *** -2.26 ***
# (0.58) (0.58)
# -------------------------------------
# r.squared 0.74 0.74
# adj.r.squared 0.72 0.72
# sigma 3.17 3.17
# statistic 41.42 41.42
# p.value 0.00 0.00
# df 3 3
# logLik -80.78 -80.78
# AIC 169.56 169.56
# BIC 175.42 175.42
# deviance 291.97 291.97
# df.residual 29 29
# =====================================
# *** p < 0.001, ** p < 0.01, * p < 0.05