data(cars)
head(cars)
##   speed dist
## 1     4    2
## 2     4   10
## 3     7    4
## 4     7   22
## 5     8   16
## 6     9   10
summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
x <- cars$speed
y <- cars$dist
n <- length(x)

Sxx <- sum((x - mean(x))^2)
Syy <- sum((y - mean(y))^2)
Sxy <- sum((x - mean(x)) * (y - mean(y)))
beta1 <- Sxy / Sxx
beta0 <- mean(y) - beta1 * mean(x)
regression_eq <- function(x) {
  beta0 + beta1 * x
}
# Plotting the data points
plot(x, y, main = "Cars dataset - Speed vs. Stopping Distance", xlab = "Speed", ylab = "Stopping Distance")

# Extracting slope and intercept from the regression equation function
slope <- beta1
intercept <- beta0

# Adding the regression line to the plot
abline(a = intercept, b = slope, col = "red")

Sxx
## [1] 1370
Syy
## [1] 32538.98
Sxy
## [1] 5387.4
beta1
## [1] 3.932409
beta0
## [1] -17.57909
# Load the dataset
data(cars)

# Check the structure of the dataset
str(cars)
## 'data.frame':    50 obs. of  2 variables:
##  $ speed: num  4 4 7 7 8 9 10 10 10 11 ...
##  $ dist : num  2 10 4 22 16 10 18 26 34 17 ...
# Make sure variable names are correct
speed <- cars$speed
distance <- cars$dist

# Fit linear model
lm_model <- lm(dist ~ speed, data = cars)

# Check summary of the model
summary(lm_model)
## 
## Call:
## lm(formula = dist ~ speed, data = cars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -29.069  -9.525  -2.272   9.215  43.201 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -17.5791     6.7584  -2.601   0.0123 *  
## speed         3.9324     0.4155   9.464 1.49e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 15.38 on 48 degrees of freedom
## Multiple R-squared:  0.6511, Adjusted R-squared:  0.6438 
## F-statistic: 89.57 on 1 and 48 DF,  p-value: 1.49e-12
# Predict stopping distance using manual coefficients
predicted_distance <- beta0 + beta1 * 8
predicted_distance
## [1] 13.88018
# Get observed stopping distance for 8 mph from dataset
observed_distance <- cars$dist[which(cars$speed == 8)]

# Calculate residual
residual <- observed_distance - predicted_distance
residual
## [1] 2.119825
data(faithful)
plot(faithful$eruptions, faithful$waiting, xlab = "Eruption Duration (mins)", ylab = "Waiting Time (mins)", main = "Waiting Times vs. Eruption Duration")

correlation <- cor(faithful$eruptions, faithful$waiting)
correlation
## [1] 0.9008112
lab_model <- lm(waiting ~ eruptions, data = faithful)
lab_model
## 
## Call:
## lm(formula = waiting ~ eruptions, data = faithful)
## 
## Coefficients:
## (Intercept)    eruptions  
##       33.47        10.73
summary(lab_model)
## 
## Call:
## lm(formula = waiting ~ eruptions, data = faithful)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -12.0796  -4.4831   0.2122   3.9246  15.9719 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  33.4744     1.1549   28.98   <2e-16 ***
## eruptions    10.7296     0.3148   34.09   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.914 on 270 degrees of freedom
## Multiple R-squared:  0.8115, Adjusted R-squared:  0.8108 
## F-statistic:  1162 on 1 and 270 DF,  p-value: < 2.2e-16
predicted_waiting_time <- predict(lab_model, newdata = data.frame(eruptions = 3.667))
predicted_waiting_time
##        1 
## 72.81999