-
Notifications
You must be signed in to change notification settings - Fork 315
/
Table 7-1-1.r
78 lines (63 loc) · 2.35 KB
/
Table 7-1-1.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# R code for Table 7.1.1 #
# Required packages #
# - haven: read in .dta files #
# - quantreg: quantile regressions #
# - knitr: markdown tables #
library(haven)
library(quantreg)
library(knitr)
# Download data and unzip the data
download.file('http://economics.mit.edu/files/384', 'angcherfer06.zip')
unzip('angcherfer06.zip')
# Create a function to run the quantile/OLS regressions so we can use a loop
quant.mincer <- function(tau, data) {
r <- rq(logwk ~ educ + black + exper + exper2,
weights = perwt,
data = data,
tau = tau)
return(rbind(summary(r)$coefficients["educ", "Value"],
summary(r)$coefficients["educ", "Std. Error"]))
}
# Create function for producing the results
calculate.qr <- function(year) {
# Create file path
dta.path <- paste('Data/census', year, '.dta', sep = "")
# Load year into the census
df <- read_dta(dta.path)
# Run quantile regressions
taus <- c(0.1, 0.25, 0.5, 0.75, 0.9)
qr <- sapply(taus, quant.mincer, data = df)
# Run OLS regressions and get RMSE
ols <- lm(logwk ~ educ + black + exper + exper2,
weights = perwt,
data = df)
coef.se <- rbind(summary(ols)$coefficients["educ", "Estimate"],
summary(ols)$coefficients["educ", "Std. Error"])
rmse <- sqrt(sum(summary(ols)$residuals^2) / ols$df.residual)
# Summary statistics
obs <- length(na.omit(df$educ))
mean <- mean(df$logwk, na.rm = TRUE)
sd <- sd(df$logwk, na.rm = TRUE)
return(cbind(rbind(obs, NA),
rbind(mean, NA),
rbind(sd, NA),
qr,
coef.se,
rbind(rmse, NA)))
}
# Generate results
results <- rbind(calculate.qr("80"),
calculate.qr("90"),
calculate.qr("00"))
# Name rows and columns
row.names(results) <- c("1980", "", "1990", "", "2000", "")
colnames(results) <- c("Obs", "Mean", "Std Dev",
"0.1", "0.25", "0.5", "0.75", "0.9",
"OLS", "RMSE")
# Format decimals
results <- round(results, 3)
results[ , c(2, 10)] <- round(results[ , c(2, 10)], 2)
results[ , 1] <- formatC(results[ , 1], format = "d", big.mark = ",")
# Export table
print(kable(results))
# End of file