knitr::opts_chunk$set( collapse = TRUE, comment = "", message = FALSE, warning = FALSE ) library(stargazer2)
stargazer2 supports plm model objects natively. It auto-detects the
estimator type (FE, RE, Pooled OLS, FD, Between), displays fixed-effect and
random-effect indicator rows, and reports the appropriate fit statistics from
summary.plm.
The Grunfeld dataset (10 US manufacturing firms, 1935--1954, balanced panel)
is included with plm. We estimate four specifications of an investment
equation:
library(plm) data("Grunfeld", package = "plm") m_pool <- plm(inv ~ value + capital, Grunfeld, index = c("firm", "year"), model = "pooling") m_fe <- plm(inv ~ value + capital, Grunfeld, index = c("firm", "year"), model = "within") m_twfe <- plm(inv ~ value + capital, Grunfeld, index = c("firm", "year"), model = "within", effect = "twoways") m_re <- plm(inv ~ value + capital, Grunfeld, index = c("firm", "year"), model = "random")
A bare call produces a complete table. stargazer2 reads the model type from
each plm object and builds indicator rows for the fixed and random effects
present across all columns:
stargazer(m_pool, m_fe, m_twfe, m_re, type = "text")
Things to notice:
plm ships its own vcov functions. Pass them inline through the vcov
argument. vcovHC(..., method = "arellano") gives standard errors clustered
by the individual unit, the most common choice for FE models:
stargazer(m_fe, m_twfe, type = "text", vcov = list(vcovHC(m_fe, method = "arellano"), vcovHC(m_twfe, method = "arellano")))
stargazer2 auto-detects the SE type from the inline call and labels the
table note accordingly. For replication of Stata's vce(cluster id) results,
sandwich::vcovCL is preferable as it applies the G/(G−1) small-sample
correction that Stata uses; plm::vcovHC applies a heteroskedasticity-style
n/(n−k) correction instead.
For panels with cross-sectional dependence or long time dimensions,
Driscoll-Kraay (spatial HAC) standard errors are a common alternative.
plm::vcovSCC implements this estimator:
stargazer(m_fe, m_twfe, type = "text", vcov = list(vcovSCC(m_fe), vcovSCC(m_twfe)))
Once the defaults look right, labels can be added. The LaTeX source below sets a title and cross-reference label, renames the dependent variable and covariates, and assigns custom column headers:
stargazer(m_pool, m_fe, m_twfe, m_re, type = "latex", title = "Investment Equations: Grunfeld Panel Data", label = "tab:grunfeld", dep.var.labels = "Investment", covariate.labels = c("Market Value", "Capital Stock"), column.labels = c("Pooled OLS", "FE", "Two-way FE", "RE"))
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.