Purpose

In this project, students will demonstrate their understanding of linear correlation and regression.

Instructions

  1. Update the author line at the top to have your name in it.
  2. You must knit this document to an html file and publish it to RPubs. Once you have published your project to the web, you must copy the url link into the appropriate Course Project assignment in Blackboard before the end of the day on the due date.
  3. Answer all the following questions completely. Some may ask for written responses.
  4. Use R chunks for code to be evaluated where needed and always comment all of your code so the reader can understand what your code aims to accomplish.
  5. Proofread your knitted document before publishing it to ensure it looks the way you want it to. Use double spaces at the end of a line to create a line break and make sure text does not have a header label that isn’t supposed to.
  6. Delete these instructions from your published project.
  7. There are 8 questions each worth 10 points. Writing mechanics (spelling, grammer, etc.) will count for 10 points. Document organization (commenting code chunks, headers, etc.) will also count for 10 points.

Preparation

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.

Part 1 - mtcars dataset

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

Question 1 – Create Scatterplots

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

Question 2 – Correlation Coefficient

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.

Question 3 – Create a Regression Model

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.

Question 4 – Explained Variation

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

Part 2 – NSCC Student dataset

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")

Question 5 – Create Scatterplots

I’m curious if a person’s height is better predicted by their shoe length or by their pulse rate.

  1. Create two scatterplots, both with height as the response variable. One with shoe length as the explanatory variable and the other with pulse rate as the explanatory variable.
plot(PulseRate ~ Height, data = nscc_student_data)

plot(ShoeLength ~ Height, data = nscc_student_data)

  1. Discuss the two scatterplots individually. Based only on glancing at the scatter plots, does there appear to be a linear relationship between the variables? If so, is the relationship weak/moderate/strong? Based on the scatterplots, does either explanatory variable appear to be a better predictor of height? Explain your reasoning. Answers may vary here.

#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.

Question 6 – Calculate correlation coefficients

  1. Calculate the correlation coefficients for each pair of variables in #5. Use the argument 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
  1. Strictly based on the correlation coefficients, which explanatory variable is the better predictor of height?
    #Shoe length is a better predictor of height because of its higher coefficient, although not by much because the coefficients are similarily weak.

Question 7 – Creating and using a regression equation

  1. Create a linear model for height as the response variable with shoe length as a predictor variable.
lm(ShoeLength ~ Height, data = nscc_student_data)
## 
## Call:
## lm(formula = ShoeLength ~ Height, data = nscc_student_data)
## 
## Coefficients:
## (Intercept)       Height  
##      1.8288       0.1284
  1. Use that model to predict the height of someone who has a 10” shoelength
0.1284*10 + 1.8288
## [1] 3.1128
  1. Do you think that prediction is an accurate one? Explain why or why not.
    #no because not many people are roughly three foot tall unless they are children.

Question 8 – Poor Models

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.