In this assignment, you will get to put your newly acquired skills of regression modeling simply.
This assignment assumes that you have worked through all materials up to and including week 11.
Make sure you are familiar with this content.
In some questions, maybe, you need to search additional functionalities from tidyverse, tidymodels packages.
Note that these packages are also get loaded in your R Markdown environment when you Knit your R Markdown document.
Consider the given data set below
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.
State the dimension of the data set you have.
Which variables could be suitable for the response based on some other set of features.
Fit a simple linear regression model to predict Sales using Price. Provide an interpretation of the coefficient in the model and write out the model explicitly.
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
## [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
## [1] "Sales" "CompPrice" "Income" "Advertising" "Population"
## [6] "Price" "ShelveLoc" "Age" "Education" "Urban"
## [11] "US"
##
## Call:
## lm(formula = Sales ~ Price, data = car_df)
##
## Coefficients:
## (Intercept) Price
## 13.64192 -0.05307
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))).)
✏️️🧶 ✅ ⬆️ 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
## 2.5 % 97.5 %
## (Intercept) 12.3978438 14.88598655
## Price -0.0635995 -0.04254653
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
##
## 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 !
Now consider the same process over the splited data, not the whole sample.
What is data splitting context and why is it important?
How can you split your data into training and testing sets in R using the tidymodels package or any other alternative? Consider the 80-20 split rule for training and testing, respectively.
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
Once you have split your data, refit the previously considered simple linear or multiple linear regression models on training set only. Look at the summary of the model.
Use the predict() function in R to generate predictions for the testing data using the regression models you fitted. Evaluate their performance on the testing data using metrics such as RMSE or R-squared?
✏️️🧶 ✅ ⬆️ 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.
##
## 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
## R-squared: 0.2556286
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.
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:
Go to the Learn page related to HW assignment
Submit your pdf output file within the allocated submission slot
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 !
The homework is grades out of a 100 where:
Each exercise is marked out of different portions regarding the question difficulty.
If you have any questions about the assignment, please raise your voice during online lectures and send your inquiries via email.