如何从R中的zeroinfl()获得出版物质量输出?

时间:2016-03-02 20:12:03

标签: r statistics output summary

我正在对我的数据运行零膨胀泊松回归。我想在一个整洁的表格中保存回归的摘要()。我尝试了write.table()和其他变体。大多数错误都说它无法解释摘要输出。

以下是我想要做的一个例子。我已经标记了不起作用的部件。数据和正在运行的zeroinfl()示例来自this site。如何以简洁的格式保存回归模型的摘要?

# This example comes directly from http://statistics.ats.ucla.edu/stat/r/dae/zipoisson.html

zinb <- read.csv("http://www.ats.ucla.edu/stat/data/fish.csv")
zinb <- within(zinb, {
   nofish <- factor(nofish)
   livebait <- factor(livebait)
    camper <- factor(camper)
 })

summary(m1 <- zeroinfl(count ~ child + camper | persons, data = zinb))

#my code for writing the output
write.table(x=summary(m1), file="summary_m1.csv") # this doesn't work
htmlreg(summary(m1), digits=3, file="summary_m1.html") # this doesn't work

2 个答案:

答案 0 :(得分:2)

require(pscl)
summary(m1 <- zeroinfl(count ~ child + camper | persons, data = zinb))

Call:
zeroinfl(formula = count ~ child + camper | persons, data = zinb)

Pearson residuals:
    Min      1Q  Median      3Q     Max 
-1.2369 -0.7540 -0.6080 -0.1921 24.0847 

Count model coefficients (poisson with log link):
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)  1.59789    0.08554  18.680   <2e-16 ***
child       -1.04284    0.09999 -10.430   <2e-16 ***
camper1      0.83402    0.09363   8.908   <2e-16 ***

Zero-inflation model coefficients (binomial with logit link):
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)   1.2974     0.3739   3.470 0.000520 ***
persons      -0.5643     0.1630  -3.463 0.000534 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 

Number of iterations in BFGS optimization: 12 
Log-likelihood: -1032 on 5 Df

应用于该模型对象的“stargazer”的结果可以传递到Latex&#34;容器&#34;:

> stargazer(m1)   # Latex is default but type="html" is also possible
# returns ------

% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Wed, Mar 02, 2016 - 12:37:08
\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}}lc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & \multicolumn{1}{c}{\textit{Dependent variable:}} \\ 
\cline{2-2} 
\\[-1.8ex] & count \\ 
\hline \\[-1.8ex] 
 child & $-$1.043$^{***}$ \\ 
  & (0.100) \\ 
  & \\ 
 camper1 & 0.834$^{***}$ \\ 
  & (0.094) \\ 
  & \\ 
 Constant & 1.598$^{***}$ \\ 
  & (0.086) \\ 
  & \\ 
\hline \\[-1.8ex] 
Observations & 250 \\ 
Log Likelihood & $-$1,031.608 \\ 
\hline 
\hline \\[-1.8ex] 
\textit{Note:}  & \multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
\end{tabular} 
\end{table} 

在MacTex中成功的Latex代码:

\documentclass{article}
\usepackage{tikz}
\usepackage{verbatim}

\begin{document}
\pagestyle{empty}
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Wed, Mar 02, 2016 - 12:39:08
\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}}lc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & \multicolumn{1}{c}{\textit{Dependent variable:}} \\ 
\cline{2-2} 
\\[-1.8ex] & count \\ 
\hline \\[-1.8ex] 
 child & $-$1.043$^{***}$ \\ 
  & (0.100) \\ 
  & \\ 
 camper1 & 0.834$^{***}$ \\ 
  & (0.094) \\ 
  & \\ 
 Constant & 1.598$^{***}$ \\ 
  & (0.086) \\ 
  & \\ 
\hline \\[-1.8ex] 
Observations & 250 \\ 
Log Likelihood & $-$1,031.608 \\ 
\hline 
\hline \\[-1.8ex] 
\textit{Note:}  & \multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
\end{tabular} 
\end{table} 

\end{document}

enter image description here

答案 1 :(得分:0)

zeroinfl对象包含两个模型,一个具有泊松回归系数,另一个具有logit系数。 stargazer现在使用zero.component选项处理零膨胀对象。使用相同的UCLA代码:

library(pscl)
zinb <- read.csv("https://stats.idre.ucla.edu/stat/data/fish.csv")
zinb <- within(zinb, {
    nofish <- factor(nofish)
    livebait <- factor(livebait)
    camper <- factor(camper)
})

m1 <- zeroinfl(count ~ child + camper | persons, data = zinb)

# return count / Poisson model
stargazer(m1, zero.component = FALSE, type = 'text')

==========================================
                   Dependent variable:    
               ---------------------------
                          count           
------------------------------------------
child                   -1.043***         
                         (0.100)          

camper1                 0.834***          
                         (0.094)          

Constant                1.598***          
                         (0.086)          

------------------------------------------
Observations               250            
Log Likelihood         -1,031.608         
==========================================
Note:          *p<0.1; **p<0.05; ***p<0.01

# return zero / logit model
stargazer(m1, zero.component = TRUE, type = 'text')

==========================================
                   Dependent variable:    
               ---------------------------
                          count           
------------------------------------------
persons                 -0.564***         
                         (0.163)          

Constant                1.297***          
                         (0.374)          

------------------------------------------
Observations               250            
Log Likelihood         -1,031.608         
==========================================
Note:          *p<0.1; **p<0.05; ***p<0.01