# Load necessary libraries
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(MASS) # For stepAIC function
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
###Distributions
# Load the PSID data
psid_data <- read.csv("C:/Users/daniv/Downloads/PSID1982.csv")
# Visualize wage and log(wage)
hist(psid_data$wage, main = "Distribution of Wage", xlab = "Wage")
hist(log(psid_data$wage), main = "Distribution of Log(Wage)", xlab = "Log(Wage)")
The first histogram illustrates the distribution of raw wages, utilizing
the ‘hist’ function with appropriate labels and titles. Following this,
the code constructs a second histogram, this time visualizing the
log-transformed wages (‘log(wage)’). This dual visualization approach
allows for a comprehensive exploration of the wage distribution,
shedding light on potential patterns and guiding further analyses. The
log transformation is particularly insightful as it can often assist in
addressing issues of skewness and variance, making it a valuable step in
the preparation for statistical modeling, such as linear regression.
###Data Preprocessing and Model Estimation for Wage Analysis
# Log-transform the wage variable
psid_data$log_wage <- log(psid_data$wage)
# Estimate linear regression model for log(wage)
model <- lm(log_wage ~ gender + union + education + experience + I(experience^2), data = psid_data)
# Stepwise model selection
# Add 2-way interactions of gender with education and union with education
model_with_interactions <- lm(log_wage ~ . + gender:education + union:education + I(experience^2), data = psid_data)
Following the initial model estimation, the script proceeds to conduct stepwise model selection. It considers a refined model by adding two-way interactions between ‘gender’ and ‘education,’ as well as ‘union’ membership and ‘education.’ This stepwise approach allows for the automatic selection of relevant variables, potentially improving the model’s fit and interpretability. The resulting ‘model_with_interactions’ encapsulates these additional interactions, providing a more nuanced understanding of the relationships between the variables and the log-transformed wages in the PSID dataset.
###Stepwise model
# Perform stepwise model selection
step_model <- stepAIC(model_with_interactions, direction = "both")
## Start: AIC=-2237.18
## log_wage ~ wage + experience + education + gender + occupation +
## union + gender:education + union:education + I(experience^2)
##
## Df Sum of Sq RSS AIC
## - I(experience^2) 1 0.013 9.261 -2238.4
## - experience 1 0.018 9.266 -2238.1
## <none> 9.248 -2237.2
## - education:union 1 0.083 9.331 -2234.3
## - occupation 1 0.109 9.357 -2232.7
## - education:gender 1 0.331 9.580 -2219.8
## - wage 1 49.715 58.963 -1216.6
##
## Step: AIC=-2238.42
## log_wage ~ wage + experience + education + gender + occupation +
## union + education:gender + education:union
##
## Df Sum of Sq RSS AIC
## - experience 1 0.014 9.275 -2239.6
## <none> 9.261 -2238.4
## + I(experience^2) 1 0.013 9.248 -2237.2
## - education:union 1 0.077 9.338 -2235.8
## - occupation 1 0.107 9.368 -2234.1
## - education:gender 1 0.328 9.589 -2221.2
## - wage 1 51.033 60.294 -1206.3
##
## Step: AIC=-2239.6
## log_wage ~ wage + education + gender + occupation + union + education:gender +
## education:union
##
## Df Sum of Sq RSS AIC
## <none> 9.275 -2239.6
## + experience 1 0.014 9.261 -2238.4
## + I(experience^2) 1 0.009 9.266 -2238.1
## - education:union 1 0.081 9.356 -2236.8
## - occupation 1 0.112 9.386 -2235.0
## - education:gender 1 0.338 9.613 -2221.8
## - wage 1 53.006 62.281 -1190.4
# Describe the influence of education on wage in the selected model
summary(step_model)
##
## Call:
## lm(formula = log_wage ~ wage + education + gender + occupation +
## union + education:gender + education:union, data = psid_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.24192 -0.03893 0.03175 0.07175 0.22169
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.4003387 0.0994162 54.320 < 2e-16 ***
## wage 0.0007027 0.0000126 55.758 < 2e-16 ***
## education 0.0451048 0.0075466 5.977 4.12e-09 ***
## gendermale 0.5615831 0.1027456 5.466 7.03e-08 ***
## occupationwhite 0.0399817 0.0156279 2.558 0.010786 *
## unionyes 0.2150230 0.0592176 3.631 0.000309 ***
## education:gendermale -0.0337346 0.0075720 -4.455 1.02e-05 ***
## education:unionyes -0.0101497 0.0046523 -2.182 0.029561 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1306 on 544 degrees of freedom
## Multiple R-squared: 0.9074, Adjusted R-squared: 0.9062
## F-statistic: 761.8 on 7 and 544 DF, p-value: < 2.2e-16
###Regresssion equations
# Calculate regression equations for log(wage) as a function of education
# For given values of gender, union, and experience
# Assuming average_work_experience is the average work experience in the dataset
# Create a data frame for the four groups
groups <- expand.grid(gender = c("male", "female"), union = c("yes", "no"), experience = mean(psid_data$experience))
# Add education values to the data frame
groups$education <- mean(psid_data$education)
# Rename the variables to match the regression model
names(groups)[names(groups) == "experience"] <- "experience"
names(groups)[names(groups) == "education"] <- "education"
names(groups)[names(groups) == "gender"] <- "gender"
names(groups)[names(groups) == "union"] <- "union"
# Remove 'occupation' if not needed in the model
groups$occupation <- NULL
# Add other necessary variables (wage and experience^2) to the data frame
groups$wage <- NA
groups$experience_squared <- groups$experience^2
The created data frame, ‘groups,’ is meticulously structured to match the variables used in the previously estimated regression model. Unnecessary variables, such as ‘occupation,’ are removed, ensuring alignment with the model’s specifications. To facilitate the regression equation calculations, placeholders for ‘wage’ and the squared term of ‘experience’ are added to the data frame. This systematic approach sets the stage for deriving specific regression equations, offering valuable insights into how education influences log-transformed wages across different gender and union membership categories, while controlling for average work experience.