更改stargazer输出的系数名称

时间:2021-09-04 06:08:46

I recently received a question about the answer here about how to change the names of variables when outputting regression results using Stargazer. The answer didn't work for the lrm function from rms. Specifically, the coefficients were output correctly but the standard errors disappeared. Here's a reproducible example:

我最近收到一个关于答案的问题,关于如何在使用Stargazer输出回归结果时更改变量的名称。答案对rms的lrm函数不起作用。具体而言,系数输出正确但标准误差消失。这是一个可重复的例子:

library(rms)
library(stargazer)
stargazer(attitude)

# logit models
m1 <- lrm(rating ~ complaints + learning + privileges,x=TRUE, y=TRUE,data=attitude)
m2 <- lrm(rating ~ complaints + learning + privileges,x=TRUE, y=TRUE,data=attitude)
names(m1$coefficients)[names(m1$coefficients) == "privileges"] <- "past"
names(m2$coefficients)[names(m2$coefficients) == "privileges"] <- "past"

stargazer(m1,m2, type="text")

Any idea how to make this work?

知道如何使这项工作?

1 个解决方案

#1


1  

The answer is that the lrm class object stores not just coefficients but the variance-covariance matrix, as well, so you need to do an additional step:

答案是lrm类对象不仅存储系数,还存储方差 - 协方差矩阵,因此您需要执行额外的步骤:

rownames(m1$var)[rownames(m1$var) == "privileges"] <- "past"
rownames(m2$var)[rownames(m2$var) == "privileges"] <- "past"
colnames(m1$var)[colnames(m1$var) == "privileges"] <- "past"
colnames(m2$var)[colnames(m2$var) == "privileges"] <- "past"

This changes the rownames and colnames of the variance-covariance matrix and thus produces the correct result:

这会更改方差 - 协方差矩阵的rownames和colnames,从而生成正确的结果:

stargazer(m1,m2, type="text")

==========================================
                  Dependent variable:     
              ----------------------------
                         rating           
                   (1)            (2)     
------------------------------------------

....

complaints       0.196***      0.196***   
                 (0.045)        (0.045)   

learning          0.063          0.063    
                 (0.039)        (0.039)   

past              -0.034        -0.034    
                 (0.035)        (0.035)   

------------------------------------------
Observations        30            30      
R2                0.713          0.713    
chi2 (df = 3)   37.238***      37.238***  
==========================================
Note:          *p<0.1; **p<0.05; ***p<0.01

#1


1  

The answer is that the lrm class object stores not just coefficients but the variance-covariance matrix, as well, so you need to do an additional step:

答案是lrm类对象不仅存储系数,还存储方差 - 协方差矩阵,因此您需要执行额外的步骤:

rownames(m1$var)[rownames(m1$var) == "privileges"] <- "past"
rownames(m2$var)[rownames(m2$var) == "privileges"] <- "past"
colnames(m1$var)[colnames(m1$var) == "privileges"] <- "past"
colnames(m2$var)[colnames(m2$var) == "privileges"] <- "past"

This changes the rownames and colnames of the variance-covariance matrix and thus produces the correct result:

这会更改方差 - 协方差矩阵的rownames和colnames,从而生成正确的结果:

stargazer(m1,m2, type="text")

==========================================
                  Dependent variable:     
              ----------------------------
                         rating           
                   (1)            (2)     
------------------------------------------

....

complaints       0.196***      0.196***   
                 (0.045)        (0.045)   

learning          0.063          0.063    
                 (0.039)        (0.039)   

past              -0.034        -0.034    
                 (0.035)        (0.035)   

------------------------------------------
Observations        30            30      
R2                0.713          0.713    
chi2 (df = 3)   37.238***      37.238***  
==========================================
Note:          *p<0.1; **p<0.05; ***p<0.01