Skip to content

Instantly share code, notes, and snippets.

@soodoku
Last active January 22, 2025 21:11
Show Gist options
  • Save soodoku/2197f1b36dc3119ab90399374cd1fc3d to your computer and use it in GitHub Desktop.
Save soodoku/2197f1b36dc3119ab90399374cd1fc3d to your computer and use it in GitHub Desktop.
RQ Boot Results to Latex Table

RQ Boot to Latex

ModelSummary and Stargazer fail when RQ s.e. calculation fails

stargazer(m_demo_int_qr)
Error in base::backsolve(r, x, k = k, upper.tri = upper.tri, transpose = transpose,  : 
  singular matrix in 'backsolve'. First zero in diagonal [13]
modelsummary(m_demo_int_qr)
Error: `modelsummary could not extract the required information from a model of class
  "rq". The package tried a sequence of 2 helper functions to extract estimates:
  
  parameters::parameters(model)
  broom::tidy(model)
  
  To draw a table, one of these commands must return a `data.frame` with a column
  named "term". The `modelsummary` website explains how to summarize unsupported
  models or add support for new models yourself:
  https://modelsummary.com/articles/modelsummary.html
  
  These errors messages were generated during extraction:
  
  `parameters::parameters(model)` did not return a valid data.frame.
`broom::tidy(model)` did not return a valid data.frame.

If you bootstrap the s.e., you need a way to combine the results to a table.

Assume a list of model coefs gotten via commands like:

summary(m_demo_int_qr, se = "boot")$coef

And then use the code above to produce a latex table. Sample usage:

model_list <- list(
  Model1 = summary(m_demo_int_qr, se = "boot")$coef,
  Model2 = summary(m_demo_int_qr, se = "boot")$coef
)

# Pass coefficient names (optional)
coef_names <- c(
  "(Intercept)" = "Intercept",
  "women" = "Women",
  "race_labAsian" = "Asian",
  "race_labBlack" = "Black",
  "race_labHispanic" = "Hispanic",
  "race_labOther" = "Other Race",
  "educ_labCollege" = "College Education",
  "educ_labPostgrad" = "Postgraduate Education",
  "educ_labSome college" = "Some College Education",
  "agegroup_lab25-34" = "Age 25-34",
  "agegroup_lab35-49" = "Age 35-49",
  "agegroup_lab50-64" = "Age 50-64",
  "agegroup_lab65+" = "Age 65+"
)

# Generate the LaTeX table
latex_output <- generate_latex_table(model_list, coef_names)

generate_latex_table <- function(model_list, coef_names = NULL) {
library(dplyr)
library(tidyr)
library(kableExtra)
# Define a function for formatting coefficients with standard errors and significance levels
format_coef_latex <- function(coef, se, p) {
sig <- case_when(
p < 0.001 ~ "\\textsuperscript{***}",
p < 0.01 ~ "\\textsuperscript{**}",
p < 0.05 ~ "\\textsuperscript{*}",
TRUE ~ ""
)
paste0(
formatC(coef, digits = 3, format = "f"), sig, "\\newline\\small{(",
formatC(se, digits = 3, format = "f"), ")}"
)
}
# Combine model summaries into a single table
output_table <- bind_rows(
lapply(model_list, function(df) {
as.data.frame(df) %>%
mutate(Variable = row.names(df)) %>%
select(Variable, everything())
}),
.id = "Model"
)
# If coef_names is provided, join to replace Variable names
if (!is.null(coef_names)) {
coef_df <- tibble(Variable = names(coef_names), OfficialName = coef_names)
output_table <- output_table %>%
left_join(coef_df, by = "Variable") %>%
mutate(Variable = coalesce(OfficialName, Variable)) %>%
select(-OfficialName)
}
# Reshape the table and format the coefficients
output_table_formatted <- output_table %>%
rename(
Coefficient = Value,
`Std. Error` = `Std. Error`,
`p-value` = `Pr(>|t|)`
) %>%
mutate(Formatted = format_coef_latex(Coefficient, `Std. Error`, `p-value`)) %>%
select(Model, Variable, Formatted) %>%
pivot_wider(
names_from = Model,
values_from = Formatted
)
# Generate the LaTeX table
latex_table <- output_table_formatted %>%
kable(
align = c("l", rep("c", length(model_list))),
format = "latex",
escape = FALSE,
col.names = c("Variable", paste0("Model ", seq_along(model_list)))
) %>%
kable_styling(full_width = FALSE, position = "center", latex_options = c("hold_position"))
return(latex_table)
}
# Example Usage
# Pass a list of models with their summaries
model_list <- list(
Model1 = summary(m_demo_int_qr, se = "boot")$coef,
Model2 = summary(m_demo_int_qr, se = "boot")$coef
)
# Pass coefficient names (optional)
coef_names <- c(
"(Intercept)" = "Intercept",
"race_labAsian" = "Asian",
"race_labBlack" = "Black",
"race_labHispanic" = "Hispanic",
"race_labOther" = "Other Race"
)
# Generate the LaTeX table
latex_output <- generate_latex_table(model_list, coef_names)
# Print the table for integration into LaTeX
cat(latex_output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment