###### Script for Slidedeck e-9 ### Correlation Test ### Preamble source("http://rfs.kvasaheim.com/stat200.R") dt = read.csv("http://rfs.kvasaheim.com/data/crime.csv") attach(dt) ### Initial Graphics # Graphic 1: Data par(xaxs="i",yaxs="i") par(family="serif",las=1) par(mar=c(3,3,0,0)+1) par(font.lab=2,cex.lab=1.1) plot.new() plot.window( xlim=c(0,2500), ylim=c(0,2500)) points(vcrime90,vcrime00, pch=21, bg="salmon") axis(1); axis(2) title(xlab="Violent Crime Rate (1990)", line=2.25) title(ylab="Violent Crime Rate (2000)", line=3.00) # Graphic 2: Data w. line modOLS = lm(vcrime00~vcrime90) xx = 0:2500 y1 = predict(modOLS, newdata=data.frame(vcrime90=xx)) par(xaxs="i",yaxs="i") par(family="serif",las=1) par(mar=c(3,3,0,0)+1) par(font.lab=2,cex.lab=1.1) plot.new() plot.window( xlim=c(0,2500), ylim=c(0,2500)) axis(1); axis(2) title(xlab="Violent Crime Rate (1990)", line=2.25) title(ylab="Violent Crime Rate (2000)", line=3.00) lines(xx,y1, lwd=2, col="dodgerblue") points(vcrime90,vcrime00, pch=21, bg="salmon") # Graphic 3: Data w. line and all error pr = predict(modOLS) par(xaxs="i",yaxs="i") par(family="serif",las=1) par(mar=c(3,3,0,0)+1) par(font.lab=2,cex.lab=1.1) plot.new() plot.window( xlim=c(0,2500), ylim=c(0,2500)) axis(1); axis(2) title(xlab="Violent Crime Rate (1990)", line=2.25) title(ylab="Violent Crime Rate (2000)", line=3.00) lines(xx,y1, lwd=2, col="dodgerblue") points(vcrime90,vcrime00, pch=21, bg="salmon") segments(vcrime90,vcrime00, vcrime90,pr, col="black", lwd=1 ) ### Framing Example modOLS = lm( vcrime00~vcrime90 ) summary.aov( modOLS ) summary.lm( modOLS ) summary( modOLS ) confint( modOLS ) # Graphic 5: Data w. line and eqn png("linreg-vcrime-4.png", height=4,width=4, units="in",res=600) par(cex=0.8) par(xaxs="i",yaxs="i") par(family="serif",las=1) par(mar=c(3,3,0,0)+1) par(font.lab=2,cex.lab=1.1) plot.new() plot.window( xlim=c(0,2500), ylim=c(0,2500)) axis(1); axis(2) title(xlab="Violent Crime Rate (1990)", line=2.25) title(ylab="Violent Crime Rate (2000)", line=3.00) lines(xx,y1, lwd=2, col="dodgerblue") points(vcrime90,vcrime00, pch=21, bg="salmon") text(1600,1150, label="y = 109.5 + 0.58 x", srt=30) # Interval calculations predict(modOLS, newdata=data.frame(vcrime90=500)) predict(modOLS, newdata=data.frame(vcrime90=500), interval="confidence") predict(modOLS, newdata=data.frame(vcrime90=500), interval="prediction") # Graphic 6: Data w. intervals x5 = 1500 y5c = predict(modOLS, newdata=data.frame(vcrime90=x5), interval="confidence") y5p = predict(modOLS, newdata=data.frame(vcrime90=x5), interval="prediction") y5c y5p par(xaxs="i",yaxs="i") par(family="serif",las=1) par(mar=c(3,3,0,0)+1) par(font.lab=2,cex.lab=1.1) plot.new() plot.window( xlim=c(0,2500), ylim=c(0,2500)) axis(1); axis(2) title(xlab="Violent Crime Rate (1990)", line=2.25) title(ylab="Violent Crime Rate (2000)", line=3.00) lines(xx,y1, lwd=2, col="dodgerblue") points(vcrime90,vcrime00, pch=21, bg="salmon") segments(x5,y5c[2],x5,y5c[3], lwd=5,col="black") segments(x5,y5p[2],x5,y5p[3], lwd=2,col="red") ##### ##### ##### ### Example 1 mod1 = lm(vcrime00 ~ enroll90) summary(mod1) ### Example 2 mod2 = lm(vcrime00~gspcap90) summary(mod2) confint(mod2) predict(mod2, newdata=data.frame(gspcap90=50000), interval="predict") ### Example 3 mod3 = lm(vcrime00 ~ unemp1990) summary(mod3) confint(mod3) predict(mod3, newdata=data.frame(profleg=1), intereval="confidence") ### End of File