如何将多回归分析结果按指定格式导出至Excel?
Great question! There are a few solid ways to get your regression table (with significance stars intact) into Excel while preserving the clean format you like. Here are the most straightforward methods:
1. Use the modelsummary Package (Simplest & Most Flexible)
The modelsummary package is built exactly for this kind of task—it generates publication-ready tables and supports direct Excel export out of the box.
First, install the package if you haven't already:
install.packages("modelsummary")
Then run this code to get your desired table in Excel:
library(modelsummary) # Your existing model code data("mtcars") m1 <- lm(hp ~ disp, data = mtcars) m2 <- lm(hp ~ disp + wt, data = mtcars) # Generate table and export directly to Excel modelsummary( list(m1, m2), output = "regression_results.xlsx", stars = TRUE, # Adds significance stars based on p-values statistic = "({std.error})", # Shows standard errors in parentheses below coefficients gof_map = c("r.squared", "adj.r.squared", "nobs", "rmse") # Include the exact stats you want )
This creates an Excel file with a table that matches your desired format perfectly—coefficients with stars, standard errors below them, and all the goodness-of-fit metrics you need.
2. Use texreg to Export HTML, Then Convert to Excel
If you prefer sticking with texreg, you can export the table to HTML first, which Excel can read directly (or convert it to a data frame in R first):
library(texreg) library(openxlsx) # For writing Excel files # Your models data("mtcars") m1 <- lm(hp ~ disp, data = mtcars) m2 <- lm(hp ~ disp + wt, data = mtcars) # Export to HTML htmlreg(list(m1, m2), file = "reg_table.html", include.ci = FALSE) # Option 1: Open the HTML file directly in Excel (it will auto-convert to a formatted table) # Option 2: Read the HTML table into R and export to Excel library(rvest) table_df <- read_html("reg_table.html") %>% html_table(fill = TRUE) %>% .[[1]] write_xlsx(table_df, "regression_results.xlsx")
3. Manually Build a Data Frame (For Full Control)
If you want complete control over every aspect of the table, you can extract components from your models and build a data frame manually before exporting:
library(texreg) library(openxlsx) library(dplyr) data("mtcars") m1 <- lm(hp ~ disp, data = mtcars) m2 <- lm(hp ~ disp + wt, data = mtcars) # Extract model components ext1 <- extract(m1) ext2 <- extract(m2) # Helper function to add significance stars add_stars <- function(coef, pval) { stars <- case_when( pval < 0.001 ~ "***", pval < 0.01 ~ "**", pval < 0.05 ~ "*", TRUE ~ "" ) paste0(round(coef, 2), " ", stars) } # Build coefficient rows coef_df <- data.frame( Term = c("(Intercept)", "disp", "wt"), Model1 = c( add_stars(ext1$coef[1], ext1$coef.pvalues[1]), add_stars(ext1$coef[2], ext1$coef.pvalues[2]), "" ), Model1_SE = c(paste0("(", round(ext1$se[1], 2), ")"), paste0("(", round(ext1$se[2], 2), ")"), ""), Model2 = c( add_stars(ext2$coef[1], ext2$coef.pvalues[1]), add_stars(ext2$coef[2], ext2$coef.pvalues[2]), add_stars(ext2$coef[3], ext2$coef.pvalues[3]) ), Model2_SE = c(paste0("(", round(ext2$se[1], 2), ")"), paste0("(", round(ext2$se[2], 2), ")"), paste0("(", round(ext2$se[3], 2), ")")) ) # Build goodness-of-fit rows stats_df <- data.frame( Term = c("R^2", "Adj. R^2", "Num. obs.", "RMSE"), Model1 = c(round(ext1$rsquared, 2), round(ext1$adj.rsquared, 2), ext1$nobs, round(ext1$rmse, 2)), Model1_SE = rep("", 4), Model2 = c(round(ext2$rsquared, 2), round(ext2$adj.rsquared, 2), ext2$nobs, round(ext2$rmse, 2)), Model2_SE = rep("", 4) ) # Combine and format the table final_df <- rbind(coef_df, stats_df) %>% mutate( Model1 = ifelse(Model1_SE != "", paste0(Model1, "\n", Model1_SE), Model1), Model2 = ifelse(Model2_SE != "", paste0(Model2, "\n", Model2_SE), Model2) ) %>% select(Term, Model1, Model2) # Add significance note at the bottom note_df <- data.frame( Term = "*** p < 0.001, ** p < 0.01, * p < 0.05", Model1 = "", Model2 = "" ) final_df <- rbind(final_df, note_df) # Export to Excel write_xlsx(final_df, "regression_results.xlsx")
In Excel, enable "Wrap Text" on the columns to see standard errors below coefficients, matching your original screenreg output.
内容的提问来源于stack exchange,提问作者msh855

