########################################################################### # PUBLG100: Introduction to Quantitative Methods # # Week 4 Solutions: Bivariate linear regression models # # ## ----message = FALSE----------------------------------------------------- library(texreg) library(Zelig) library(dplyr) ## ----eval = FALSE-------------------------------------------------------- ## # Change your working directory ## setwd("N:/PUBLG100") ## ## # Check your working directory ## getwd() ## ## # clear the environment ## rm(list = ls()) ## ------------------------------------------------------------------------ communities <- read.csv("http://uclspp.github.io/PUBLG100/data/communities.csv") communities_employment <- read.csv("http://uclspp.github.io/PUBLG100/data/communities_employment.csv") communities_immigration <- read.csv("http://uclspp.github.io/PUBLG100/data/communities_immig.csv") communities <- merge(communities, communities_employment, by = c("state", "communityname")) communities <- merge(communities, communities_immigration, by = c("state", "communityname")) ## ------------------------------------------------------------------------ communities <- select(communities, state, Community = communityname, UnemploymentRate = PctUnemployed, NoHighSchool = PctNotHSGrad, White = racePctWhite, RecentImmigration5 = PctImmigRec5) ## ------------------------------------------------------------------------ model_immigration <- lm(UnemploymentRate ~ RecentImmigration5, data = communities) ## ------------------------------------------------------------------------ plot(communities$RecentImmigration5, communities$UnemploymentRate, xlab = "Recent Immigration", ylab = "Unemployment Rate") abline(model_immigration, col = "red") ## ------------------------------------------------------------------------ model_education <- lm(UnemploymentRate ~ NoHighSchool, data = communities) communities$Minority <- 1 - communities$White model_minority <- lm(UnemploymentRate ~ Minority, data = communities) screenreg(list(model_education, model_minority, model_immigration)) ## ------------------------------------------------------------------------ htmlreg(list(model_education, model_minority, model_immigration), file = "solutions4_model_comparison.doc") ## ------------------------------------------------------------------------ z.out <- zelig(UnemploymentRate ~ RecentImmigration5, data = communities, model = "ls") x.out <- setx(z.out, RecentImmigration5 = seq(0, 1, 0.1)) s.out <- sim(z.out, x = x.out) plot(s.out, qi = "pv", xlab = "Recent Immigration (last 5 years)") ## ----eval = FALSE-------------------------------------------------------- ## png(filename = "solutions4_model_immigration.png") ## plot(communities$RecentImmigration5, communities$UnemploymentRate, ## xlab = "Recent Immigration", ## ylab = "Unemployment Rate") ## abline(model_immigration, col = "red") ## dev.off() ## ----eval = FALSE-------------------------------------------------------- ## png(filename = "solutions4_conf_interval.png") ## plot(s.out, qi = "pv", xlab = "Recent Immigration") ## dev.off()