You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在stargazer中翻转回归表格?flip参数无效求助

Why 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,提问作者鈩暿樖樶竼岣结笜

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:50:52