In this project, students will demonstrate their understanding of linear correlation and regression.
The project will use two datasets – mtcars and
nscc_student_data. Do some exploratory analysis of each of
them on your own when you load them into your report.
The following chunk of code will load the mtcars dataset into your environment. Familiarize yourself with the dataset before proceeding with the next 4 questions related to it.
# Store mtcars dataset into environment
mtcars <- mtcars
a.) Create a scatterplot of the weight variable (explanatory variable) and the miles per gallon variable (response variable) in the mtcars dataset.
plot(mpg ~ wt, data = mtcars)
b.) Only by looking at the scatterplot, does there appear to be a
linear relationship between the weight and mpg of a car?
# yes there is in fact a linear relationship, and it seems to be
generally solid
a.) Calculate the linear correlation coefficient of the weight and mpg variables.
#finding the correlation between wt and mpg in mtcars
cor(x = ,mtcars$mpg, y = mtcars$wt)
## [1] -0.8676594
b.) Based on that correlation coefficient, describe the linear
relationship between the two variables.
#the linear relationship is certainly strong but on a downward
trend.
a.) Create a regression equation to model the relationship between the weight and mpg of a car.
lm(mpg ~ wt, data = mtcars)
##
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
##
## Coefficients:
## (Intercept) wt
## 37.285 -5.344
b.) Use the regression equation to estimate the mpg of a car that weighs 2,000 lbs.
mpg2000 <- -5.344*2+37.285
print(mpg2000)
## [1] 26.597
c.) Use the regression equation to estimate the mpg of a car that weighs 7,000 lbs.
mpg2000 <- -5.344*7+37.285
print(mpg2000)
## [1] -0.123
d.) Do you think the predictions in parts b and c are reliable ones?
Explain why or why not.
#part B seems plausible but part C does not because a negative miles per
gallon is not possible.
What percent of the variation in a car’s mpg is explained by the car’s weight?
summary(lm(mpg ~ wt, data = mtcars))$r.squared *100
## [1] 75.28328
#75.28% of the variation is explained by the cars weight
Use the following chunk of code to load the NSCC student dataset into your environment. Familiarize yourself with the dataset before proceeding with the next few questions related to it.
# Store nscc_student_data into environment
nscc_student_data <- read.csv("nscc_student_data.csv")
I’m curious if a person’s height is better predicted by their shoe length or by their pulse rate.
plot(PulseRate ~ Height, data = nscc_student_data)
plot(ShoeLength ~ Height, data = nscc_student_data)
#For the Pulse Rate plot, there appears to be no correlation at all as the data seems very randomly placed out. #For the Shoe Length plot, there seems to be a moderate upward trend with one extreme variable. #shoe length appears to be a determining factor in height because it has a correlation.
use = "pairwise.complete.obs" in your
call to the cor() function to deal with the missing
values.cor(x = nscc_student_data$PulseRate, y = nscc_student_data$Height, use = "pairwise.complete.obs")
## [1] 0.2028639
cor(x = nscc_student_data$ShoeLength, y = nscc_student_data$Height, use = "pairwise.complete.obs")
## [1] 0.2695881
lm(ShoeLength ~ Height, data = nscc_student_data)
##
## Call:
## lm(formula = ShoeLength ~ Height, data = nscc_student_data)
##
## Coefficients:
## (Intercept) Height
## 1.8288 0.1284
0.1284*10 + 1.8288
## [1] 3.1128
a.) You hopefully found that these were both poor models. Which pair
of variables, based on common sense, would you have expected to have a
poor/no relationship before your analysis?
#Pulse Rate seemed like the poorer variable to compare considering that
heart health and height have no medical correlation.
b.) Perhaps you expected the other pair of variables to have a
stronger relationship than it ended up having. Can you come up with any
reasoning based on the specific sample of data for why the relationship
did not turn out to be very strong?
#there is a myth about tall people having larger shoes, this dataset
very much disproves this as it shows that the correlation is only 27%
and the linear model is not an effective judge of height.