# edps 587 # Spring 2019 # c.j.anderson # # Introduction: NELS data --- 2 schools setwd("D:\\Dropbox\\edps587\\\\Lectures\\1 introduction") nels <- read.csv("nels_data.csv",header=TRUE,sep=",") nels <- na.omit(nels) # Pull out data for schools 24725 and 62821 nels1 <- nels[ which(nels$sch_id==24725 | nels$sch_id==62821), ] # plot points for 2 schools plot(nels1$homework,nels1$math, type="p", pch=19, main=expression(paste("NELS Data (sub-set) ")), xlab="Time Spent Doing Homework", ylab="Math Scores", xlim=c(0,7), ylim=c(20,80) ) abline(lm(nels1$math ~ nels1$homework),col="blue",lwd=2) summary(lm1<- lm(nels1$math ~ nels1$homework)) text(19,47.5, "math = 47.97 + 3.49*homework",cex=1.25) # plot points for 2 schools w/ jitter plot(jitter(nels$homework),nels$math, type="p", pch=19, main=expression(paste("NELS Data (sub-set) with some Horizondent Jittering")), xlab="Time Spent Doing Homework", ylab="Math Scores", xlim=c(0,7), ylim=c(20,80) ) abline(lm(nels1$math ~ nels1$homework),col="blue",lwd=2) summary(lm1<- lm(nels1$math ~ nels1$homework)) text(19,47.5, "math = 47.97 + 3.49*homework",cex=1.25) ########################################## # Idenify schools in graph # ########################################## nels.s1 <- nels[ which(nels$sch_id==24725), ] nels.s2 <- nels[ which(nels$sch_id==62821), ] # Graph to illustrate need for random intercept and slope # -- create frame for plot, notice that type='n' for none par(new=FALSE) plot(jitter(nels.s1$homework),nels.s1$math, type="p", pch=19, col="blue", lwd=2, main="NELS: Linear Regression by School", xlab="Time Spent Doing Homework", ylab="Math Scores", xlim=c(0,7), ylim=c(20,80) ) abline(lm(math ~ homework,data=nels.s1),col="blue") par(new=TRUE) plot(jitter(nels.s2$homework),nels.s2$math,type="p", pch=19, col="red", xlab="Time Spent Doing Homework", ylab="Math Scores", xlim=c(0,7), ylim=c(20,80) ) abline(lm(math ~ homework,data=nels.s2),col="red",pch=19) legend(0, 80, legend=c("School 62821","School 24725"), col=c("red","blue"), lty=1, cex=1.1, box.lty=1, box.lwd=1)