R语言 stargazer 使用指南简介
rstudio吧
全部回复
仅看楼主
level 1
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 = "1px",
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 = "ht
bp
",
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|
http://www.rdaizuo.com
http://www.daixie.it.com
http://www.rcodedaixie.com
http://www.rdaima.com
7. 常见问题解决问题1:表格太宽
# 解决方案:调整字体大小和列间距
stargazer(model1, model2, model3,
type = "latex",
font.size = "footnotesize",
column.sep.width = "4px",
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的各种需求。
2025年09月19日 05点09分 1
1