calories <- c(110,110,150,130,120) fat <- c(0,1,3,2,1) Cereal <- data.frame(calories,fat) rownames(Cereal) <- c("Crispix","Froot Loops","Muesli Raisins","Oatmeal Raisin","Post Nat Raisin") Cereal rm(calories,fat) # remove unneeded objects Cereal.Fit <- lm(Cereal$calories ~ Cereal$fat) # fit a linear regression model # the output from SAS Proc Reg is contained in one or the other of summary() or anova() summary(Cereal.Fit) anova(Cereal.Fit) # a plot with the raw data, the fitted regression line, and a lowess line plot(Cereal$fat,Cereal$calories,xlab="Fat",ylab="Calories",main="Regression with cereal data") abline(Cereal.Fit) lines(lowess(Cereal$fat,Cereal$calories)) # this code creates a residual-by-predicted plot and a normal plot of the residuals par(mfrow=c(2,1)) # makes all future plots appear two # per page, vertically plot(fitted(Cereal.Fit),resid(Cereal.Fit),xlab="Predicted Values", ylab="Residuals",main="Residual by Predicted plot") qqnorm(resid(Cereal.Fit)) qqline(resid(Cereal.Fit)) par(mfrow=c(1,1)) # restores one plot per page # this code produces a plot like the one in the corresponding SAS code # note that the function 'predict' can produce predicted values, # confidence interval values, and prediction interval values plot(Cereal$fat,Cereal$calories,xlab="Fat",ylab="Calories",main="Regression with cereal data", xlim=c(0,3),ylim=c(75,180)) points(Cereal$fat,predict(Cereal.Fit),pch="r") points(Cereal$fat,predict(Cereal.Fit, interval="confidence")[,2],pch="c") points(Cereal$fat,predict(Cereal.Fit, interval="confidence")[,3],pch="c") points(Cereal$fat,predict(Cereal.Fit, interval="prediction")[,2],pch="p") points(Cereal$fat,predict(Cereal.Fit, interval="prediction")[,3],pch="p") # here we print out the confidence and prediction interval values, compare to the SAS printout predict(Cereal.Fit, interval="confidence") predict(Cereal.Fit, interval="prediction")