# R code to analyze the portrait studio data # using multiple linear regression # reading the file off the web: # (This may not work in some implementations of R) studio.data <- read.table(file = url("http:/www.stat.sc.edu/~hitchcock/stat701/studiodata.txt"), header=FALSE, col.names = c('people16', 'income', 'sales')) # Or, save the data file into a directory and # use the full path name: studio.data <- read.table(file = "y:/My Documents/teaching/stat_701/studiodata.txt", header=FALSE, col.names = c('people16', 'income', 'sales')) # attaching the data frame: attach(studio.data) # fitting the regression model: studio.reg <- lm(sales ~ people16 + income) # getting the summary regression output: # (This includes the overall F* = 99.1) summary(studio.reg) # getting the ANOVA table: anova(studio.reg) # Note that to get the "regression df" and SSR to reflect the SAS # ANOVA table, we must add the first two lines together. # getting the fitted values: fitted(studio.reg) # getting the residuals: resid(studio.reg) # getting the 95% confidence interval for the mean at X1=65.4 and X2=17.6: xh.values <- data.frame(cbind(people16 = 65.4, income = 17.6)) predict(studio.reg, xh.values, interval="confidence", level=0.95) #### Residual plots and other plots #### # producing the scatter plot matrix pairs(studio.data) # residual plot (against fitted values) plot(fitted(studio.reg), resid(studio.reg), ylab="Residuals", xlab="Fitted Values"); abline(h=0) # Q-Q plot of residuals qqnorm(resid(studio.reg))