# R code to analyze the cornmeal data # using polynomial regression # In this data set (taken from Spurrier # (1993, Technometrics)), the response # is the diameter of a cornmeal product # and the predictor is the percent of # sucrose in the product. # Save the data file into a directory and # use the full path name: cornmeal.data <- read.table(file = "z:/My Documents/teaching/stat_704/cornmealdata.txt", header=FALSE, col.names = c('sucrpct', 'diameter')) # attaching the data frame: attach(cornmeal.data) # Scatterplot to check the shape of the regression relationship: plot(sucrpct, diameter) # Centering the X variable and # creating a quadratic term: csucrpct <- sucrpct - mean(sucrpct); csucrpctsq <- csucrpct^2; # fitting the regression model: corn.reg <- lm(diameter ~ csucrpct + csucrpctsq) # getting the summary regression output: # (This includes the overall F* = 15.41) summary(corn.reg) # getting the ANOVA table: anova(corn.reg) # Plotting the fitted regression curve with the data (naive approach): plot(sucrpct, diameter); lines(sucrpct, fitted(corn.reg)) # A better way to plot, but more complicated: xgrid <- seq(from=min(csucrpct), to=max(csucrpct), length=100) predgrid <- cbind(rep(1,times=length(xgrid)), xgrid, xgrid^2) %*% corn.reg$coef plot(sucrpct, diameter); lines(xgrid + mean(sucrpct), predgrid)