# January 27, 2009 # try some linear regression examples eg.0910 <- function() { set.seed(478); x = c(rnorm(50,.2,.06),rnorm(50,.5,.06),rnorm(50,.8,.06)) y1=rep(1,50); y0=rep(0,50); X = cbind(rep(1,150),x) Y = cbind(c(y1,y0,y0),c(y0,y1,y0),c(y0,y0,y1)) Yh = X %*% solve(t(X)%*%X) %*% t(X) %*% Y plot(X[,2],Y %*% c(1,2,3),main="K=3 groups",xlim=range(X[,1],0,1)) browser() plot(X[,2],Yh[,1],pch=16,main="linear scores",xlim=range(X[,1],0,1)); rug(X[,2]) points(X[,2],Yh[,2],pch=16,col=5); points(X[,2],Yh[,3],pch=16,col=6); abline(h=c(0,1),lty=2) browser() X = cbind(X,X[,2]^2) Yh = X %*% solve(t(X)%*%X) %*% t(X) %*% Y plot(X[,2],Yh[,1],pch=16,main="linear scores w/ quadratic",xlim=range(X[,1],0,1), ylim=range(Yh)); rug(X[,2]); abline(h=c(0,1),lty=2) points(X[,2],Yh[,2],pch=16,col=5); points(X[,2],Yh[,3],pch=16,col=6) browser() X = cbind(X,X[,2]^3) Yh = X %*% solve(t(X)%*%X) %*% t(X) %*% Y plot(X[,2],Yh[,1],pch=16,main="linear scores w/cubic",xlim=range(X[,1],0,1), ylim=range(Yh)); rug(X[,2]); abline(h=c(0,1),lty=2) points(X[,2],Yh[,2],pch=16,col=5); points(X[,2],Yh[,3],pch=16,col=6) browser() X = cbind(X,X[,2]^4) Yh = X %*% solve(t(X)%*%X) %*% t(X) %*% Y plot(X[,2],Yh[,1],pch=16,main="linear scores w/quartic",xlim=range(X[,1],0,1), ylim=range(Yh)); rug(X[,2]); abline(h=c(0,1),lty=2) points(X[,2],Yh[,2],pch=16,col=5); points(X[,2],Yh[,3],pch=16,col=6) } eg.0910()