GreenBlueBar.gif GreenBlueBar.gif

R Code for Chapter 11

Multiple Regression


This chapter does not have much in the way of R code, but what there is can be seen below. In the first section of code I read in the entire data set of school expenditures, but then deleted the second variable, which was the name of the state. That is a character variable and can't be correlated with anything. Then I decided that I only want a few of the variables, so I created a new data frame with only the important variables. Note how I did this. (Actually I could have gotten away without deleting state names, because that variable was not used in the new data frame.) Usually if you have a command like cor(), you simply put the names of the variables you want to correlated within the parentheses. But because I wanted to correlate all of the variables that were in that new data frame, I could simply enter "cor(data frame name)." The same goes for the plot command.

The second section of code uses something that I mentioned on the last Web page. I created a model by predicting SATcombined from both Expend and PctSAT. Then I pulled out the set of predicted values from that model with "pred <- model1$fitted.values". So I now have a new variable (pred) which consists of the values predicted by model1. In the next statement I compute a new model that just predicts SATcombined from pred. I wanted to show that the multiple correlation is simply the correlation between the dependent variable and the values predicted from the equation (SATcombined = b1*Expend + b2PctSAT + b0)

The final set of code is simply creating the model by predicting distress at time 2 from distress at time 1 and the degree to which the individual blames their cancer on the type of person they are. There is nothing new here.


### Chapter 11  Multiple REgression

schoolData <- read.table("http://www.uvm.edu/~dhowell/fundamentals9/DataFiles/Tab11-1.dat", header = TRUE)
attach(schoolData)
schoolData <- schoolData[-2]    #Remove string variable "State" for future analyses
       ## Create a new data frame with only the variables of interest.
smallData <- as.data.frame(cbind(Expend, Salary, PctSAT, SATcombined) )

cor(smallData)                             #Produces Table 11.1 
plot(smallData)                            #Produces Figure 11.3

model1 <- lm(SATcombined ~ Expend + PctSAT)
summary(model1)
pred <- model1$fitted.values                    # Extract predicted values
model2 <- lm(SATcombined~pred)           # Needed to draw line

# The following produces R's equivalent of Figure 11.3
plot(SATcombined~pred, xlab = "unstandardized predicted value", ylab = "Combined value")   
abline(model2)
legend(950, 1100, 'Rsq = .8195', bty = "n")

###  Table 11.9 Analysis
distress.data <- read.table("http://www.uvm.edu/~dhowell/fundamentals9/DataFiles/Tab11-9.dat", header = TRUE)
attach(distress.data)
model4 <- lm(Distress2 ~ Distress1 + BlamePer)
summary(model4)

GreenBlueBar.gif GreenBlueBar.gif dch: