R Markdown

Install and load ISLR2 package (if not already installed)

if (!requireNamespace(“ISLR2”, quietly = TRUE)) { install.packages(“ISLR2”) } library(ISLR2)

Function to load and preview dataset

load_data <- function(dataset_name) { data(dataset_name, package=“ISLR2”) head(get(dataset_name)) }

Load and preview Auto dataset

load_data(“Auto”)

Simple Linear Regression on Auto dataset

auto_model <- lm(mpg ~ horsepower, data = Auto) summary(auto_model)

Scatter plot with regression line

plot(Auto\(horsepower, Auto\)mpg, main=“Horsepower vs MPG”, xlab=“Horsepower”, ylab=“MPG”, pch=16) abline(auto_model, col=“blue”, lwd=2)

Load and preview Carseats dataset

load_data(“Carseats”)

Multiple Linear Regression on Carseats dataset

carseats_model <- lm(Sales ~ Price + Urban + US, data = Carseats) summary(carseats_model)

Interpretation of coefficients:

- Price: Impact of price on sales

- Urban: Influence of being in an urban area

- US: Effect of being in the U.S.