Appendix B: Complete R Code
#load Ecdat package
library("Ecdat")
#rename dataset as school
school <- Schooling
#show head of dataset
head(school)
#show structure of dataset
str(school)
#show levels of
#histogram of wages
hist(school$lwage76)
#boxplot of ME of smsa on wage
boxplot(school$lwage76~school$smsa66, xlab="Lived in SMSA",ylab="Wage")
title("Lived in SMSA")
#boxplot of ME of proximity to 4-year college on wage
boxplot(school$lwage76~school$nearc4, xlab="Lived near 4-Year College",ylab="Wage")
title("Lived near 4-Year College")
#boxplot of ME of library card on wage
boxplot(school$lwage76~school$libcrd14, xlab="Had Library Card at Age 14",ylab="Wage")
title("Ownership of Library Card")
#boxplot of ME of single mother on wage
boxplot(school$lwage76~school$sinmom14, xlab="Had a Single Mother at Age 14",ylab="Wage")
title("Single Mother")
#split smsa factor by levels
smsa_y <- subset(school,school$smsa66 == "yes")
smsa_n <- subset(school,school$smsa66 == "no")
#plot of ME of smsa on wage
plot(c(1,2), c(mean(smsa_n$lwage76),mean(smsa_y$lwage76)),type = 'l',
main = "ME of SMSA on Wages",xlab = "Lived in SMSA",
ylab = "Wages",xlim = c(.5,2.5),xaxt = "n")
#label x-axis
axis(1,at = c(1,2),labels = c("No","Yes"))
#show means on plot
text(c(1,2) - .25,c(mean(smsa_n$lwage76),mean(smsa_y$lwage76)),
round(c(mean(smsa_n$lwage76),mean(smsa_y$lwage76)),digits = 2))
#split proximity to 4-year college factor by levels
nearc4_y <- subset(school,school$nearc4 == "yes")
nearc4_n <- subset(school,school$nearc4 == "no")
#plot of ME of proximity to 4-year college on wage
plot(c(1,2), c(mean(nearc4_n$lwage76),mean(nearc4_y$lwage76)),type = 'l',
main = "ME of Proximity to 4-year College on Wages",xlab = "Lived near 4 year college",
ylab = "Wages",xlim = c(.5,2.5),xaxt = "n")
#label x-axis
axis(1,at = c(1,2),labels = c("No","Yes"))
#show means on plot
text(c(1,2) - .25,c(mean(nearc4_n$lwage76),mean(nearc4_y$lwage76)),
round(c(mean(nearc4_n$lwage76),mean(nearc4_y$lwage76)),digits = 2))
#split library card factor by levels
lib_y <- subset(school,school$libcrd14 == "yes")
lib_n <- subset(school,school$libcrd14 == "no")
#plot of ME of library card on wage
plot(c(1,2), c(mean(lib_n$lwage76),mean(lib_y$lwage76)),type = 'l',
main = "ME of Library Card on Wages",xlab = "Had library card at age 14",
ylab = "Wages",xlim = c(.5,2.5),xaxt = "n")
#label x-axis
axis(1,at = c(1,2),labels = c("No","Yes"))
#show means on plot
text(c(1,2) - .25,c(mean(lib_n$lwage76),mean(lib_y$lwage76)),
round(c(mean(lib_n$lwage76),mean(lib_y$lwage76)),digits = 2))
#split single mother factor by levels
mom_y <- subset(school,school$sinmom14 == "yes")
mom_n <- subset(school,school$sinmom14 == "no")
#plot of ME of single mother on wage
plot(c(1,2), c(mean(mom_n$lwage76),mean(mom_y$lwage76)),type = 'l',
main = "ME of Single Mother on Wages",xlab = "Had a single mother at age 14",
ylab = "Wages",xlim = c(.5,2.5),xaxt = "n")
#label x-axis
axis(1,at = c(1,2),labels = c("No","Yes"))
#show means on plot
text(c(1,2) - .25,c(mean(mom_n$lwage76),mean(mom_y$lwage76)),
round(c(mean(mom_n$lwage76),mean(mom_y$lwage76)),digits = 2))
#anova of smsa on wage
anova1 <- aov(school$lwage76~school$smsa66)
anova(anova1)
#anova of proximity to 4-year college on wage
anova2 <- aov(school$lwage76~school$nearc4)
anova(anova2)
#anova of library card on wage
anova3 <- aov(school$lwage76~school$libcrd14)
anova(anova3)
#anova of single mother on wage
anova4 <- aov(school$lwage76~school$sinmom14)
anova(anova4)
#interaction between smsa and library card on wage
int1 <- aov(school$lwage76~school$smsa66*school$libcrd14)
anova(int1)
#interaction plot of smsa and library card on wage
interaction.plot(school$libcrd14,school$smsa66,school$lwage76)
#interaction between library card and single mother on wage
int2 <- aov(school$lwage76~school$libcrd14*school$sinmom14)
anova(int2)
#interaction plot of library card and single mother on wage
interaction.plot(school$sinmom14,school$libcrd14,school$lwage76)
#interaction between single mother and near 4 year college on wage
int3 <- aov(school$lwage76~school$sinmom14*school$nearc4)
anova(int3)
#interaction plot of single mother and near 4 year college wage
interaction.plot(school$nearc4,school$sinmom14,school$lwage76)
#interaction between near 4 year college and smsa on wage
int4 <- aov(school$lwage76~school$nearc4*school$smsa66)
anova(int4)
#interaction plot of near 4 year college and smsa on wage on wage
interaction.plot(school$smsa66,school$nearc4,school$lwage76)
#interaction between smsa and single mother on wage
int5 <- aov(school$lwage76~school$smsa66*school$sinmom14)
anova(int5)
#interaction plot of smsa and single mother on wage
interaction.plot(school$sinmom14,school$smsa66,school$lwage76)
#interaction between library card and near 4 year college on wage
int6 <- aov(school$lwage76~school$libcrd14*school$nearc4)
anova(int6)
#interaction plot of library card and near 4 year college on wage
interaction.plot(school$nearc4,school$libcrd14,school$lwage76)