abline(v=35,lty=5) segments(48,g2$coef[1]+g2$coef[2]*48,35,g2$coef[1]+g2$coef[2]*35) segments(20,g1$coef[1]+g1$coef[2]*20,35,g1$coef[1]+g1$coef[2]*35) lines(x,py,type=2) lines(x,py,lty=2) summary(lm(sr~ddpi,savings)) summary(lm(sr~ddpi+I(ddpi^2),savings)) plot(savings$ddpi,savings$sr) summary(lm(sr~ddpi+I(ddpi^2)+I(ddpi^3),savings)) poly(savings$ddpi,3) g <- lm(sr~poly(ddpi,3),savings) summary(g) g <- lm(sr~poly(ddpi,2),savings) g x <- model.matrix(g) t(x) %*% x weird <- function(x) sin(2*pi*x^3)^3 x <- seq(0,1,by=0.01) plot(x,weird(x),type="l") y <- weird(x)+0.1*rnorm(101) plot(x,y) g4 <- lm(y~poly(x,4)) g12 <- lm(y~poly(x,12)) matplot(x,cbind(y,g4fit,g12$fit),type="pll",ylab="y",pch=18,lty=c(1,2)) matplot(x,cbind(y,g4$fit,g12$fit),type="pll",ylab="y",pch=18,lty=c(1,2)) library(splines) knots <- c(0,0,0,0,0.2,0.4,0.5,0.6,0.7,0.8,0.85,0.9,1,1,1,1) bx <- splineDesign(knots,x) gs <- lm(y~bx) lines(x,gs$fit,col=3) lines(x,gs$fit,col=4) matplot(x,bx,type="l") gx summary(gs) matplot(x,cbind(y,g4$fit,g12$fit),type="pll",ylab="y",pch=18,lty=c(1,2)) matplot(x,bx,type="l") matplot(x,cbind(y,g4fit,g12$fit),type="pll",ylab="y",pch=18,lty=c(1,2)) matplot(x,cbind(y,g4$fit,g12$fit),type="pll",ylab="y",pch=18,lty=c(1,2)) library(splines) knots <- c(0,0,0,0,0.2,0.4,0.5,0.6,0.7,0.8,0.85,0.9,1,1,1,1) bx <- splineDesign(knots,x) gs <- lm(y~bx) lines(x,gs$fit,col=3) lines(x,gs$fit,col=4) matplot(x,bx,type="l") gx summary(gs) matplot(x,cbind(y,g4$fit,g12$fit),type="pll",ylab="y",pch=18,lty=c(1,2)) matplot(x,bx,type="l") matplot(x,cbind(y,g4$fit,g12$fit),type="pll",ylab="y",pch=18,lty=c(1,2)) lines(x,gs$fit,col=4)