stargazer 学习笔记
李家翔
2019-06-24
- 使用 RMarkdown 的
child
参数,进行文档拼接。 - 这样拼接以后的笔记方便复习。
- 相关问题提交到 GitHub
1 介绍
You can use the style argument to choose a template of your choice. Economics and management scholars can thus create tables that resemble those published in the American Economic Review, in the Quarterly Journal of Economics, or in Administrative Science Quarterly. Political scientists can avail themselves of templates based on the American Political Science Review, the American Journal of Political Science, and on International Organization. For sociologists and demographers, the American Sociological Review, the American Sociological Reviews and Demography are available. www.r-bloggers.com
stargazer (占星师) 是一个广泛用于发表的制表软件包。
参考 Torres-Reyna (2014) stargazer 主要美化两方面
- 描述性统计展示
- 模型展示
并且导出到 Word 文档,方便整合。
2 描述性统计展示
library(stargazer)
desc_path <- "../output/desc.doc"
stargazer(mtcars, type = "html", out = desc_path)
Statistic | N | Mean | St. Dev. | Min | Pctl(25) | Pctl(75) | Max |
mpg | 32 | 20.091 | 6.027 | 10 | 15.4 | 22.8 | 34 |
cyl | 32 | 6.188 | 1.786 | 4 | 4 | 8 | 8 |
disp | 32 | 230.722 | 123.939 | 71 | 120.8 | 326 | 472 |
hp | 32 | 146.688 | 68.563 | 52 | 96.5 | 180 | 335 |
drat | 32 | 3.597 | 0.535 | 2.760 | 3.080 | 3.920 | 4.930 |
wt | 32 | 3.217 | 0.978 | 1.513 | 2.581 | 3.610 | 5.424 |
qsec | 32 | 17.849 | 1.787 | 14.500 | 16.892 | 18.900 | 22.900 |
vs | 32 | 0.438 | 0.504 | 0 | 0 | 1 | 1 |
am | 32 | 0.406 | 0.499 | 0 | 0 | 1 | 1 |
gear | 32 | 3.688 | 0.738 | 3 | 3 | 4 | 5 |
carb | 32 | 2.812 | 1.615 | 1 | 2 | 4 | 8 |
见展示 Word 文档
Output as html, which produce editable tables for Word documents. (Torres-Reyna 2014)
type = "html"
限定导出的 word 文档中表格已经 latex 化。
library(tidyverse)
mtcars %>%
gather() %>%
group_by(key) %>%
summarise(mean = mean(value), sd = sd(value)) %>%
stargazer(type = "html", out = desc_path, summary = FALSE)
key | mean | sd | |
1 | am | 0.40625 | 0.498990917235846 |
2 | carb | 2.8125 | 1.61519997763185 |
3 | cyl | 6.1875 | 1.78592164694654 |
4 | disp | 230.721875 | 123.938693831382 |
5 | drat | 3.5965625 | 0.534678736070971 |
6 | gear | 3.6875 | 0.737804065256947 |
7 | hp | 146.6875 | 68.5628684893206 |
8 | mpg | 20.090625 | 6.0269480520891 |
9 | qsec | 17.84875 | 1.78694323609684 |
10 | vs | 0.4375 | 0.504016128774185 |
11 | wt | 3.21725 | 0.978457442989697 |
见展示 Word 文档
library(stargazer)
stargazer(mtcars, type = "html", title="Descriptive statistics", digits=3, out = desc_path)
Statistic | N | Mean | St. Dev. | Min | Pctl(25) | Pctl(75) | Max |
mpg | 32 | 20.091 | 6.027 | 10 | 15.4 | 22.8 | 34 |
cyl | 32 | 6.188 | 1.786 | 4 | 4 | 8 | 8 |
disp | 32 | 230.722 | 123.939 | 71 | 120.8 | 326 | 472 |
hp | 32 | 146.688 | 68.563 | 52 | 96.5 | 180 | 335 |
drat | 32 | 3.597 | 0.535 | 2.760 | 3.080 | 3.920 | 4.930 |
wt | 32 | 3.217 | 0.978 | 1.513 | 2.581 | 3.610 | 5.424 |
qsec | 32 | 17.849 | 1.787 | 14.500 | 16.892 | 18.900 | 22.900 |
vs | 32 | 0.438 | 0.504 | 0 | 0 | 1 | 1 |
am | 32 | 0.406 | 0.499 | 0 | 0 | 1 | 1 |
gear | 32 | 3.688 | 0.738 | 3 | 3 | 4 | 5 |
carb | 32 | 2.812 | 1.615 | 1 | 2 | 4 | 8 |
见展示 Word 文档
title="Descriptive statistics", digits=3
加入标题和有效数字限制(论文使用3)。
library(stargazer)
stargazer(mtcars, type = "html", title="Descriptive statistics", digits=3, out = desc_path,
covariate.labels=c("Miles/(US)gallon","No. of cylinders","Displacement (cu.in.)",
"Gross horsepower","Rear axle ratio","Weight (lb/1000)",
"1/4 mile time","V/S","Transmission (0=auto, 1=manual)",
"Number of forward gears","Number of carburetors")
)
Statistic | N | Mean | St. Dev. | Min | Pctl(25) | Pctl(75) | Max |
Miles/(US)gallon | 32 | 20.091 | 6.027 | 10 | 15.4 | 22.8 | 34 |
No. of cylinders | 32 | 6.188 | 1.786 | 4 | 4 | 8 | 8 |
Displacement (cu.in.) | 32 | 230.722 | 123.939 | 71 | 120.8 | 326 | 472 |
Gross horsepower | 32 | 146.688 | 68.563 | 52 | 96.5 | 180 | 335 |
Rear axle ratio | 32 | 3.597 | 0.535 | 2.760 | 3.080 | 3.920 | 4.930 |
Weight (lb/1000) | 32 | 3.217 | 0.978 | 1.513 | 2.581 | 3.610 | 5.424 |
1/4 mile time | 32 | 17.849 | 1.787 | 14.500 | 16.892 | 18.900 | 22.900 |
V/S | 32 | 0.438 | 0.504 | 0 | 0 | 1 | 1 |
Transmission (0=auto, 1=manual) | 32 | 0.406 | 0.499 | 0 | 0 | 1 | 1 |
Number of forward gears | 32 | 3.688 | 0.738 | 3 | 3 | 4 | 5 |
Number of carburetors | 32 | 2.812 | 1.615 | 1 | 2 | 4 | 8 |
covariate.labels
加入变量名称(非字母),这样让描述性统计更加直观。
Use the option
covariate.labels
to replace variable names with variable labels. Must be in same order as in the dataset.
注意变量数和 label 数要保持一致。
3 模型展示
library(stargazer)
models_path <- "../output/models.doc"
mtcars$fast <-
as.numeric((mtcars$mpg > 20.1)) #Creating a dummy variable 1 = fast car
m1 <- lm(mpg ~ hp, data = mtcars)
m2 <- lm(mpg ~ hp + drat, data = mtcars)
m3 <- lm(mpg ~ hp + drat + factor(gear), data = mtcars)
m4 <-
glm(fast ~ hp + drat + am,
family = binomial(link = "logit"),
data = mtcars)
stargazer(
m1,
m2,
m3,
m4,
type = "html",
dep.var.labels = c("Miles/(US) gallon", "Fast car (=1)"),
covariate.labels = c(
"Gross horsepower",
"Rear axle ratio",
"Four foward gears",
"Five forward gears",
"Type of transmission (manual=1)"
),
out = models_path
)
Dependent variable: | ||||
Miles/(US) gallon | Fast car (=1) | |||
OLS | logistic | |||
(1) | (2) | (3) | (4) | |
Gross horsepower | -0.068*** | -0.052*** | -0.064*** | -0.397 |
(0.010) | (0.009) | (0.011) | (1.358) | |
Rear axle ratio | 4.698*** | 3.510* | 4.248 | |
(1.192) | (1.851) | (21.106) | ||
Four foward gears | -0.276 | |||
(2.135) | ||||
Five forward gears | 3.761* | |||
(2.161) | ||||
Type of transmission (manual=1) | 11.743 | |||
(359.486) | ||||
Constant | 30.099*** | 10.790** | 16.306** | 29.882 |
(1.634) | (5.078) | (6.429) | (85.238) | |
Observations | 32 | 32 | 32 | 32 |
R2 | 0.602 | 0.741 | 0.782 | |
Adjusted R2 | 0.589 | 0.723 | 0.749 | |
Log Likelihood | -1.953 | |||
Akaike Inf. Crit. | 11.906 | |||
Residual Std. Error | 3.863 (df = 30) | 3.170 (df = 29) | 3.017 (df = 27) | |
F Statistic | 45.460*** (df = 1; 30) | 41.522*** (df = 2; 29) | 24.179*** (df = 4; 27) | |
Note: | p<0.1; p<0.05; p<0.01 |
设定好 dep.var.labels
因变量名称(非字母),covariate.labels
参考文献
Torres-Reyna, Oscar. 2014. “Using Stargazer to Report Regression Output and Descriptive Statistics in R: For Non-Latex Users.” Princeton University DSS Lab. 2014. https://www.princeton.edu/~otorres/NiceOutputR.pdf.