I've got results from a linear regression model with a factor variable in R that I would like pretty up and then output into LaTeX. Ideally the factor variable would be presented in the table via a row that gives the name of the variable and the reference category but is otherwise blank and then rows with indented text below that give the levels of the factor together with the corresponding estimates.
I've long used the stargazer
package to get regression results from R into LaTeX but see no way of achieving the result I want with it. An example:
library(ggplot2)
library(stargazer)
levels(diamonds$cut)
options(contrasts = c("contr.treatment", "contr.treatment"))
model1 <- lm(price~cut,data=diamonds)
stargazer(model1,type='text')
This yields the default output:
===============================================
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
Here's what I want:
===============================================
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
Is there any way to achieve this in stargazer
without too much hackery? Are there other packages in which this would be simpler to do?
Thus, each of the groups is defined by having a one of the dummy variables equal to one except of one group which is all zero's. The group with all zeros is known as the reference group, which in our example is group 4. We will see exactly what this means after we look at the regression analysis results.
What are reference levels. The reference level of a categorical predictor variable is often considered the “baseline” or “usual” value that is observed for the given variable. In the process of dummy coding, the variable for the reference level is left out since it would simply contain “0” for every observation.
For each of the categorical predictor variables (Gender and Race), the first category (0) is identified as the REFERENCE category; this will be explained in further detail in the Results section.
Not entirely what you wanted, but you're able to manually specify covariate labels via the covariate.labels argument. I haven't been able to find out how you could add a header, though, requiring you to manually add the linebreak.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With