{r} library ( MASS ) library ( ISLR2 )
{r} head ( Boston )
```{r} library(MASS) data(“Boston”)
lm_fit <- lm(medv ~ lstat, data = Boston) summary(lm_fit)
library(MASS) # Load the MASS package
data(“Boston”) # Load the Boston dataset
lm_fit <- lm(medv ~ lstat, data = Boston) # Use the correct tilde (~)
attach(Boston) # Attach the dataset (optional, but not recommended) lm_fit <- lm(medv ~ lstat) # Use a normal tilde (~) lm_fit <- lm(medv ~ lstat)
```{r}
lm_fit
{r} summary ( lm_fit )
{r} names ( lm_fit )
{r} coef ( lm_fit )
{r} confint ( lm_fit )
```{r} # Load necessary library and dataset library(MASS) data(“Boston”)
lm_fit <- lm(medv ~ lstat, data = Boston)
predict(lm_fit, newdata = data.frame(lstat = c(5, 10, 15)))
```{r}
predict(lm_fit, newdata = data.frame(lstat = c(5, 10, 15)))
{r} plot ( lstat , medv )
```{r} # Load necessary library and dataset library(MASS) data(“Boston”)
lm_fit <- lm(medv ~ lstat, data = Boston)
plot(Boston\(lstat, Boston\)medv, main = “LSTAT vs MEDV with Regression Line”, xlab = “LSTAT”, ylab = “MEDV”, pch = 16, col = “blue”)
abline(lm_fit, col = “red”, lwd = 2)
```{r}
# Load necessary library and dataset
library(MASS)
data("Boston")
# Fit the linear model
lm_fit <- lm(medv ~ lstat, data = Boston)
# Create a scatter plot first
plot(Boston$lstat, Boston$medv,
main = "LSTAT vs MEDV with Regression Line",
xlab = "LSTAT", ylab = "MEDV",
pch = 16, col = "blue")
# Now add the regression line
abline(lm_fit, col = "red", lwd = 3) # Make line thicker with lwd = 3
abline ( lm_fit , lwd = 3 , col = " red " )
plot ( lstat , medv , col = " red " )
plot ( lstat , medv , pch = 20)
plot ( lstat , medv , pch = " + " )
plot (1:20 , 1:20 , pch = 1:20)
{r} par ( mfrow = c (2 , 2) ) plot ( lm_fit )
{r} plot ( predict ( lm_fit ) , residuals ( lm_fit ) ) plot ( predict ( lm_fit ) , rstudent ( lm_fit ) )
{r} plot ( hatvalues ( lm_fit ) ) which.max(hatvalues(lm_fit))
```{r} # Load the necessary package library(MASS)
lm_fit <- lm(medv ~ lstat + age, data = Boston)
summary(lm_fit)
```{r}
# Load necessary package
library(MASS)
# Fit the linear model using all predictors in the dataset
lm_fit <- lm(medv ~ ., data = Boston)
# Display the summary of the model
summary(lm_fit)
{r} library ( car ) vif ( lm_fit )
```{r} # Load necessary package library(MASS)
lm_fit1 <- lm(medv ~ . - age, data = Boston)
summary(lm_fit1)
```{r}
# Load necessary package
library(MASS)
# Load the Boston dataset
data("Boston")
# Fit and summarize the linear model with interaction
summary(lm(medv ~ lstat * age, data = Boston))
```{r} # Load necessary package library(MASS)
data(“Boston”)
lm_fit2 <- lm(medv ~ lstat + I(lstat^2), data = Boston)
summary(lm_fit2)
```{r}
# Load necessary package
library(MASS)
# Load the dataset
data("Boston")
# Fit the simple linear model
lm_fit <- lm(medv ~ lstat, data = Boston)
# Fit the quadratic model
lm_fit2 <- lm(medv ~ lstat + I(lstat^2), data = Boston)
# Compare models using ANOVA
anova(lm_fit, lm_fit2)
{r} par ( mfrow = c (2 , 2) ) plot ( lm_fit2 )
```{r} # Load necessary package library(MASS)
data(“Boston”)
lm_fit5 <- lm(medv ~ poly(lstat, 5), data = Boston)
summary(lm_fit5)
```{r}
summary(lm(medv ~ log(rm), data = Boston))
{r} head ( Carseats )
{r} lm_fit <- lm(Sales ~ . + Income:Advertising + Price:Age, data = Carseats) summary ( lm_fit )
{r} attach ( Carseats ) contrasts ( ShelveLoc )