########################################################################### # PUBLG100: Introduction to Quantitative Methods # # Week 4 Seminar: Bivariate linear regression models # # # Set your working directory # # CAUTION: Make sure the directory you specify here matches the working directory on your computer. # We're using N:/PUBLG100 only for illustration purposes and it would only work if you're # using a UCL dekstop. If you're using your own laptop, then replace N:/PUBLG100 with the # appropriate directory (or folder) setwd("N:/PUBLG100") # Verify that your working directory is set correctly getwd() ## ----eval = FALSE-------------------------------------------------------- ## install.packages("Zelig") ## install.packages("texreg") ## ----message = FALSE----------------------------------------------------- library(texreg) library(Zelig) library(dplyr) ## ------------------------------------------------------------------------ # clear environment rm(list = ls()) # set working directory # setwd(...) ## ----echo = FALSE-------------------------------------------------------- source("../common/knitr_hooks.R") ## ------------------------------------------------------------------------ # load the California test score datasets CA_admin <- read.csv("CASchoolAdmin.csv") CA_students <- read.csv("CAStudents.csv") ## ------------------------------------------------------------------------ names(CA_admin) str(CA_admin) ## ------------------------------------------------------------------------ names(CA_students) str(CA_students) ## ------------------------------------------------------------------------ # merge the two datasets CA_schools <- merge(CA_admin, CA_students, by = c("county", "school")) # explore dataset names(CA_schools) ## ----eval = FALSE-------------------------------------------------------- ## head(CA_schools) ## ------------------------------------------------------------------------ CA_schools <- mutate(CA_schools, STR = students / teachers, score = (math + read) / 2) ## ------------------------------------------------------------------------ plot(score ~ STR, data = CA_schools, xlab = "Student-Teacher Ratio", ylab = "Test Score") ## ------------------------------------------------------------------------ STR_model <- lm(score ~ STR, data = CA_schools) ## ------------------------------------------------------------------------ summary(STR_model) ## ------------------------------------------------------------------------ plot(score ~ STR, data = CA_schools, xlab = "Student-Teacher Ratio", ylab = "Test Score") abline(STR_model, col = "red") ## ------------------------------------------------------------------------ screenreg(STR_model) ## ------------------------------------------------------------------------ income_model <- lm(score ~ income, data = CA_schools) summary(income_model) ## ------------------------------------------------------------------------ plot(score ~ income, data = CA_schools, xlab = "District Income", ylab = "Test Score") abline(income_model, col = "blue") ## ------------------------------------------------------------------------ screenreg(list(STR_model, income_model)) ## ------------------------------------------------------------------------ htmlreg(list(STR_model, income_model), file = "California_Schools.doc") ## ----message = FALSE----------------------------------------------------- z.out <- zelig(score ~ income, data = CA_schools, model = "ls") ## ------------------------------------------------------------------------ summary(z.out) ## ------------------------------------------------------------------------ x.out <- setx(z.out, income = seq(min(CA_schools$income), max(CA_schools$income), 1)) s.out <- sim(z.out, x = x.out) ## ----out.lines=1:40------------------------------------------------------ summary(s.out) ## ------------------------------------------------------------------------ ci.plot(s.out, ci = 95, xlab = "District Income (in USD $1000)")