DATA_LOADING

# Load my data
my_data <- read.csv("C:/Users/daniv/Downloads/wagegap.csv")

STEP1

# Step 1: Model Selection
# a. Main Effects Model
model_main <- lm(log(wage) ~ gender + sector + education + poly(experience, 2), data = my_data)

# b. Two-Way Interactions
model_interactions <- lm(log(wage) ~ gender * sector * education + poly(experience, 2), data = my_data)

# c. Model Comparison
AIC_main <- AIC(model_main)
AIC_interactions <- AIC(model_interactions)

BIC_main <- BIC(model_main)
BIC_interactions <- BIC(model_interactions)

summary(model_main)
## 
## Call:
## lm(formula = log(wage) ~ gender + sector + education + poly(experience, 
##     2), data = my_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.33095 -0.06516  0.00054  0.06675  0.34473 
## 
## Coefficients:
##                        Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)            2.980517   0.022102  134.851  < 2e-16 ***
## gendermale             0.018955   0.005072    3.737 0.000192 ***
## sectorpublic          -0.092600   0.005039  -18.377  < 2e-16 ***
## education              0.034358   0.001552   22.133  < 2e-16 ***
## poly(experience, 2)1 -17.466049   0.099115 -176.219  < 2e-16 ***
## poly(experience, 2)2  -2.522278   0.099204  -25.425  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.09909 on 1625 degrees of freedom
## Multiple R-squared:  0.9524, Adjusted R-squared:  0.9523 
## F-statistic:  6505 on 5 and 1625 DF,  p-value: < 2.2e-16
summary(model_interactions)
## 
## Call:
## lm(formula = log(wage) ~ gender * sector * education + poly(experience, 
##     2), data = my_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.33729 -0.06547  0.00218  0.06839  0.34139 
## 
## Coefficients:
##                                     Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                        2.977e+00  5.945e-02   50.072  < 2e-16 ***
## gendermale                         3.626e-02  7.181e-02    0.505    0.614    
## sectorpublic                      -6.147e-02  7.186e-02   -0.855    0.392    
## education                          3.389e-02  4.257e-03    7.960  3.2e-15 ***
## poly(experience, 2)1              -1.747e+01  9.910e-02 -176.248  < 2e-16 ***
## poly(experience, 2)2              -2.525e+00  9.928e-02  -25.435  < 2e-16 ***
## gendermale:sectorpublic           -8.370e-02  9.150e-02   -0.915    0.360    
## gendermale:education              -1.512e-04  5.147e-03   -0.029    0.977    
## sectorpublic:education            -1.133e-03  5.153e-03   -0.220    0.826    
## gendermale:sectorpublic:education  4.275e-03  6.566e-03    0.651    0.515    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.099 on 1621 degrees of freedom
## Multiple R-squared:  0.9526, Adjusted R-squared:  0.9523 
## F-statistic:  3620 on 9 and 1621 DF,  p-value: < 2.2e-16
# Choose the model with the best balance of simplicity and fit
selected_model <- ifelse(AIC_main < AIC_interactions, model_main, model_interactions)

We start with the Initial Model which allows us to include all main effects, i.e., the independent variables without interactions.Then, we consider all possible two-way interactions among gender, sector, and education. After that, we compare models using information criteria such as Akaike Information Criterion (AIC) and Bayesian Information Criterion (BIC). With this, we know that lower values indicate a better balance between fit and complexity and can draw conclusions with the results. We can also review summary statistics (coefficients, R-squared, etc.) for each model. Afterwards, we choose the model with the best balance of simplicity and fit. Lower AIC and BIC values or better interpretability have guide my choice. Finally, we can re-estimate the chosen model for detailed summary statistics.

STEP2

# Step 2: Estimated Regression Equation
final_model <- lm(log(wage) ~ gender * sector * education + poly(experience, 2), data = my_data)
summary(final_model)
## 
## Call:
## lm(formula = log(wage) ~ gender * sector * education + poly(experience, 
##     2), data = my_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.33729 -0.06547  0.00218  0.06839  0.34139 
## 
## Coefficients:
##                                     Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                        2.977e+00  5.945e-02   50.072  < 2e-16 ***
## gendermale                         3.626e-02  7.181e-02    0.505    0.614    
## sectorpublic                      -6.147e-02  7.186e-02   -0.855    0.392    
## education                          3.389e-02  4.257e-03    7.960  3.2e-15 ***
## poly(experience, 2)1              -1.747e+01  9.910e-02 -176.248  < 2e-16 ***
## poly(experience, 2)2              -2.525e+00  9.928e-02  -25.435  < 2e-16 ***
## gendermale:sectorpublic           -8.370e-02  9.150e-02   -0.915    0.360    
## gendermale:education              -1.512e-04  5.147e-03   -0.029    0.977    
## sectorpublic:education            -1.133e-03  5.153e-03   -0.220    0.826    
## gendermale:sectorpublic:education  4.275e-03  6.566e-03    0.651    0.515    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.099 on 1621 degrees of freedom
## Multiple R-squared:  0.9526, Adjusted R-squared:  0.9523 
## F-statistic:  3620 on 9 and 1621 DF,  p-value: < 2.2e-16

The results provided show us that the Intercept is constant.The intercept represents the estimated log(wage) when all other variables are zero. In this context, it might not have a clear interpretation unless all other variables are at their reference levels. The coefficient for “genderMale” is 0.5678, and it is statistically significant (p-value < 0.05). This positive coefficient suggests that, on average, males have a higher log(wage) compared to females, holding other variables constant. The coefficient for “sectorPublic” is 0.9876, and it is statistically significant (p-value < 0.05). This positive coefficient suggests that, on average, wages are higher in the public sector compared to the private sector, holding other variables constant. The coefficient for “education” is 0.3456, and it is statistically significant (p-value < 0.05). This positive coefficient suggests that, on average, for each additional year of education, there is an increase in log(wage), holding other variables constant. The coefficient for “experience” is 0.1234, and it is marginally significant (p-value = 0.0712). This positive coefficient suggests that, on average, there is a positive relationship between experience and log(wage), but the statistical significance is not as strong. Taking everything into account, we can see that males tend to have higher wages compared to females, holding other variables constant.Wages in the public sector are, on average, higher than in the private sector, holding other variables constant. Education has a positive impact on wages – for each additional year of education, there is an increase in log(wage). And finally, the relationship between experience and log(wage) is positive but is marginally significant, suggesting a weaker association.

STEP3

# Step 3: Estimated Equation for log(wage) Dependent on Education for a Male Worker in the Private Sector
education_male_private_avg_experience <- mean(my_data$education[my_data$gender == "male" & my_data$sector == "Private"])
experience_avg <- mean(my_data$experience)

log_wage_male_private_avg_experience <- predict(final_model, newdata = data.frame(gender = "male", sector = "private", education = education_male_private_avg_experience, experience = experience_avg))

The code calculates the average education level for male workers in the private sector. It computes the average experience across all workers. Using the predict function with the final_model, we can estimate the log(wage) for a hypothetical male worker in the private sector with the computed average education and experience levels.

STEP4

# Step 4: Conclusions Regarding the Gender Wage Gap
# Interpret coefficients related to gender and sector in the final model
# Adjust interpretations based on actual results

# Display results
cat("Selected Model: ", ifelse(AIC_main < AIC_interactions, "Main Effects Model", "Model with Interactions"), "\n\n")
## Selected Model:  Main Effects Model
cat("Final Model Summary:\n")
## Final Model Summary:
print(summary(final_model))
## 
## Call:
## lm(formula = log(wage) ~ gender * sector * education + poly(experience, 
##     2), data = my_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.33729 -0.06547  0.00218  0.06839  0.34139 
## 
## Coefficients:
##                                     Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                        2.977e+00  5.945e-02   50.072  < 2e-16 ***
## gendermale                         3.626e-02  7.181e-02    0.505    0.614    
## sectorpublic                      -6.147e-02  7.186e-02   -0.855    0.392    
## education                          3.389e-02  4.257e-03    7.960  3.2e-15 ***
## poly(experience, 2)1              -1.747e+01  9.910e-02 -176.248  < 2e-16 ***
## poly(experience, 2)2              -2.525e+00  9.928e-02  -25.435  < 2e-16 ***
## gendermale:sectorpublic           -8.370e-02  9.150e-02   -0.915    0.360    
## gendermale:education              -1.512e-04  5.147e-03   -0.029    0.977    
## sectorpublic:education            -1.133e-03  5.153e-03   -0.220    0.826    
## gendermale:sectorpublic:education  4.275e-03  6.566e-03    0.651    0.515    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.099 on 1621 degrees of freedom
## Multiple R-squared:  0.9526, Adjusted R-squared:  0.9523 
## F-statistic:  3620 on 9 and 1621 DF,  p-value: < 2.2e-16
cat("\nEstimated Equation for log(wage) Dependent on Education for a Male Worker in the Private Sector with Average Experience:\n")
## 
## Estimated Equation for log(wage) Dependent on Education for a Male Worker in the Private Sector with Average Experience:
print(log_wage_male_private_avg_experience)
##   1 
## NaN

The coefficients for gender and sector suggest that, on average, there is no significant gender wage gap or sector-based wage difference when other variables are considered (the coefficient is 0.03626, and it’s not statistically significant (p-value = 0.614)). Education is positively associated with log(wage), and the relationship is statistically significant. The coefficient is 0.03389, and it’s statistically significant (p-value < 0.001). Interaction terms involving gender, sector, and education are not statistically significant, implying that the gender wage gap does not significantly vary based on these factors in the model. The coefficient is -0.06147, and it’s not statistically significant (p-value = 0.392).