# edps 587 # Spring 2019 # c.j.anderson # # Introduction: JSP data setwd("D:\\Dropbox\\edps587\\\\Lectures\\1 introduction") jsp <- read.csv("jsp_data.csv",header=TRUE,sep=",") jsp <- na.omit(jsp) # Pull out data for school 1, years 0 and 1 jsp1 <- jsp[ which(jsp$school==1 & jsp$jsyear!=2), ] # change long to wide jsp.wide1 <- reshape(jsp1, idvar = "id", timevar = "jsyear", direction = "wide") # plot points for school 1 plot(jsp.wide1$math.0,jsp.wide1$math.1, type="p", pch=19, main=expression(paste("JSP Data, (", R^2, "= .70)")), xlab="Math Scores Year=0", ylab="Math Scores Year=1", xlim=c(0,50), ylim=c(0,50) ) abline(lm(jsp.wide1$math.1 ~ jsp.wide1$math.0),col="blue",lwd=2) summary(lm1<- lm(jsp.wide1$math.1 ~ jsp.wide1$math.0)) text(19,47.5, expression(paste(math[1]," = 3.4251 + 0.8803*",math[0])),cex=1.25) ########################################## # Graph regression lines all in one plot # ########################################## # Pull out data for years 0 and 1 (all school) jsp.2 <- jsp[ which(jsp$jsyear!=2), ] # Change from long to wide jsp.all <- reshape(jsp.2, idvar = "id", timevar = "jsyear", direction = "wide") jsp.all <- na.omit(jsp.all) school <- unique(jsp.all$school.0) N <- length(school) # Graph to illustrate need for random intercept and slope # -- create frame for plot, notice that type='n' for none plot(jsp.all$math.0,jsp.all$math.1, type="n", col="blue", lwd=2, main="JSP: Linear Regression by School", xlab="Math Scores in Year = 0", ylab="Math Scores in Year = 1" ) # -- loop through the N schools and plot regression line for each for (j in 1:N) { sub <- subset(jsp.all,jsp.all$school.0==j) fitted <- fitted(lm(math.1~math.0,data=sub)) lines(sub$math.0,fitted,col=j) }