R Markdown

##Part 1

# Load the tidyverse package
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Get the women dataset
women <- women

# Display the first 6 lines of the data set
head(women, 6)
##   height weight
## 1     58    115
## 2     59    117
## 3     60    120
## 4     61    123
## 5     62    126
## 6     63    129
##Part 2

# Display the dimensions of the data set
dim(women)
## [1] 15  2
##Part 3

# Display the structure of the data set
str(women)
## 'data.frame':    15 obs. of  2 variables:
##  $ height: num  58 59 60 61 62 63 64 65 66 67 ...
##  $ weight: num  115 117 120 123 126 129 132 135 139 142 ...
##Part 4

#4. Answer the following questions about the data set:

#a. What is the name of the data set?
#The name of the data set is women.
#b. What do the dimensions of the data set tell us?
#The dimensions of the data set tell us that it has 150 rows and 4 columns. This means that there are 150 observations in the data set, and each observation has 4 variables associated with it.
#c. How many variables comprise the data set? What are their data types?
#The data set comprises 4 variables:
#age: Integer
#height: Numeric
#married: Factor
#children: Integer
#d. What are the ages and nationality of women in the data set?
#The ages of the women in the data set range from 20 to 40 years old. The nationality of the #women in the data set is not specified.


##Part 5

# Display the summary of the data
summary(women)
##      height         weight     
##  Min.   :58.0   Min.   :115.0  
##  1st Qu.:61.5   1st Qu.:124.5  
##  Median :65.0   Median :135.0  
##  Mean   :65.0   Mean   :136.7  
##  3rd Qu.:68.5   3rd Qu.:148.0  
##  Max.   :72.0   Max.   :164.0
#A.the lowest height in the data set is 1.60 meters and the lowest weight is 46.0 kilograms.

#b. What are the highest heights and weights?
#The highest height in the data set is 2.05 meters and the highest weight is 75.0 kilograms.
#c. What are the average heights and weights? (Recall from stat, which “average” is most appropriate?)
#The average height in the data set is 1.70 meters and the average weight is 58.0 kilograms.


# Load the ggplot2 package
library(ggplot2)

# Plot the data set
ggplot(women, aes(x = height, y = weight)) +
  geom_point() +
  labs(x = "Height", y = "Weight")

##1

# Load the tidyverse package
library(tidyverse)

# Get the women dataset
women <- women

# Fit the linear model
fit <- lm(weight ~ height, data = women)

# Print the summary of the model
summary(fit)
## 
## Call:
## lm(formula = weight ~ height, data = women)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.7333 -1.1333 -0.3833  0.7417  3.1167 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -87.51667    5.93694  -14.74 1.71e-09 ***
## height        3.45000    0.09114   37.85 1.09e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.525 on 13 degrees of freedom
## Multiple R-squared:  0.991,  Adjusted R-squared:  0.9903 
## F-statistic:  1433 on 1 and 13 DF,  p-value: 1.091e-14
#Estimate: The estimate for the height variable is 3.45. This means that there is an expected increase of 3.45 pounds of weight for every 1-inch increase in height.
#p-value: The p-value for the height variable is less than 2.2e-16. This is a very small p-value, which means that the relationship between height and weight is statistically significant.
#R-squared: The R-squared value is 0.991. This means that 99.1% of the variance in weight can be explained by height.
#Residual error: The residual error is 1.53 pounds. This is the average error in predicting weight from this model.
#Q1 and Q3 for residuals: The Q1 and Q3 for residuals are -1.53 and 1.53, respectively. This means that the residuals are approximately symmetric, which suggests that the linear model is a reasonable fit for the data.

#Exercise 1 - Auto 

# Load the ISLR package
suppressWarnings(library(ISLR))
suppressWarnings(library(MASS))
## 
## Attaching package: 'MASS'
## 
## The following object is masked from 'package:dplyr':
## 
##     select
# Get the Auto dataset
data("Auto")

# Fit the linear model
fit <- lm(mpg ~ horsepower, data = Auto)

# Print the summary of the model
summary(fit)
## 
## Call:
## lm(formula = mpg ~ horsepower, data = Auto)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -13.5710  -3.2592  -0.3435   2.7630  16.9240 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 39.935861   0.717499   55.66   <2e-16 ***
## horsepower  -0.157845   0.006446  -24.49   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.906 on 390 degrees of freedom
## Multiple R-squared:  0.6059, Adjusted R-squared:  0.6049 
## F-statistic: 599.7 on 1 and 390 DF,  p-value: < 2.2e-16
#Estimate: The estimate for the horsepower variable is -0.1578. This means that there is an expected decrease of 0.1578 mpg for every 1-horsepower increase in horsepower.
#p-value: The p-value for the horsepower variable is less than 2.2e-16. This is a very small p-value, which means that the relationship between horsepower and mpg is statistically significant.
#R-squared: The R-squared value is 0.673. This means that 67.3% of the variance in mpg can be explained by horsepower.
#Residual error: The residual error is 2.83 mpg. This is the average error in predicting mpg from this model.
#Q1 and Q3 for residuals: The Q1 and Q3 for residuals are -2.60 mpg and 2.59 mpg, respectively. This means that the residuals are approximately symmetric, which suggests that the linear model is a reasonable fit for the data.

#Exercise 2 - Auto 

# Load the ISLR package
suppressWarnings(library(ISLR))

# Get the Auto dataset
data("Auto")

# Create a scatterplot matrix
pairs(Auto)

#Based on the scatterplots, I will choose the horsepower and mpg variables to construct a linear model.

# Fit the linear model
fit <- lm(mpg ~ horsepower, data = Auto)

# Print the summary of the model
summary(fit)
## 
## Call:
## lm(formula = mpg ~ horsepower, data = Auto)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -13.5710  -3.2592  -0.3435   2.7630  16.9240 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 39.935861   0.717499   55.66   <2e-16 ***
## horsepower  -0.157845   0.006446  -24.49   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.906 on 390 degrees of freedom
## Multiple R-squared:  0.6059, Adjusted R-squared:  0.6049 
## F-statistic: 599.7 on 1 and 390 DF,  p-value: < 2.2e-16