Created
September 16, 2010 15:33
-
-
Save marutter/582627 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unm <- read.csv("enrollment.csv") | |
plot(unm$unem,unm$enroll,xlab="Unemployment (%)",ylab="Enrollment") | |
# | |
# We could use our commands, but there is built in tools | |
# | |
res <- lm(enroll~unem,data=unm) | |
names(res) | |
res$coef | |
res$fitted | |
res$resid | |
summary(res) | |
# | |
# We will discuss some of this output more in detail on Monday | |
# | |
plot(unm$unem,unm$enroll,xlab="Unemployment (%)",ylab="Enrollment") | |
abline(res) | |
confint(res) | |
confint(res,level=.90) | |
# | |
# Predicted values based on model | |
# | |
xh <- data.frame(unem=7) | |
predict(res,newdata=xh) | |
xh <- data.frame(unem=c(6,7,8,9,10)) | |
predict(res,newdata=xh) | |
predict(res,newdata=xh,se.fit=T) | |
predict(res,newdata=xh,interval="confidence",level=.95) | |
predict(res,newdata=xh,interval="prediction",level=.95) | |
# | |
# Can we graph this in a useful fashion? | |
# | |
attach(unm) # Break apart the file so we have easy access to data | |
plot(unem,enroll,xlab="Unemployment (%)",ylab="Enrollment",pch=20) | |
abline(res,lwd=2) | |
newData <- data.frame(unem=seq(min(unem),max(unem),by=(max(unem)-min(unem))/45)) | |
conf.lim <- predict(res,newData,interval="confidence") | |
pred.lim <- predict(res,newData,interval="prediction") | |
matlines(newData$unem,conf.lim[,-1],col="red",lty=2) | |
matlines(newData$unem,pred.lim[,-1],col="green",lty=3) | |
legend(8.5,7000,legend=c("Fitted Line","Confidence Bands","Prediction Bands"), | |
lty=c(1,2,3),col=c("black","red","green")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment