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))
Find the Least-Squares Regression Line
To find the Least-Squares regression line between two variables, use the following command:
lm(y-variable ~ x-variable, data = df_name)
lm(Distance ~ Speed,data=Table1)
##
## Call:
## lm(formula = Distance ~ Speed, data = Table1)
##
## Coefficients:
## (Intercept) Speed
## -55.797 3.166
Letting x represent the speed and y represent the distance, the least-squares regression model is
\(\hat{y}=3.166x - 55.797\)
Graph Least-Squares Regression Line on Scatter Diagram
To graph the least-squares regression line on the scatter diagram, first store the regression line in a variable called golf_model. Then, draw the scatter diagram using the plot command. Finally, use the abline command to draw the regression line on the scatter diagram.
golf_model <- lm(Distance ~ Speed, data=Table1)
plot(Table1$Speed, Table1$Distance,xlab="Club-Head Speed (mph)",ylab="Distance (yards)",main="Driving Distance versus Club-Head Speed")
abline(golf_model) # Draws regression line on the scatter diagram.
Make Predictions Using a Least-Squares Regression Model
predict(golf_model,data.frame(Speed=103))
## 1
## 270.3119
We predict the mean distance the golf ball will travel when hit at 103 mph is 270.3 yards.
Obtaining the Sum of Squared Residuals of a Least-Squares Regression Model
resids <- residuals(golf_model)
sum(resids^2)
## [1] 49.85763
The sum of squared residuals is 49.86.
install.packages("Mosaic")
To find the least-squares regression model, use the lm command.
lm(y-variable ~ x-variable, data = df_name)
library(mosaic)
lm(Distance ~ Speed,data=Table1)
##
## Call:
## lm(formula = Distance ~ Speed, data = Table1)
##
## Coefficients:
## (Intercept) Speed
## -55.797 3.166
Letting x represent the speed and y represent the distance, the least-squares regression model is
\(\hat{y}=3.166x - 55.797\)
Graph Least-Squares Regression Line on Scatter Diagram
To graph the least-squares regression line on the scatter diagram, first store the regression line in a variable called golf_model. Then, draw the scatter diagram and least-squares regression model using the plotModel command.
golf_model <- lm(Distance ~ Speed, data=Table1)
plotModel(golf_model,main="Club-Head Speed versus Distance")
Make Predictions Using a Least-Squares Regression Model
predict(golf_model,data.frame(Speed=103))
## 1
## 270.3119
We predict the mean distance the golf ball will travel when hit at 103 mph is 270.3 yards.
Obtaining the Sum of Squared Residuals of a Least-Squares Regression Model
resids <- residuals(golf_model)
sum(resids^2)
## [1] 49.85763
The sum of squared residuals is 49.86.