library(tidyverse) # for data wrangling
library(alr4) # for the data sets #
library(GGally)
library(ggpmisc)
library(parameters)
library(performance)
library(see)
library(car)
library(broom)
library(modelsummary)
library(texreg)
library(report)
library(wooldridge)
ggplot2::theme_set(ggplot2::theme_bw())
knitr::opts_chunk$set(
fig.width = 12,
fig.asp = 0.618,
fig.retina = 3,
dpi = 300,
out.width = "100%",
message = FALSE,
echo = TRUE,
cache = TRUE
)
arrange_diagnostics <-
function(fit_obj_list,
...,
which = c(1, 3),
cols = NULL) {
if (class(fit_obj_list) == "lm") fit_obj_list <- list(fit_obj_list)
if (length(rlang::dots_list(...)) > 0) fit_obj_list <- fit_obj_list %>% append(rlang::dots_list(...))
if (is.null(cols)) cols = ceiling(length(fit_obj_list) * length(which) / 2)
rows = ceiling(length(fit_obj_list) * length(which) / cols)
if (length(which) > 1) {
par(mfcol = c(rows, cols))
} else {
par(mfrow = c(rows, cols))
}
fit_obj_list %>%
walk(~ {
fitt <- .x
which %>%
walk(~ {
plot(fitt, which = .x, main = as.character(fitt$call)[2])
})
})
par(mfrow = c(1, 1))
}
my_gof <- function(fit_obj, digits = 4) {
sum_fit <- summary(fit_obj)
stars <-
pf(sum_fit$fstatistic[1],
sum_fit$fstatistic[2],
sum_fit$fstatistic[3],
lower.tail=FALSE) %>%
symnum(corr = FALSE, na = FALSE,
cutpoints = c(0, .001,.01,.05, 1),
symbols = c("***","**","*"," ")) %>%
as.character()
list(
# `R^2` = sum_fit$r.squared %>% round(digits),
# `Adj. R^2` = sum_fit$adj.r.squared %>% round(digits),
# `Num. obs.` = sum_fit$residuals %>% length(),
`Num. df` = sum_fit$df[[2]],
`F statistic` = str_c(sum_fit$fstatistic[1] %>% round(digits), " ", stars)
)
}
screen_many_regs <-
function(fit_obj_list, ..., digits = 4, single.row = TRUE) {
if (class(fit_obj_list) == "lm") fit_obj_list <- list(fit_obj_list)
if (length(rlang::dots_list(...)) > 0) fit_obj_list <- fit_obj_list %>% append(rlang::dots_list(...))
# browser()
fit_obj_list %>%
texreg::screenreg(
custom.note =
list(., seq_along(.), names(.)) %>%
pmap_chr(~ {
# browser()
# mod_nm <- str_c("Model ", ..2)
if (!is.null(..3) && ..3 != "") mod_nm <- ..3 else {
mod_nm <- str_c("Model ", ..2)
}
str_c(mod_nm, " ", as.character(..1$call)[[2]])
}) %>%
c("*** p < 0.001; ** p < 0.01; * p < 0.05", .) %>%
str_c(collapse = "\n") ,
digits = digits,
single.row = single.row,
custom.gof.rows =
map(., ~my_gof(.x, digits)) %>%
transpose() %>%
map(unlist),
reorder.gof = c(3, 4, 5, 1, 2)
)
}