# Scott Merrill # heteroscedasticity vs homoscedasticity rm(list = ls(all=TRUE)) # Below in the graphics function par, pty = "s" # forces individual graphs into squares par(mfrow = c(1,1), pty = "s") ############# ## linear ## ############# wheat.height = sample(seq(0,5, by = .01),50,replace=TRUE) aphids=numeric(length(wheat.height)) aphids = 10*wheat.height + rnorm(mean=0,n=length(wheat.height),sd=10) plot(wheat.height,aphids, ylim=c(-25,60)) modL = lm(aphids ~ wheat.height) summary(modL) abline(modL$coefficients,lwd=3, col ="blue") plot(wheat.height,modL$residuals) ########### ## non-linear function ############ aphids=numeric(length(wheat.height)) aphids = 3*wheat.height^3 + rnorm(mean=0,n=length(wheat.height),sd=4) plot(wheat.height,aphids, ylim=c(-150,400)) modA = lm(aphids ~ wheat.height) summary(modA) abline(modA$coefficients,lwd=3, col ="blue") plot(wheat.height,modA$residuals) # Contrasted with linear points(wheat.height,modL$residuals, lwd = 3, col=2) ######## ## variation increasing with increasing x value ######## aphids=numeric(length(wheat.height)) aphids = 10*wheat.height + 2*wheat.height*rnorm(mean=0,n=length(wheat.height),sd=10) plot(wheat.height,aphids) modM = lm(aphids ~ wheat.height) summary(modM) abline(modM$coefficients,lwd=3, col ="blue") plot(wheat.height,modM$residuals) require(graphics) locator(n = 512, type = "l", lwd = 4, col = "green") # Cool locator function site: http://www.inside-r.org/r-doc/graphics/locator # Contrasted with linear points(wheat.height,modL$residuals, lwd = 3, col=2) locator(n = 512, type = "l", lwd = 4, col = "green") ###### # Independence of errors ###### plot(modM$residuals,aphids) plot(modA$residuals,aphids) # plot points that are of interest from one plot onto a second plot plot(locator(n = 512, type = "l", lwd = 4, col = "green"))