# Load libraries
install.packages("wooldridge")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
library(wooldridge)

# Load the WAGE2 data
data("wage2")

# (i) Run a simple regression of IQ on educ to obtain the slope coefficient d1
model_i <- lm(IQ ~ educ, data = wage2)
d1 <- coef(model_i)[2]  
cat("The slope coefficient d1 (IQ on educ):", d1, "\n")
## The slope coefficient d1 (IQ on educ): 3.533829
# (ii) Run a simple regression of log(wage) on educ to obtain the slope coefficient b1
model_ii <- lm(log(wage) ~ educ, data = wage2)
b1 <- coef(model_ii)[2]  
cat("The slope coefficient b1 (log(wage) on educ):", b1, "\n")
## The slope coefficient b1 (log(wage) on educ): 0.05983921
# (iii) Run the multiple regression of log(wage) on educ and IQ to obtain b^1 and b^2
model_iii <- lm(log(wage) ~ educ + IQ, data = wage2)
b1_hat <- coef(model_iii)[2]  # slope for educ
b2_hat <- coef(model_iii)[3]  # slope for IQ
# (iv) Verify the relationship b1 = b^1 + b^2 * d1
b1_verification <- b1_hat + b2_hat * d1
cat("The calculated b1 from b^1 and b^2 * d1:", b1_verification, "\n")
## The calculated b1 from b^1 and b^2 * d1: 0.05983921
cat("The slope coefficients b^1 (educ):", b1_hat, " and b^2 (IQ):", b2_hat, "\n")
## The slope coefficients b^1 (educ): 0.0391199  and b^2 (IQ): 0.005863132
cat("Does b1 match the calculated value?", all.equal(b1, b1_verification), "\n")
## Does b1 match the calculated value? TRUE
summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00