Splus Log for Stat 798C Concerning Different Approaches to Nonparametric Regression (with Scalar Response & Predictor) ==================================================== 5/7/03 > motif() > nprgfram <- cbind.data.frame(x= runif(300) -> xv, y= 2-xv+xv^3 + rnorm(300)*sqrt(1+0.5*xv^2)) > plot(nprgfram$x,nprgfram$y, main="Scatterplot of Raw Data, n=300") > for(i in 1:4) abline(v=i*.2, lty=3) > for(i in 1:5) { inds <- (1:300)[abs(nprgfram$x-.2*i+.1) < 0.1] itmp <- order(nprgfram$x[inds]) lines(nprgfram$x[inds[itmp]], lm(y ~ x, data=nprgfram[ inds,])$fit[itmp], lty=6) } > lines((1:300)/300 -> xv2, 2-xv2+xv2^3) > printgraph(file="NPRgRaw.ps") ### Now consider a lowess fit: > plot(nprgfram$x,nprgfram$y, main="Raw Data with Lowess Line, n=300") > tmplow <- lowess(nprgfram$x,nprgfram$y) lines(tmplow$x,tmplow$y, lty=6) lines((1:300)/300 -> xv2, 2-xv2+xv2^3) > printgraph(file="Lowess.ps") ### Now consider a kernel-based nonparametric-regression fit: ### Take expectations near each of 40 evenly spaced points > xp <- (1:40)/41 kermat <- outer(xp, nprgfram$x, function(x,y) dnorm(x,y,.16)) ### Our bandwidth is .16, using Gaussian kernel: > plot(nprgfram$x,nprgfram$y, main= "Raw Data with Kernel Reg Line, n=300") ### Now create conditional expectation using kernel as weights > cexp <- c((kermat %*% nprgfram$y)/kermat %*% rep(1,300)) > points(xp, cexp, pch=5, type="b") > lines((1:300)/300 -> xv2, 2-xv2+xv2^3) > printgraph(file="KernCexp.ps") > tmpsp <- smooth.spline(nprgfram, all.knots=F) > plot(nprgfram$x,nprgfram$y, main= "Raw Data with Smoothing Spline Reg Line, n=300") lines(tmpsp$x, tmpsp$y, lty=6) ### Very flat, almost horizontal line ### Finally, let's see just how the parametric fit would work in this case > xv2 <- sort(xv) > lines(xv2, lm(nprgfram$y ~ xv2 + I(xv2^2) + I(xv2^3))$fit, lty=2) > printgraph(file="SplineLin.ps")