Load the data from Table 1 in Section 4.1 into R.
Table1 <- read.csv("https://sullystats.github.io/Statistics6e/Data/Chapter4/Table1.csv")
head(Table1,n=4)
## Speed Distance
## 1 100 257
## 2 102 264
## 3 103 274
## 4 101 266
Rather than reading the data from Github, we could manually enter the data into R.
Table1a=data.frame("Speed"=c(100, 102, 103, 101, 105, 100, 99, 105), "Distance"=c(257, 264, 274, 266, 277, 263, 258, 275))
As always, find the least-squares regression model and save it. The lm command automatically determines the residuals and saves them (as a vector) using the name residuals.
golf_model <- lm(Distance ~ Speed,data=Table1) # Name and find regression model.
residuals(golf_model)
## 1 2 3 4 5 6 7
## -3.8135593 -3.1457627 3.6881356 2.0203390 0.3559322 2.1864407 0.3525424
## 8
## -1.6440678
To draw a residual plot, use the plot command. Add the commmand abline(h=0) to draw a horizontal line at y = 0.
plot(Table1$Speed, golf_model$residuals, xlab = "Club-Head Speed (mph)", ylab = "Residuals", main = "Residual Plot") + abline(h=0)
## integer(0)
To draw a boxplot of residuals, use the boxplot command.
boxplot(golf_model$residuals,horizontal = T, col = '#6897bb',main = "Boxplot of Residuals")