1 Required Libraries

library(stargazer)
library(afcommon)

2 Prepare Dataset

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

3 Model Specification

We’ll create several models to demonstrate different aspects of the functions:

  1. Basic linear model with numeric predictors (mpg ~ wt + hp_per_wt)
  2. Model with categorical predictor (mpg ~ size_cat)
  3. Model with both categorical and numeric predictors (mpg ~ size_cat + hp_per_wt)
  4. Model with interaction terms (mpg ~ size_cat * am_cat)
  5. Model that we’ll want to exponentiate (log(mpg) ~ wt + hp_per_wt)
  6. Another model with different specification (mpg ~ size_cat + cyl_cat + hp_per_wt)
# 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)

3.1 Creating Display Names

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)

3.2 Generate Regression Table

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.

3.3 Interpretation

The table above demonstrates several features of the af_stargazer function:

  1. Handling of both numeric and categorical variables
  2. Custom labels for variables using af_cov_names
  3. Selective exponentation of coefficients (Model 5)
  4. Proper formatting of interaction terms
  5. Consistent presentation of multiple models
  6. Automatic handling of categorical variable levels

Some key points about the output:

  • For categorical variables, the reference levels are automatically omitted
  • Interaction terms are properly labeled with the × symbol
  • Model 5’s coefficients are exponentiated while others remain in their original scale
  • R², Adjusted R², and number of observations are reported for each model
  • Stars indicate statistical significance levels
  • The table includes proper labels and formatting for HTML 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.

3.4 Installing Latex Modern font in RStudio

3.4.1 Getting the font file

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.

3.4.2 Updating extrafont package

install.packages("extrafont")
install.packages("pkgbuild")
library(extrafont)
library(remotes)
remotes::install_version("Rttf2pt1", version = "1.3.8")

3.4.3 Installing the Latex Modern font

extrafont::font_import(paths = "<dirctory path where the font file resides>")
extrafont::fonts()

3.5 Additional material

Browse the following blog posts to get additional clear & relevant information:

https://yuzar-blog.netlify.app/posts/2022-08-01-sjplot/

https://rpubs.com/ayyildiz/759651