# R Code, September 19, 2011 # Wesley Burr # STAT 464/864 # setwd("~/doc/STAT464_11/data/") uspop<-read.table("USpop.dat",header=T) uspop2<-scan("USpop2.dat") plot(seq(1790,1990,10),uspop2/10^6,type="b",xlab="Year", ylab="Population in Millions",main="US Population, 1790-1990") lines(uspop[,1],uspop[,2]/10^6,type="l",col="red") # But what does the last 100 years look like? plot(uspop[,1],uspop[,2]/10^6,type="l",xlab="Year",ylab="Population in Millions", main="US Population, 1901-1999") # Fit some models to these series T <- seq(1790,1990,10) # Quadratic fitPoly1 <- lm(uspop2 ~ T + I(T^2)) plot(T,uspop2/10^6,type="b",xlab="Year",ylab="Population in Millions", main="US Population, 1790-1990") attributes(fitPoly1) lines(T,fitPoly1$fitted.values/10^6,type="l",col="red") # Try the same fit to the 1901-1999 data fitPoly2 <- lm(uspop[,2] ~ uspop[,1] + I(uspop[,1]^2)) plot(uspop[,1],uspop[,2]/10^6,type="l",xlab="Year",ylab="Population in Millions", main="US Population, 1901-1999") lines(uspop[,1],fitPoly2$fitted.values/10^6,type="l",col="red") # Compare coefficients fitPoly1$coef/10^6 fitPoly2$coef/10^6 # On 1901-1999 data, fit linear and quadratic, compare # fitPoly2 is already the quadratic fit fitPoly3 <- lm(uspop[,2] ~ uspop[,1]) # Compare Mean-Squared Error sum((fitPoly2$residuals)^2)/length(uspop[,1]) sum((fitPoly3$residuals)^2)/length(uspop[,1])