R语言Stargazer包异常值控制引发换行问题及回归汇总咨询
It sounds like you’ve got a solid analytical workflow going—spotting outliers via boxplots, running paired regression models (with and without those outliers), and now hitting formatting snags with Stargazer. Let’s break down how to fix those annoying line break problems step by step.
First, let’s recap your workflow with complete, reproducible code to make sure we’re aligned:
# Load required packages library(stargazer) # Identify outliers for GDP growth outlier_val_GDPGrowth <- boxplot.stats(data$GDPpCGrowth1991)$out # Create dataset excluding outliers data_no_outliers <- data[!data$GDPpCGrowth1991 %in% outlier_val_GDPGrowth, ] # Run regression models # Model retaining outliers LRUnemploymentwOutliers <- lm(UnemploymentRate ~ GDPpCGrowth1991 + DevelopmentLevel + OtherControlVars, data = data) # Model excluding outliers LRUnemploymentwoOutliers <- lm(UnemploymentRate ~ GDPpCGrowth1991 + DevelopmentLevel + OtherControlVars, data = data_no_outliers)
Now, let’s tackle the line break issues you’re seeing with Stargazer. These almost always stem from either overly long variable/model names, or the table exceeding the width of your output format (text/LaTeX/HTML). Here’s how to fix both scenarios:
1. Fix Line Breaks from Long Variable/Model Names
If your variable labels or model titles are wrapping awkwardly, use Stargazer’s built-in arguments to customize them into concise, clean labels:
stargazer(LRUnemploymentwOutliers, LRUnemploymentwoOutliers, # Shorten model names to avoid wrapping model.names = c("With Outliers", "Without Outliers"), # Replace long variable names with readable labels covariate.labels = c("GDP Growth (1991)", "Development Level", "Other Controls"), # Rename the dependent variable for clarity dep.var.labels = "Unemployment Rate", # Adjust column spacing to prevent unwanted breaks column.sep.width = "10pt", type = "text") # Swap to "latex" or "html" based on your output needs
2. Fix Line Breaks from Overly Wide Tables
If the entire table is wrapping because it’s too wide for your output medium, tweak formatting parameters to compress or resize it:
For Plain Text Output:
stargazer(LRUnemploymentwOutliers, LRUnemploymentwoOutliers, font.size = "small", single.row = TRUE, # Puts coefficients and standard errors in one row to save space type = "text")
For LaTeX Output (e.g., for academic papers):
stargazer(LRUnemploymentwOutliers, LRUnemploymentwoOutliers, font.size = "footnotesize", width = "0.8\\textwidth", # Shrink table to 80% of page width type = "latex")
For HTML Output (e.g., for R Markdown web docs):
stargazer(LRUnemploymentwOutliers, LRUnemploymentwoOutliers, style = "html", out = "regression_results.html", width = "80%", # Set table width to 80% of the page type = "html")
3. Quick Pre-emptive Tip
Avoid most line break headaches upfront by renaming long variables in your dataset before running regressions. For example, use GDP_growth_91 instead of GDPpCGrowth1991—short, clear variable names make Stargazer formatting far smoother.
内容的提问来源于stack exchange,提问作者JPR

