# install.packages('lmtest')
library(readr) # read csv
library(lmtest) # stat tests
library(sandwich) # cov matrixes
library(readxl) # csv and excel files
library(psych) # descriptive statistics
library(knitr) # for tables
library(xtable) # for latex tables
library(ggplot2) # for graphs
earning_data <- read_csv('C:/Users/Popov/Documents/Studies/NES_studies/R/Econometrics_1/HA1/cps99_ps1.csv',
show_col_types = FALSE)
# Describe the data
description <- describe(earning_data)
# For required variables
desc <- description[c('ahe', 'yrseduc', 'female'), c('mean', 'sd')]
# xtable(desc, caption = 'Descriptive statistics', digits = 3, type = "latex")
knitr::kable(desc, caption = "Descriptive statistics", format = "markdown")
| mean | sd | |
|---|---|---|
| ahe | 15.1904191 | 8.027257 |
| yrseduc | 13.4654853 | 2.479692 |
| female | 0.4385083 | 0.496270 |
# desc # not cute
ahe_yrseduc <- lm(ahe ~ yrseduc, data = earning_data)
rob_se_ahe_yrseduc<- sqrt(diag(vcovHC(ahe_yrseduc, type = "HC1"))) #for plots
print(coeftest(ahe_yrseduc, vcov = vcovHC, type = 'HC1'))
##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2.609050 0.647177 -4.0314 5.653e-05 ***
## yrseduc 1.321859 0.050043 26.4145 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
ggplot(earning_data, aes(yrseduc, ahe)) +
geom_point() + # Scatter plot of data points
geom_abline(intercept = coef(ahe_yrseduc)[1], slope = coef(ahe_yrseduc)[2], color = "blue") + # Add regression line
labs(title = "Scatter Plot with Regression Line",
x = "yrseduc",
y = "ahe")
c_int <- confint(ahe_yrseduc, coef = "yrseduc", vcov. = vcovHC(ahe_yrseduc, type = "HC1"))
cat("\n95% Confidence Interval for β1 (slope):\n", c_int[2,])
##
## 95% Confidence Interval for β1 (slope):
## 1.227613 1.416104
r_2 <- summary(ahe_yrseduc)$r.squared
# Display the R-squared value
cat("R-squared (R2) of the regression model:\n", r_2)
## R-squared (R2) of the regression model:
## 0.1667368
corr<- cor(earning_data$ahe, earning_data$yrseduc)
cat("Correlation coefficient between ahe and yrseduc:\n", corr)
## Correlation coefficient between ahe and yrseduc:
## 0.4083341
# Make predictions using the model
pred_values <- predict(ahe_yrseduc)
# Compute residuals
res <- earning_data$ahe - pred_values
# Calculate RMSE
rmse <- sqrt(mean(res^2))
# Display the RMSE
cat("Root Mean Squared Error (RMSE) of the regression model:\n", rmse)
## Root Mean Squared Error (RMSE) of the regression model:
## 7.326572
ahe_female <- lm(ahe ~ female, data = earning_data)
rob_se_ahe_female<- sqrt(diag(vcovHC(ahe_female, type = "HC1"))) #for plots
print(coeftest(ahe_female, vcov = vcovHC, type = 'HC1'))
##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 16.59478 0.18535 89.534 < 2.2e-16 ***
## female -3.20258 0.25141 -12.739 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
m_earn <- subset(earning_data, female == 0)$ahe
fem_earn <- subset(earning_data, female == 1)$ahe
## means
mean_m_earn <- mean(m_earn)
mean_fem_earn <- mean(fem_earn )
## gender gap
gender_gap <- mean_m_earn - mean_fem_earn
## t-statistic (using robust errors)
t_stat_gender_gap <- gender_gap/rob_se_ahe_female[2] #SE for "female" coef
## Extract p-value
p_value_gender_gap <- summary(ahe_female)$coefficients[2, 4]
## Display results
cat("Sample Average of ahe for Men:", mean_m_earn, "\n",
"Sample Average of ahe for Women:", mean_fem_earn, "\n",
"Gender Gap in Earnings:", gender_gap, "\n",
"T-Statistic for Testing Gender Gap = 0:", t_stat_gender_gap, "\n", "P-value for the hypothesis test:", p_value_gender_gap)
## Sample Average of ahe for Men: 16.59478
## Sample Average of ahe for Women: 13.3922
## Gender Gap in Earnings: 3.202581
## T-Statistic for Testing Gender Gap = 0: 12.73868
## P-value for the hypothesis test: 9.945135e-35
# ==============================================
# Alternative way (gives the same test, but without calculation of the gap)
t.test(ahe ~ female, data = earning_data, vcov = vcovHC, type = 'HC1')
##
## Welch Two Sample t-test
##
## data: ahe by female
## t = 12.739, df = 3774, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
## 2.709676 3.695487
## sample estimates:
## mean in group 0 mean in group 1
## 16.59478 13.39220