We’ll use the mtcars dataset and create some categorical variables to demonstrate the functionality.
# Load and prepare the data
data <- mtcars
# Create categorical variables
data$size_cat <- cut(data$wt,
breaks = c(-Inf, 2.5, 3.5, Inf),
labels = c("light", "medium", "heavy"))
data$cyl_cat <- factor(data$cyl)
data$am_cat <- factor(data$am, labels = c("automatic", "manual"))
# Create some numeric variables for controls
data$hp_per_wt <- data$hp / data$wt
We’ll create several models to demonstrate different aspects of the functions:
# Define models
m1 <- lm(mpg ~ wt + hp_per_wt, data = data)
m2 <- lm(mpg ~ size_cat, data = data)
m3 <- lm(mpg ~ size_cat + hp_per_wt, data = data)
m4 <- lm(mpg ~ size_cat * am_cat, data = data)
m5 <- lm(log(mpg) ~ wt + hp_per_wt, data = data)
m6 <- lm(mpg ~ size_cat + cyl_cat + hp_per_wt, data = data)
models <- list("Model A" = m1,
"Model B" = m2,
"Model C" = m3,
"Model D" = m4,
"Model E" = m5,
"Model F" = m6)
# Create a vector to specify which models should have exponentiated coefficients
exp_ctrl <- c(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE)
Now we’ll create display names for our variables to demonstrate the af_cov_names function.
# Create display names mapping
display_names <- list(
"wt" = "Weight (1000 lbs)",
"hp_per_wt" = "Power-to-Weight Ratio",
"size_cat" = "Vehicle Size",
"am_cat" = "Transmission",
"cyl_cat" = "Cylinders"
)
# Generate covariate labels using af_cov_names
cov_labels <- af_cov_names(data, models, display_names)
Now we’ll demonstrate af_stargazer with html output file (for MS Word), html display and automatic notes:
# Generate Notes
notes <- af_create_regression_notes(
df = data, models = models, display_names = display_names,
is_robust = FALSE, is_exp = exp_ctrl,
show_significance = TRUE, significance_levels = c(0.05, 0.01, 0.001)
)
# Generate the table
af_stargazer(
models = list(m1, m2, m3, m4, m5, m6),
tag = "regression_results",
cov_labels = cov_labels,
coef_exp = FALSE,
exp_ctrl = exp_ctrl,
table_type = 'html',
out_type = "html",
notes = notes
)
Dependent variable: | ||||||
mpg | log(mpg) | mpg | ||||
(1) | (2) | (3) | (4) | (5) | (6) | |
Weight (1000 lbs) | -5.255*** | -0.267*** | ||||
(0.480) | (0.021) | |||||
Power-to-Weight Ratio | -0.099** | -0.038 | -0.005*** | 0.001 | ||
(0.029) | (0.037) | (0.001) | (0.039) | |||
Vehicle Size[medium] | -8.357*** | -8.321*** | -1.800 | -5.214* | ||
(1.429) | (1.428) | (3.245) | (2.077) | |||
Vehicle Size[heavy] | -13.387*** | -12.962*** | -6.830* | -7.077* | ||
(1.477) | (1.535) | (3.209) | (2.904) | |||
Transmission[manual] | 7.529* | |||||
(3.271) | ||||||
Vehicle Size[medium] × Transmission[manual] | -7.449 | |||||
(3.707) | ||||||
Vehicle Size[heavy] × Transmission[manual] | -7.199 | |||||
(4.582) | ||||||
Cylinders[6] | -3.130 | |||||
(2.107) | ||||||
Cylinders[8] | -6.317* | |||||
(2.703) | ||||||
Observations | 32 | 32 | 32 | 32 | 32 | 32 |
R2 | 0.824 | 0.740 | 0.749 | 0.784 | 0.868 | 0.793 |
Adjusted R2 | 0.812 | 0.722 | 0.722 | 0.742 | 0.859 | 0.753 |
Note: * p < 0.050; ** p < 0.010; *** p < 0.001. The reference category for Vehicle Size is ‘light’. The reference category for Transmission is ‘automatic’. The reference category for Cylinders is ‘4’. Standard errors in parentheses. The coefficients of model 5 are exponentiated. |
Now we’ll demonstrate af_stargazer with LaTex output file
and inline-code (hidden) results:
# Generate the table
af_stargazer(
models = list(m1, m2, m3, m4, m5, m6),
tag = "regression_results",
cov_labels = cov_labels,
coef_exp = FALSE,
exp_ctrl = exp_ctrl,
table_type = 'text',
out_type = 'tex'
)
#>
#> ====================================================================================================
#> Dependent variable:
#> --------------------------------------------------------
#> mpg log(mpg) mpg
#> (1) (2) (3) (4) (5) (6)
#> ----------------------------------------------------------------------------------------------------
#> Weight (1000 lbs) -5.255*** 0.765***
#> (0.480) (0.021)
#> Power-to-Weight Ratio -0.099** -0.038 0.995*** 0.001
#> (0.029) (0.037) (0.001) (0.039)
#> Vehicle Size[medium] -8.357*** -8.321*** -1.800 -5.214*
#> (1.429) (1.428) (3.245) (2.077)
#> Vehicle Size[heavy] -13.387*** -12.962*** -6.830* -7.077*
#> (1.477) (1.535) (3.209) (2.904)
#> Transmission[manual] 7.529*
#> (3.271)
#> Vehicle Size[medium] × Transmission[manual] -7.449
#> (3.707)
#> Vehicle Size[heavy] × Transmission[manual] -7.199
#> (4.582)
#> Cylinders[6] -3.130
#> (2.107)
#> Cylinders[8] -6.317*
#> (2.703)
#> ----------------------------------------------------------------------------------------------------
#> Observations 32 32 32 32 32 32
#> R2 0.824 0.740 0.749 0.784 0.868 0.793
#> Adjusted R2 0.812 0.722 0.722 0.742 0.859 0.753
#> ====================================================================================================
#> *p<0.05; **p<0.01; ***p<0.001
#> Estimates of models 5 are exponentiated.
The table above demonstrates several features of the af_stargazer function:
Some key points about the output:
This demonstration shows how these functions can be used to create publication-ready regression tables with proper labeling and formatting, while handling both categorical and continuous variables appropriately.
Open webpage https://www.fontsquirrel.com/fonts/latin-modern-roman Click “Webfont Kit”, then check “TTF” Click “DOWNLOAD @FONT-FACE KIT”, to download a zip file containing TTF version fonts. COpy the font file lmroman10-regular-webfont.ttf to the project folder
Note: Due to a bug in the extrafont package, installing the font file in RStudio requires an old version of Rttf2pt1 tools should be installed.
Browse the following blog posts to get additional clear & relevant information: