# # MATH 3330 Section B F01 # Intro to Regression # Week 2 # Chapter 2: What is regression analysis # Capturing the pattern in the # conditional expectation of Y given X # E ( Y | X ) # # An artificial data set: # plot( c(0,10), c(0,10), type = "n" ) # sets up axes # z <- locator( n=100, type = "p" ) # generate data for a swan function # swan <- data.frame(Y = z$y, X = z$x ) # # Pretend X is temperature and Y is amount of chemical Z produced # in a reaction # xyplot( Y ~ X , swan ) xyplot( Y ~ X , swan , col = 1 ) # adding lines to the plot with a 'panel' function xyplot( Y ~ X , swan, col = 1, panel = function( x, y ,...) { panel.xyplot( x, y, ...) # plotting points } ) xyplot( Y ~ X , swan, col = 1, panel = function( x, y ,...) { panel.xyplot( x, y, ...) panel.lmline( x, y, ...) # add a least-squares line } ) xyplot( Y ~ X , swan, col = 1, panel = function( x, y ,...) { panel.xyplot( x, y, ...) panel.lmline( x, y, ...) panel.loess( x, y, span = 2/3, family = "gaussian") # add a very smooth non-resistant loess line } ) xyplot( Y ~ X , swan, col = 1, panel = function( x, y ,...) { panel.xyplot( x, y, ...) panel.lmline( x, y, ...) panel.loess( x, y, span = 2/3, family = "gaussian") panel.loess( x, y, span = 1/3, family = "gaussian", lty = 3, col=8) # add a less smooth non-resistant loess line } ) xyplot( Y ~ X , swan, col = 1, panel = function( x, y ,...) { panel.xyplot( x, y, ...) panel.lmline( x, y, ...) panel.loess( x, y, span = 2/3, family = "gaussian") panel.loess( x, y, span = 1/3, family = "gaussian", lty = 3, col=2) panel.loess( x, y, span = 1/8, family = "gaussian", lty = 5, col=8) # add a much less smooth non-resistant loess line } ) xyplot( Y ~ X , swan, col = 1, panel = function( x, y ,...) { panel.xyplot( x, y, ...) panel.lmline( x, y, ...) panel.loess( x, y, span = 2/3, family = "gaussian") panel.loess( x, y, span = 1/3, family = "gaussian", lty = 3, col=2) panel.loess( x, y, span = 1/8, family = "gaussian", lty = 5, col=5) panel.loess( x, y, span = 1/8, family = "symmetric", lty = 5, col=8) # same smoothness but resistant } ) # # problem: maybe we need a wider window on the left than # on the right # # S-Plus function supsmu (super smoother) uses a variable # window determined by cross-validation [see SGS p. 160 ff] # # There is no special panel function for supsmu so we write # it a bit differently: xyplot( Y ~ X , swan, col = 1, panel = function( x, y ,...) { panel.xyplot( x, y, ...) panel.loess( x, y, span = 1/8, family = "symmetric", lty = 5, col=5) # last loess from before fit <- supsmu( x, y, span = "cv" ) panel.xyplot( fit$x, fit$y, type = "l", col = 8) } ) # # supsmu looks better on the left but worse on the right # it works better with more data (n > 100 or more) #