Generalized Linear Model output through texreg

为君一笑 提交于 2021-02-19 06:15:48

问题


I can use texreg to get beautiful output of glm to be used for knitr. Sometimes we need to convert the output of glm back to response using inverse link. I wonder how to get inverse link output with texreg. Something like texreg(exp(glm.D93)).

counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)
d.AD <- data.frame(treatment, outcome, counts)
glm.D93 <- glm(counts ~ outcome + treatment, family = poisson())

library(texreg)
texreg(glm.D93)

which produces

\begin{table}
\begin{center}
\begin{tabular}{l c }
\hline
               & Model 1 \\
\hline
(Intercept)    & $3.04^{***}$ \\
               & $(0.17)$     \\
outcome2       & $-0.45^{*}$  \\
               & $(0.20)$     \\
outcome3       & $-0.29$      \\
               & $(0.19)$     \\
treatment2     & $0.00$       \\
               & $(0.20)$     \\
treatment3     & $0.00$       \\
               & $(0.20)$     \\
\hline
AIC            & 56.76        \\
BIC            & 57.75        \\
Log Likelihood & -23.38       \\
Deviance       & 5.13         \\
Num. obs.      & 9            \\
\hline
\multicolumn{2}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}

But texreg(exp(glm.D93)) say

Error in exp(glm.D93) : non-numeric argument to mathematical function

Edited

glm uses some link function and provides the coefficients, standard errors and confidence intervals on link scale. But sometimes we also need coefficients, standard errors and confidence intervals on the response scale. texreg directly provides coefficients, standard errors and confidence intervals on link scale, I wonder if it is possible to get coefficients, standard errors and confidence intervals on response scale directly.

I found a way to do this with stargazer but still the standard errors and confidence intervals are not the correct one. Looking the solution to this one.

library(stargazer)

stargazer(glm.D93, coef=list(exp(glm.D93$coefficients)), type="text")

=============================================
                      Dependent variable:    
                  ---------------------------
                            counts           
---------------------------------------------
outcome2                   0.635***          
                            (0.202)          

outcome3                   0.746***          
                            (0.193)          

treatment2                 1.000***          
                            (0.200)          

treatment3                 1.000***          
                            (0.200)          

Constant                   21.000***         
                            (0.171)          

---------------------------------------------
Observations                   9             
Log Likelihood              -23.381          
Akaike Inf. Crit.           56.761           
=============================================
Note:             *p<0.1; **p<0.05; ***p<0.01

回答1:


Either use override arguments to accomplish this or manipulate an intermediate texreg object:

# solution 1
tr <- extract(glm.D93)
texreg(glm.D93, override.coef = exp(tr@coef), override.se = exp(tr@se))

# solution 2
tr <- extract(glm.D93)
tr@coef <- exp(tr@coef)
tr@se <- exp(tr@se)
texreg(tr)

Or extract the values directly from the model object or its summary (if you don't want to use texreg's extract function) and hand over the exponentiated values to the override arguments.

Any of these solutions produces the following output (in conjunction with screenreg):

==========================
                Model 1   
--------------------------
(Intercept)      21.00 ***
                 (1.19)   
outcome2          0.63 *  
                 (1.22)   
outcome3          0.75    
                 (1.21)   
treatment2        1.00    
                 (1.22)   
treatment3        1.00    
                 (1.22)   
--------------------------
AIC              56.76    
BIC              57.75    
Log Likelihood  -23.38    
Deviance          5.13    
Num. obs.         9       
==========================
*** p < 0.001, ** p < 0.01, * p < 0.05


来源:https://stackoverflow.com/questions/31459714/generalized-linear-model-output-through-texreg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!