如何在stargazer中翻转回归表格?flip参数无效求助
flip = TRUE Isn't Working for Your Stargazer Regression Tables Got it, let's break down why your flip parameter isn't transposing the regression table in stargazer 5.2.1, and how to fix it.
Core Problem: Version Limitation
Even though the latest stargazer documentation states that flip now supports regression tables, this feature was not included in version 5.2.1. It was added in later releases (starting with version 5.2.2). So your current install doesn't have the code to handle flip for regression outputs—hence you're still getting the regular table layout.
Solutions
1. Upgrade Stargazer to the Latest Version
This is the most straightforward fix. Run these commands to update the package:
# Install devtools if you don't have it already install.packages("devtools") # Install the latest version from GitHub (CRAN may also have the updated version now) devtools::install_github("cran/stargazer")
Once upgraded, re-run your original code, and flip = TRUE will transpose the regression table as expected.
2. Workaround for Stargazer 5.2.1 (If You Can't Upgrade)
If you're stuck on 5.2.1, you can manually extract regression results into a data frame, then use stargazer's summary statistics mode (which does support flip):
library(stargazer) library(dplyr) # Your original model setup linear.1 <- lm(rating ~ complaints + privileges + learning + raises + critical, data=attitude) linear.2 <- lm(rating ~ complaints + privileges + learning, data=attitude) attitude$high.rating <- (attitude$rating > 70) probit.model <- glm(high.rating ~ learning + critical + advance, data=attitude, family = binomial(link = "probit")) # Function to extract formatted coefficients + significance stars extract_formatted_results <- function(model) { coefs <- round(coef(model), 3) p_vals <- summary(model)$coefficients[, 4] # Add significance stars stars <- case_when( p_vals < 0.001 ~ "***", p_vals < 0.01 ~ "**", p_vals < 0.05 ~ "*", TRUE ~ "" ) paste0(coefs, stars) } # Build a data frame of results results_df <- data.frame( Model_1 = extract_formatted_results(linear.1), Model_2 = extract_formatted_results(linear.2), Model_3 = extract_formatted_results(probit.model) ) # Set row names to variable names rownames(results_df) <- names(coef(linear.1)) # Generate transposed table with stargazer's summary mode stargazer(results_df, title = "Regression Results", type = "text", flip = TRUE)
Alternatively, use the texreg package (which has supported transposed regression tables for longer):
install.packages("texreg") library(texreg) texreg(list(linear.1, linear.2, probit.model), transpose = TRUE, type = "text")
内容的提问来源于stack exchange,提问作者鈩暿樖樶竼岣结笜

