R:扩展通过texreg()打印的plm()和coeftest()输出

时间:2019-06-20 14:04:39

标签: r regression plm texreg

请考虑以下固定效果回归的简单示例:

# Load packages
packs <- list("texreg", "plm", "lmtest")
lapply(packs, require, character.only = T)

# Load mtcars data set
d <- mtcars

# Run fixed effects regressions
fe_results <- lapply(c("hp", "drat", "qsec", "vs"), function(x) {
  plm_eq <- paste0("mpg ~ wt + ", x)
  plm_outp <- plm(plm_eq, index = "cyl", data = d, model = "within")
  return(plm_outp)
})

# Print via texreg
texreg(fe_results, stars = c(0.01, 0.05, 0.1), include.rsquared = F, include.rmse = F, include.adjrsq = T)

最后一行生成此表:

\begin{table}
\begin{center}
\begin{tabular}{l c c c c }
\hline
 & Model 1 & Model 2 & Model 3 & Model 4 \\
\hline
wt         & $-3.18^{***}$ & $-3.24^{***}$ & $-3.89^{***}$ & $-3.30^{***}$ \\
           & $(0.72)$      & $(0.83)$      & $(0.91)$      & $(0.78)$      \\
hp         & $-0.02^{*}$   &               &               &               \\
           & $(0.01)$      &               &               &               \\
drat       &               & $-0.14$       &               &               \\
           &               & $(1.32)$      &               &               \\
qsec       &               &               & $0.50$        &               \\
           &               &               & $(0.38)$      &               \\
vs         &               &               &               & $0.86$        \\
           &               &               &               & $(1.64)$      \\
\hline
Adj. R$^2$ & 0.39          & 0.30          & 0.34          & 0.31          \\
Num. obs.  & 32            & 32            & 32            & 32            \\
\hline
\multicolumn{5}{l}{\scriptsize{$^{***}p<0.01$, $^{**}p<0.05$, $^*p<0.1$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}

但是,我想将标准错误更改为异方差性强的标准错误。因此,我将coeftest添加到给定的函数中:

fe_results <- lapply(c("hp", "drat", "qsec", "vs"), function(x) {
  plm_eq <- paste0("mpg ~ wt + ", x)
  plm_outp <- plm(plm_eq, index = "cyl", data = d, model = "within")
  plm_outp <- coeftest(plm_outp, vcov = vcovHC(plm_outp, type = "HC1"))
  return(plm_outp)
})

不幸的是,coeftest会产生不希望有的结果,即丢弃Adj. R$^2$Num. obs.上的信息,然后还将其从texreg输出中删除:

\begin{table}
\begin{center}
\begin{tabular}{l c c c c }
\hline
 & Model 1 & Model 2 & Model 3 & Model 4 \\
\hline
wt   & $-3.18^{***}$ & $-3.24^{***}$ & $-3.89^{***}$ & $-3.30^{***}$ \\
     & $(0.95)$      & $(0.83)$      & $(1.29)$      & $(1.08)$      \\
hp   & $-0.02^{*}$   &               &               &               \\
     & $(0.01)$      &               &               &               \\
drat &               & $-0.14$       &               &               \\
     &               & $(0.72)$      &               &               \\
qsec &               &               & $0.50^{***}$  &               \\
     &               &               & $(0.10)$      &               \\
vs   &               &               &               & $0.86$        \\
     &               &               &               & $(0.63)$      \\
\hline
\multicolumn{5}{l}{\scriptsize{$^{***}p<0.01$, $^{**}p<0.05$, $^*p<0.1$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}

一种解决方案发现绕过Adj. R$^2$Num. obs.的情况是通过以下方式造成的:

fe_results <- lapply(c("hp", "drat", "qsec", "vs"), function(x) {
  plm_eq <- paste0("mpg ~ wt + ", x)
  plm_outp <- plm(plm_eq, index = "cyl", data = d, model = "within")
  plm_outp_s <- summary(plm_outp)
  plm_outp_s$coefficients <- unclass(coeftest(plm_outp, vcov = vcovHC(plm_outp, type = "HC1")))
  return(plm_outp_s)
})

缺点是返回的输出现在为summary.plm格式,无法再由texreg打印(Error in (function (classes, fdef, mtable): unable to find an inherited method for function ‘extract’ for signature ‘"summary.plm"’)。我该如何解决?我如何打印回归输出包括。 Adj. R$^2$Num. obs.和使用texreg的异方差稳健标准误差?

此外,我想以一种Stata方式添加“拦截”。我认为该解决方案可能依赖于within_intercept(),但不确定如何以将其包含在texreg输出中的方式使用它。

我试图使这个示例尽可能简单和连贯,并期待任何评论和建议。

0 个答案:

没有答案