Final exam
Preparing the data
# Loading necessary libraries
library(ggplot2)
library(tidyr)
library(dplyr)##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(effects)## Loading required package: carData
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
# Loading the dataset
library(readr)
wagegap <- read_csv("~/Documents/DATA ANALYTICS/wagegap.csv")## Rows: 1935 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): gender, sector
## dbl (3): wage, education, experience
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Doing the regression model
# Create a quadratic term for work experience
wagegap$experience_squared <- wagegap$experience^2
# Fit the linear regression model
model <- lm(log(wage) ~ gender * sector + education + experience + experience_squared, data = wagegap)
# Display the summary of the model
summary(model)##
## Call:
## lm(formula = log(wage) ~ gender * sector + education + experience +
## experience_squared, data = wagegap)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.40825 -0.06759 -0.00260 0.06896 0.44056
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.168418 0.074652 55.838 < 2e-16 ***
## gendermale 0.023439 0.008698 2.695 0.00710 **
## sectorpublic -0.091187 0.008817 -10.343 < 2e-16 ***
## education 0.034008 0.001402 24.256 < 2e-16 ***
## experience 0.028272 0.009686 2.919 0.00355 **
## experience_squared -0.007247 0.000322 -22.503 < 2e-16 ***
## gendermale:sectorpublic -0.020674 0.010539 -1.962 0.04995 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1009 on 1928 degrees of freedom
## Multiple R-squared: 0.9507, Adjusted R-squared: 0.9505
## F-statistic: 6195 on 6 and 1928 DF, p-value: < 2.2e-16
gendermale (0.023439): Males are estimated to have wages approximately 2.35% higher than females.
education (0.034008): A one-year increase in education is associated with an estimated wage increase of approximately 3.43%.
experience (0.028272): A one-year increase in experience is associated with an estimated wage increase of approximately 2.83%.
experience_squared (-0.007247): Indicates diminishing returns to experience. The wage increase associated with experience diminishes as experience increases.
gendermale:sectorpublic (-0.020674): Males in the public sector are estimated to have wages approximately 2.06% lower than females in the private sector.
Model Fit: The model fits well with a high Multiple R-squared (0.9507), suggesting it explains a significant portion of the variance in log-wages. The F-statistic is significant (p-value < 2.2e-16), indicating the overall model’s statistical significance.
Selecting model using Forward Selection:
Steps for Forward Selection:
- Initialize Model: Started with an empty model that includes only the intercept.
- Evaluate Candidates: Fitted models with each remaining predictor separately and select the one that improves the model the most based on a chosen criterion (e.g., AIC, BIC, adjusted R-squared).
- Add Selected Predictor: Added the selected predictor to the model.
- Repeat: Repeated steps 2-3 until no more predictors meet the chosen criterion for inclusion.
# Initialize an empty model
selected_vars <- c()
# Forward selection loop
while (length(selected_vars) < ncol(wagegap) - 1) {
remaining_vars <- setdiff(names(wagegap), selected_vars)
best_candidate <- NULL
best_model <- NULL
# Iterate over remaining variables
for (var in remaining_vars) {
temp_formula <- as.formula(paste("log(wage) ~", paste(c(selected_vars, var), collapse = '+')))
temp_model <- lm(temp_formula, data = wagegap)
# Check if the model is better
if (is.null(best_model) || AIC(temp_model) < AIC(best_model)) {
best_candidate <- var
best_model <- temp_model
}
}
# Add the best candidate to the model
selected_vars <- c(selected_vars, best_candidate)
cat("Selected Variable:", best_candidate, "\n")
}## Selected Variable: experience_squared
## Selected Variable: wage
## Selected Variable: experience
## Selected Variable: education
## Selected Variable: sector
# Display the final selected variables
cat("\nFinal Selected Variables:", selected_vars, "\n")##
## Final Selected Variables: experience_squared wage experience education sector
# Display the final model summary
final_formula <- as.formula(paste("log(wage) ~", paste(selected_vars, collapse = '+')))
final_model <- lm(final_formula, data = wagegap)
summary(final_model)##
## Call:
## lm(formula = final_formula, data = wagegap)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.259649 -0.021124 0.001759 0.024390 0.298823
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.2666518 0.0596246 4.472 8.19e-06 ***
## experience_squared -0.0140481 0.0001742 -80.644 < 2e-16 ***
## wage 0.0238074 0.0002919 81.572 < 2e-16 ***
## experience 0.3640939 0.0061746 58.967 < 2e-16 ***
## education 0.0089165 0.0007337 12.152 < 2e-16 ***
## sectorpublic -0.0239730 0.0024481 -9.793 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.0479 on 1929 degrees of freedom
## Multiple R-squared: 0.9889, Adjusted R-squared: 0.9888
## F-statistic: 3.429e+04 on 5 and 1929 DF, p-value: < 2.2e-16
The intercept represents the estimated log-wage when all other variables are zero. In this context, it sets the baseline for log-wage.
Experience Squared (-0.0140481): The negative coefficient indicates diminishing returns to experience squared. As experience squared increases, the rate of increase in log-wage diminishes.
Experience (0.3640939):A one-year increase in experience is associated with an estimated increase in log-wage. This implies that more experience is strongly associated with higher log-wages.
Education (0.0089165):A one-year increase in education is associated with an increase in log-wage. This suggests that higher education levels are associated with higher log-wages.
Sector (sectorpublic -0.0239730): Public sector workers are estimated to have log-wages lower compared to private sector workers. This implies that being in the public sector is associated with a lower log-wage compared to the private sector.
In summary, the model indicates that experience, wage, and education have positive associations with log-wage, while experience squared has a diminishing effect. Being in the public sector is associated with a lower log-wage compared to the private sector.