简体   繁体   English

Bayesglm的回归表?

[英]Regression table for bayesglm?

stargazer is a great tool to generate a regression table if you are not using bayesglm . 如果您不使用bayesglm stargazer是生成回归表的好工具。 For example, suppose I have the following data: 例如,假设我有以下数据:

library(dplyr)
set.seed(9782)
N<-1000
df1 <- data.frame(v1=sample(c(0,1),N,replace = T),
                  v2=sample(c(0,1),N,replace = T),
                  Treatment=sample(c("A", "B", "C"), N, replace = T),
                  noise=rnorm(N)) %>% 
  mutate(Y=0.5*v1-0.7*v2+2*I(Treatment=="B")+3*I(Treatment=="C")+noise)

I can run lm and then create html (or text) output for my r markdown: 我可以运行lm ,然后为我的r降价创建html(或文本)输出:

lm(data = df1, formula = Y~Treatment+v1+v2) %>% 
  stargazer::stargazer(type="html", style = "qje")

Is there a way to do something similar for bayesglm ? 有没有办法对bayesglm做类似的bayesglm In this case, pointEstimate has the coefficients and standardError the standard errors 在这种情况下, pointEstimate具有系数, standardError具有标准误差

library(arm)
fit <- bayesglm(data = df1, formula = Y~Treatment+v1+v2)
posteriorDraws <- coef(sim(fit, n.sims=5000))
pointEstimate <- colMeans(posteriorDraws)
standardError <- apply(posteriorDraws, 2, sd)

It looks like this does the trick: 看起来像这样:

library(texreg)
htmlreg(fit)

and for text: 和文本:

screenreg(list(fit))

As @rawr points out in comments stargazer is -- while useful -- unfortunately written in a rather monolithic, hard-to-extend style. 正如@rawr在评论中指出的那样, stargazer虽然有用-但不幸的是以一种相当单一的,难以扩展的风格编写。 The broom package is (IMO) a nicely designed modular/object-oriented framework for extracting summary information from a large range of model types, but it's not oriented toward producing textual/tabular summaries. broom软件包 (IMO)是一个设计良好的模块化/面向对象的框架,用于从各种模型类型中提取摘要信息,但它并不针对生成文本/表格摘要。 For those who like that sort of thing, it would be great if the stargazer front end could be grafted onto a broom back end, but it would be a lot of work. 对于喜欢这种东西的人来说,如果将观stargazer前端嫁接到broom后端,那将是很棒的选择,但这将是很多工作。 In the meantime, short of hacking the internal stargazer:::.stargazer.wrap function, this method (tricking stargazer into believing that fit is actually an lm() model) sort of works: 同时,由于没有破解内部stargazer:::.stargazer.wrap函数,因此该方法(使stargazer相信fit实际上是lm()模型) 可以工作:

class(fit) <- c("glm","lm")
fit$call[1] <- quote(lm())
stargazer(fit)

It presents the coefficients and standard errors that are built into the fit object rather than the output of your posterior draws, but in this example at least those answers are extremely similar. 它提出的是内置到系数和标准误差fit的对象,而不是你的后路输出平局,但在这个例子中,至少这些问题的答案都极其相似。

If you are OK with markdown, then the generic pander S3 method usually does a pretty good job: 如果您对Markdown没问题,那么通用pander S3方法通常会做得很好:

> pander(fit, round = 4)

--------------------------------------------------------------
     &nbsp;        Estimate   Std. Error   t value   Pr(>|t|) 
----------------- ---------- ------------ --------- ----------
 **(Intercept)**    0.0864      0.0763      1.131     0.2581  

 **TreatmentB**     1.951       0.0826      23.62       0     

 **TreatmentC**     3.007       0.0802      37.49       0     

     **v1**         0.4555      0.0659      6.915       0     

     **v2**        -0.6845      0.0659     -10.39       0     
--------------------------------------------------------------

Table: Fitting generalized (gaussian/identity) linear model: Y ~ Treatment + v1 + v2

Please note that I forced the numbers to be rounded in this example, as some P values were extremely low, so the default digits and other global options would result in an extremely wide table. 请注意,由于某些P值极低,因此在此示例中我将数字四舍五入,因此默认digits和其他全局选项将导致表的宽度过大。 But there are some othe params you might want to use, eg: 但是您可能要使用一些其他参数,例如:

> pander(summary(fit), round = 4, add.significance.stars = TRUE, move.intercept = TRUE, summary = TRUE, split.cells = Inf)

----------------------------------------------------------------------
     &nbsp;        Estimate   Std. Error   t value   Pr(>|t|)         
----------------- ---------- ------------ --------- ---------- -------
 **TreatmentB**     1.951       0.0826      23.62       0       * * * 

 **TreatmentC**     3.007       0.0802      37.49       0       * * * 

     **v1**         0.4555      0.0659      6.915       0       * * * 

     **v2**        -0.6845      0.0659     -10.39       0       * * * 

 **(Intercept)**    0.0864      0.0763      1.131     0.2581          
----------------------------------------------------------------------


(Dispersion parameter for  gaussian  family taken to be  1.083267 )


-------------------- -----------------------------------
   Null deviance:     2803  on 999  degrees of freedom  

 Residual deviance:   1078  on 995  degrees of freedom  
-------------------- -----------------------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM