我得到了一个线性回归模型的结果,在R中有一个因子变量,我想要很好然后输出到LaTeX。理想情况下,因子变量将通过一个行显示在表中,该行给出变量的名称和参考类别,否则为空白,然后在下面带有缩进文本的行给出因子的级别以及相应的估计值。
我长期使用stargazer
软件包将R的回归结果导入LaTeX,但看不到实现我想要的结果。一个例子:
library(ggplot2)
library(stargazer)
levels(diamonds$cut)
options(contrasts = c("contr.treatment", "contr.treatment"))
model1 <- lm(price~cut,data=diamonds)
stargazer(model1,type='text')
这会产生默认输出:
===============================================
Dependent variable:
---------------------------
price
-----------------------------------------------
cutGood -429.893***
(113.849)
cutVery Good -376.998***
(105.164)
cutPremium 225.500**
(104.395)
cutIdeal -901.216***
(102.412)
Constant 4,358.758***
(98.788)
-----------------------------------------------
Observations 53,940
R2 0.013
Adjusted R2 0.013
Residual Std. Error 3,963.847 (df = 53935)
F Statistic 175.689*** (df = 4; 53935)
===============================================
Note: *p<0.1; **p<0.05; ***p<0.01
这就是我想要的:
===============================================
Dependent variable:
---------------------------
price
-----------------------------------------------
Cut (Reference: Fair)
Good -429.893***
(113.849)
Very Good -376.998***
(105.164)
Premium 225.500**
(104.395)
Ideal -901.216***
(102.412)
Constant 4,358.758***
(98.788)
-----------------------------------------------
Observations 53,940
R2 0.013
Adjusted R2 0.013
Residual Std. Error 3,963.847 (df = 53935)
F Statistic 175.689*** (df = 4; 53935)
===============================================
Note: *p<0.1; **p<0.05; ***p<0.01
有没有办法在没有太多hackery的stargazer
中实现这一点?还有其他包可以更简单吗?
答案 0 :(得分:2)
不完全是您想要的,但您可以通过covariate.labels参数手动指定协变量标签。我无法找到如何添加标题,但要求您手动添加换行符。
stargazer(model1,type='text',
covariate.labels=c("Cut (Reference: Fair) Good",
". Very good",
". Premium",
". Ideal"))
======================================================
Dependent variable:
---------------------------
price
------------------------------------------------------
Cut (Reference: Fair) Good -429.893***
(113.849)
. Very good -376.998***
(105.164)
. Premium 225.500**
(104.395)
. Ideal -901.216***
(102.412)
Constant 4,358.758***
(98.788)
------------------------------------------------------
Observations 53,940
R2 0.013
Adjusted R2 0.013
Residual Std. Error 3,963.847 (df = 53935)
F Statistic 175.689*** (df = 4; 53935)
======================================================
Note: *p<0.1; **p<0.05; ***p<0.01
答案 1 :(得分:2)
这合理地接近了ASCII输出所需的值。是否在Latex中成功将要求您进行测试。处理\n
可能不会产生相同的副作用。
stargazer(model1,type='text', column.labels="\nCut (Reference: Fair)",
covariate.labels=c(". Good",
". Very good",
". Premium",
". Ideal"))
控制台:
=================================================
Dependent variable:
---------------------------
price
Cut (Reference: Fair)
-------------------------------------------------
. Good -429.893***
(113.849)
. Very good -376.998***
(105.164)
. Premium 225.500**
(104.395)
. Ideal -901.216***
(102.412)
Constant 4,358.758***
(98.788)
-------------------------------------------------
Observations 53,940
R2 0.013
Adjusted R2 0.013
Residual Std. Error 3,963.847 (df = 53935)
F Statistic 175.689*** (df = 4; 53935)
=================================================
Note: *p<0.1; **p<0.05; ***p<0.01