# the regression
t = time(USpop20) - 1960
reg = lm( USpop20~ poly(t, 10, raw=TRUE) )
# the prediction curve
b = as.vector(coef(reg))
t = 1900:2044
X = outer(t - 1960, 0:10, FUN = "^")
pred = X %*% b
# the plot
tsplot(t, pred, ylab="Population", xlab='Year', cex.main=1, col=4,
main="U.S. Population by Official Census")
points(time(USpop20), USpop20, pch=21, bg=rainbow(13), cex=1.25)
mtext(bquote('\u00D7'~10^6), side=2, line=1.5, adj=1, cex=.8)
Run the code above in your browser using DataLab