R语言stargazer 使用指南
简介
stargazer是R语言中最受欢迎的统计表格生成包,广泛用于学术论文、研究报告和数据分析。它能够生成高质量的描述性统计表、回归结果表和数据框表格。
安装和加载
# 安装install.packages("stargazer")# 加载library(stargazer)# 同时加载常用包library(dplyr)library(ggplot2)
1. 描述性统计表
基本描述性统计
# 使用内置数据集data(mtcars)# 基本描述性统计stargazer(mtcars, type = "text")# 选择特定变量stargazer(mtcars[c("mpg", "wt", "hp")], type = "text")# 自定义标题和变量名stargazer(mtcars[c("mpg", "wt", "hp")], type = "text", title = "汽车数据描述性统计", covariate.labels = c("油耗", "重量", "马力"))
高级描述性统计选项
# 控制统计量显示stargazer(mtcars, type = "text", summary.stat = c("n", "mean", "sd", "min", "max"), digits = 2)# 按组描述统计stargazer(mtcars, type = "text", summary.logical = FALSE, # 不显示逻辑变量统计 nobs = FALSE, # 不显示观测数 mean.sd = TRUE) # 显示均值和标准差
2. 回归结果表
单个回归模型
# 线性回归model1 <- lm(mpg ~wt +hp, data = mtcars)# 基本输出stargazer(model1, type = "text")# 自定义输出stargazer(model1, type = "text", title = "线性回归结果", dep.var.labels = "油耗(mpg)", covariate.labels = c("重量", "马力", "截距项"))
多模型对比
# 创建多个模型model1 <- lm(mpg ~wt, data = mtcars)model2 <- lm(mpg ~wt +hp, data = mtcars)model3 <- lm(mpg ~wt +hp +cyl, data = mtcars)# 模型对比表stargazer(model1, model2, model3, type = "text", title = "回归模型对比", column.labels = c("模型1", "模型2", "模型3"), dep.var.labels = "油耗(mpg)", covariate.labels = c("重量", "马力", "气缸数"))
不同类型的回归模型
# 逻辑回归mtcars$high_mpg <- ifelse(mtcars$mpg > median(mtcars$mpg), 1, 0)logit_model <- glm(high_mpg ~wt +hp, data =mtcars, family = binomial)# 泊松回归poisson_model <- glm(cyl ~wt +hp, data =mtcars, family = poisson)# 混合输出stargazer(model1, logit_model, poisson_model, type = "text", title = "不同回归模型对比", column.labels = c("线性回归", "逻辑回归", "泊松回归"))
3. 输出格式控制
HTML输出
# 基本HTML输出stargazer(model1, type = "html", out = "regression_results.html")# 自定义HTML样式stargazer(model1, type = "html", out = "styled_results.html", title = "回归分析结果", style = "aer", # 使用AER期刊样式 table.layout = "=c#-t-a-s-n") # 自定义表格布局
LaTeX输出
# LaTeX输出stargazer(model1, type = "latex", out = "results.tex", title = "回归分析结果", label = "tab:regression", table.placement = "H")# 适合双栏期刊的紧凑格式stargazer(model1, model2, type = "latex", font.size = "small", column.sep.width = "1pt", no.space = TRUE)
4. 高级自定义选项
统计量控制
stargazer(model1, model2, type = "text", # 保留的统计量 keep.stat = c("n", "rsq", "adj.rsq"), # 或者省略的统计量 omit.stat = c("f", "ser"), # 自定义统计量标签 add.lines = list(c("控制变量", "否", "是"), c("固定效应", "否", "是")))
系数显示控制
stargazer(model1, model2, type = "text", # 保留特定变量 keep = c("wt", "hp"), # 或省略特定变量 omit = c("cyl"), # 变量顺序 order = c("hp", "wt"), # 小数位数 digits = 3, # 显著性星号 star.cutoffs = c(0.05, 0.01, 0.001), star.char = c("*", "**", "***"))
表格外观
stargazer(model1, model2, type = "text", # 表格标题 title = "主要回归结果", # 列标题 column.labels = c("基础模型", "完整模型"), # 因变量标签 dep.var.labels = "油耗表现", # 自变量标签 covariate.labels = c("车重", "马力"), # 表格注释 notes = c("注:括号内为标准误", "*** p<0.001, ** p<0.01, * p<0.05"), notes.align = "l")
5. 数据框表格
基本数据框输出
# 创建汇总数据summary_data <-mtcars %>% group_by(cyl) %>% summarise(平均油耗 = round(mean(mpg), 2),平均重量 = round(mean(wt), 2),观测数 = n() )# 输出为表格stargazer(summary_data, type = "text", summary = FALSE, title = "按气缸数分组的汇总统计")
自定义数据框表格
stargazer(summary_data, type = "html", summary = FALSE, title = "汽车数据分组统计", digits = 2, align = TRUE, out = "summary_table.html")
6. 实用技巧和最佳实践
批量输出多种格式
# 定义输出函数output_table <- function(models, filename) { # 文本格式(用于查看) stargazer(models, type = "text") # HTML格式(用于网页) stargazer(models, type = "html", out = paste0(filename, ".html")) # LaTeX格式(用于论文) stargazer(models, type = "latex", out = paste0(filename, ".tex"))}# 使用函数output_table(list(model1, model2), "regression_results")
处理中文编码
# 确保中文正确显示stargazer(model1, type = "html", out = "results_chinese.html", title = "回归分析结果", dep.var.labels = "油耗", covariate.labels = c("重量", "马力"), notes = "数据来源:R内置mtcars数据集", # 设置编码 flip = FALSE, style = "default")
学术论文模板
# 适合学术期刊的标准格式stargazer(model1, model2, model3, type = "latex", title = "主要回归结果", label = "tab:main_results", dep.var.labels = "因变量:油耗", column.labels = c("(1)", "(2)", "(3)"), covariate.labels = c("重量", "马力", "气缸数"), omit.stat = c("f", "ser"), digits = 3, star.cutoffs = c(0.05, 0.01, 0.001), notes = "注:括号内为稳健标准误。*** p$<$0.001, ** p$<$0.01, * p$<$0.05", notes.align = "l", table.placement = "htbp", out = "main_results.tex")
专业R语言辅导| Python编程| 数据分析Data analysis | 统计分析Statistics | 数据挖掘Data mining | 机器学习Machine learning | |统计分析 Statistics|STATS 202|STATS 203|STAT 110|STAT 104|STAT 705|STAT 707|STAT4203|STAT4204|STAT4205|STAT4206|STAT 133|STAT 134|STAT 101A|STAT 100A|STAT 581|STAT 520|STAT 521|STAT 4500|STAT 5805|STAT 5806|STAT 4600|STAT30001|STAT3001|STAT3002|STAT3003|STAT3004|STAT3005|STAT3006|STAT5001|STAT5002|STAT5003|STAT5004|
www.rdaizuo.com
www.daixie.it.com
www.rcodedaixie.com
www.rdaima.com
7. 常见问题解决
问题1:表格太宽
# 解决方案:调整字体大小和列间距stargazer(model1, model2, model3, type = "latex", font.size = "footnotesize", column.sep.width = "3pt", no.space = TRUE)
问题2:变量名太长
# 解决方案:使用简短的标签stargazer(model1, type = "text", covariate.labels = c("重量", "马力"), # 简化变量名 dep.var.labels = "油耗")
问题3:需要添加额外信息
# 解决方案:使用add.lines参数stargazer(model1, model2, type = "text", add.lines = list( c("样本期间", "1974", "1974"), c("控制变量", "否", "是"), c("R方调整", "是", "是") ))
8. 快速参考表
参数类别参数名说明常用值
输出控制type输出格式“text”, “html”, “latex”
out输出文件“results.html”
内容控制title表格标题“回归结果”
dep.var.labels因变量标签“油耗”
covariate.labels自变量标签c(“重量”, “马力”)
column.labels列标签c(“模型1”, “模型2”)
统计量omit.stat省略统计量c(“f”, “ser”)
keep.stat保留统计量c(“n”, “rsq”)
digits小数位数3
格式align对齐方式TRUE
font.size字体大小“small”
no.space紧凑格式TRUE
这份详细指南应该能够满足您在学术写作和数据分析中使用stargazer的各种需求。