如何在观星者中翻转回归表?

时间:2018-05-15 01:17:29

标签: r xtable stargazer

我想“翻转”回归表中的行/列。

最新的stargazer手册说现在flip参数也适用于回归表(之前它只适用于汇总统计数据)。

然而,我无法使其发挥作用。以下是stargazer 5.2.1

的示例
library(stargazer)
stargazer(attitude)

linear.1 <- lm(rating ~ complaints + privileges + learning + raises + critical, data=attitude)
linear.2 <- lm(rating ~ complaints + privileges + learning, data=attitude)

## create an indicator dependent variable, and run a probit model

attitude$high.rating <- (attitude$rating > 70)
probit.model <- glm(high.rating ~ learning + critical + advance, data=attitude, family = binomial(link = "probit"))

stargazer(linear.1, linear.2, probit.model, title= "Regression Results", type = 'text', flip = TRUE)

给出:

> stargazer(linear.1, linear.2, probit.model, title="Regression Results", type = 'text', flip = FALSE)
the condition has length > 1 and only the first element will be usednumber of rows of result is not a multiple of vector length (arg 2)number of rows of result is not a multiple of vector length (arg 2)
Regression Results
=============================================================================
                                       Dependent variable:                   
                    ---------------------------------------------------------
                                       rating                     high.rating
                                         OLS                        probit   
                             (1)                    (2)               (3)    
-----------------------------------------------------------------------------
complaints                 0.692***               0.682***                   
                           (0.149)                (0.129)                    

privileges                  -0.104                 -0.103                    
                           (0.135)                (0.129)                    

learning                    0.249                  0.238*          0.164***  
                           (0.160)                (0.139)           (0.053)  

raises                      -0.033                                       

该表只是将变量作为列的常规表。 相反,我正在寻找像

这样的东西
Regression Results
=====================================
            complaints   privileges
-------------------------------------
model1      0.692***      etc
            (0.149) 
model2      0.14**    
            (0.049)
model3      0.692    
            (0.149)

我错过了什么吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

你去,我的朋友。我希望这会有所帮助。

stargazer(coef(summary(linear.1)), coef(summary(linear.2)), coef(summary(probit.model)), title= "Regression Results", type = 'text', flip = TRUE)

<强>输出

Regression Results
======================================================================
            (Intercept) complaints privileges learning raises critical
----------------------------------------------------------------------
Estimate      11.011      0.692      -0.104    0.249   -0.033  0.015  
Std. Error    11.704      0.149      0.135     0.160   0.202   0.147  
t value        0.941      4.649      -0.769    1.560   -0.165  0.105  
Pr(> | t| )    0.356      0.0001     0.450     0.132   0.870   0.917  
----------------------------------------------------------------------

Regression Results
======================================================
            (Intercept) complaints privileges learning
------------------------------------------------------
Estimate      11.258      0.682      -0.103    0.238  
Std. Error     7.318      0.129      0.129     0.139  
t value        1.538      5.296      -0.799    1.707  
Pr(> | t| )    0.136     0.00002     0.432     0.100  
------------------------------------------------------

Regression Results
=================================================
            (Intercept) learning critical advance
-------------------------------------------------
Estimate      -7.476     0.164    -0.001  -0.062 
Std. Error     3.570     0.053    0.044    0.042 
z value       -2.094     3.079    -0.013  -1.472 
Pr(> | z| )    0.036     0.002    0.990    0.141 
-------------------------------------------------
> 
OP澄清后

编辑

    options(stringsAsFactors = F, scipen=999)
    model1 <- data.frame(t(coef(summary(linear.1))))
model1$stats <- rownames(model1)
model2 <- data.frame(t(coef(summary(linear.2))))
model2$stats <- rownames(model2)
model3 <- data.frame(t(coef(summary(probit.model))))
model3$stats <- rownames(model3)


x <- dplyr::bind_rows(model1 = model1, model2=model2,
                 model3 = model3, .id = "Name")
x <- x[,c(1,8,2:7,9 )] #rearranging the terms
stargazer(x, type="text", summary=F)

<强>输出

    =========================================================================================
    Name     stats    X.Intercept. complaints privileges learning raises critical advance
-----------------------------------------------------------------------------------------
1  model1  Estimate      11.011      0.692      -0.104    0.249   -0.033  0.015          
2  model1 Std. Error     11.704      0.149      0.135     0.160   0.202   0.147          
3  model1   t value      0.941       4.649      -0.769    1.560   -0.165  0.105          
4  model1 Pr(> | t| )    0.356       0.0001     0.450     0.132   0.870   0.917          
5  model2  Estimate      11.258      0.682      -0.103    0.238                          
6  model2 Std. Error     7.318       0.129      0.129     0.139                          
7  model2   t value      1.538       5.296      -0.799    1.707                          
8  model2 Pr(> | t| )    0.136      0.00002     0.432     0.100                          
9  model3  Estimate      -7.476                           0.164           -0.001  -0.062 
10 model3 Std. Error     3.570                            0.053           0.044    0.042 
11 model3   z value      -2.094                           3.079           -0.013  -1.472 
12 model3 Pr(> | z| )    0.036                            0.002           0.990    0.141 
-----------------------------------------------------------------------------------------