HW 02 - Regression Experiments

Due: 23 May, 23:59 TR time

In this assignment, you will get to put your newly acquired skills of regression modeling simply.

Prerequisites

Packages

library(tidyverse)
library(ggplot2)

Note that these packages are also get loaded in your R Markdown environment when you Knit your R Markdown document.

Real Data Example

Consider the given data set below

library(ISLR2)
car_df <- Carseats

You can view the dataset as a spreadsheet using the View() function. Note that you should not put this function in your R Markdown document, but instead type it directly in the Console, as it pops open a new window. When you run this in the console, you will see the following data viewer window pop up.

View(car_df)

Exercise 1 (15 pts)

Hint: Think about the summary() function if you used lm !

✏️️🧶 ✅ ⬆️ Write your answer in your R Markdown document , knit the document, make sure that your added R code chunk works well and you did not face with any knitting problem

#answersQ1:

#1
dim(car_df)
## [1] 400  11
#2
numeric_carseats <- subset(Carseats, select = sapply(Carseats, is.numeric))

correlation_matrix <- cor(numeric_carseats)
print(correlation_matrix)
##                   Sales   CompPrice       Income  Advertising   Population
## Sales        1.00000000  0.06407873  0.151950979  0.269506781  0.050470984
## CompPrice    0.06407873  1.00000000 -0.080653423 -0.024198788 -0.094706516
## Income       0.15195098 -0.08065342  1.000000000  0.058994706 -0.007876994
## Advertising  0.26950678 -0.02419879  0.058994706  1.000000000  0.265652145
## Population   0.05047098 -0.09470652 -0.007876994  0.265652145  1.000000000
## Price       -0.44495073  0.58484777 -0.056698202  0.044536874 -0.012143620
## Age         -0.23181544 -0.10023882 -0.004670094 -0.004557497 -0.042663355
## Education   -0.05195524  0.02519705 -0.056855422 -0.033594307 -0.106378231
##                   Price          Age    Education
## Sales       -0.44495073 -0.231815440 -0.051955242
## CompPrice    0.58484777 -0.100238817  0.025197050
## Income      -0.05669820 -0.004670094 -0.056855422
## Advertising  0.04453687 -0.004557497 -0.033594307
## Population  -0.01214362 -0.042663355 -0.106378231
## Price        1.00000000 -0.102176839  0.011746599
## Age         -0.10217684  1.000000000  0.006488032
## Education    0.01174660  0.006488032  1.000000000
cor_with_sales <- correlation_matrix["Sales", ]
strong_correlations <- cor_with_sales[abs(cor_with_sales) > 0.5]
print(strong_correlations)
## Sales 
##     1
colnames(car_df)
##  [1] "Sales"       "CompPrice"   "Income"      "Advertising" "Population" 
##  [6] "Price"       "ShelveLoc"   "Age"         "Education"   "Urban"      
## [11] "US"
model <- lm(Sales ~ Price, data = car_df)

print(model)
## 
## Call:
## lm(formula = Sales ~ Price, data = car_df)
## 
## Coefficients:
## (Intercept)        Price  
##    13.64192     -0.05307
# fitted model:
# Sales=13.64192−0.05307×Price

Exercise 2 (25 pts)

  • Interpret the ploting results for model diagnostics (Using the plot command, comment on the validity of the assumption of the model

(Hint: Note before using the plot command you may wish to specify a 2x2 graphics window using par(mfrow = c(2, 2))).)

  • Using the fitted model, obtain \(95\%\) confidence interval for the coefficient(s).

✏️️🧶 ✅ ⬆️ Write your answer in your R Markdown document , knit the document, make sure that your added R code chunk works well and you did not face with any knitting problem

# Your plotting code here

#1
par(mfrow = c(2, 2))
plot(model)

#2

conf_interval <- confint(model)

print(conf_interval)
##                  2.5 %      97.5 %
## (Intercept) 12.3978438 14.88598655
## Price       -0.0635995 -0.04254653

Exercise 3 (30 pts)

Fit a multiple regression model to predict Sales using Price, Urban, and US. Provide an interpretation of each coefficient in the model. Be careful-some of the variables in the model are qualitative!

✏️️🧶 ✅ ⬆️ Write your answer in your R Markdown document , knit the document, make sure that your added R code chunk works well and you did not face with any knitting problem

set.seed(27058)
model <- lm(Sales ~ Price + Urban + US, data = car_df)
summary(model)
## 
## Call:
## lm(formula = Sales ~ Price + Urban + US, data = car_df)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.9206 -1.6220 -0.0564  1.5786  7.0581 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 13.043469   0.651012  20.036  < 2e-16 ***
## Price       -0.054459   0.005242 -10.389  < 2e-16 ***
## UrbanYes    -0.021916   0.271650  -0.081    0.936    
## USYes        1.200573   0.259042   4.635 4.86e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.472 on 396 degrees of freedom
## Multiple R-squared:  0.2393, Adjusted R-squared:  0.2335 
## F-statistic: 41.52 on 3 and 396 DF,  p-value: < 2.2e-16

WARNING: WHEN I RUN YOUR .Rmd file in my computer, in case of any necessity, I need to get the same results that you mentioned in your PDF report !

Exercise 4 (30 pts)

Now consider the same process over the splited data, not the whole sample.

Hint: You can check this website for tidymodels https://rsample.tidymodels.org/reference/initial_split.html

or feel free to use other options that we discussed before

✏️️🧶 ✅ ⬆️ Write your answer in your R Markdown document , knit the document, make sure that your added R code chunk works well and you did not face with any knitting problem

#-1 data splits is critical for ML and statistical modeling by dividing data into two or more subsets for purpose of traning models and evualting predictive models for multiple reasons.

chooseCRANmirror(ind = 1)
install.packages("rsample")
## 
## The downloaded binary packages are in
##  /var/folders/0l/0n3rw26n0p53c1lcs4jqddsr0000gn/T//RtmpFWDPHQ/downloaded_packages
library(rsample)

split <- initial_split(car_df, prop = 0.8)

train_data <- training(split)
test_data <- testing(split)

lm_model <- lm(Sales ~ Price, data = train_data)

summary(lm_model)
## 
## Call:
## lm(formula = Sales ~ Price, data = train_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.5202 -1.8251 -0.2093  1.6791  7.5432 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 13.498128   0.720805  18.726  < 2e-16 ***
## Price       -0.051863   0.006105  -8.496  7.7e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.571 on 318 degrees of freedom
## Multiple R-squared:  0.185,  Adjusted R-squared:  0.1824 
## F-statistic: 72.18 on 1 and 318 DF,  p-value: 7.696e-16
predictions <- predict(lm_model, newdata = test_data)

rmse <- sqrt(mean((test_data$Sales - predictions)^2))

rsquared <- cor(predictions, test_data$Sales)^2

cat("RMSE:", rmse, "\n")
## RMSE: 2.373974
cat("R-squared:", rsquared, "\n")
## R-squared: 0.2556286

Review your work

After answering each of the exercise, take a moment to review your work and to check that your code is clear and understandable. A proportion of the homework assignment marks are attributable to how you present your work, both code and written. This is a good practice for getting a new habit on writing codes.

When reviewing your code, add new lines, tabs and spaces where appropriate to help aid the reader to understand what your code is doing. Even if the exercise does not explicitly ask you to write some text, it is also good practice to add a short sentence to be explicit about what is your answer to the question.

Submission

Once you have answered all of the exercises, please make sure that you have knitted your document and got the html output correctly.

To submit your homework:

  1. Go to the Learn page related to HW assignment

  2. Submit your pdf output file within the allocated submission slot

  3. Make sure that the knitted html document presents the anticipated output before completing your submission. After getting your html, you can simply save your file as pdf after opening it in a separate html window.

IMPORTANT: You must only submit the knitted html - converted pdf document! If you face with any issue regarding your submission let us know asap !

How the homework graded

The homework is grades out of a 100 where: