library(tidyverse)
library(tidymodels)
library(DAAG)Lab 07
Import
allbacksExercise 1
ggplot(allbacks, aes(x = volume, y = weight)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "blue") +
labs(title = "Relationship between Volume and Weight",
x = "Volume",
y = "Weight") +
theme_minimal()Exercise 2 + 3
weight_fit <- lm(weight ~ volume, data = allbacks)
tidy(weight_fit)Intercept is 107.68 and the slope is 0.7086. It means that for each additional unit of volume, the weight of the allbacks increases by 0.7086 units.
Exercise 4
summary(weight_fit)$r.squared[1] 0.8026346
The R-squared value is 0.80263, it means that 80.26% of the variation in weight can be explained by the volume of the allbacks.
Exercise 5
ggplot(allbacks, aes(x = volume, y = weight, color = cover, shape = cover)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE, aes(group = cover)) +
geom_smooth(method = "lm", se = FALSE, color = "gray", linetype = "dashed") +
labs(
title = "Relationship Between Volume and Weight by Cover Type",
x = "Volume",
y = "Weight",
color = "Cover Type",
shape = "Cover Type"
) +
theme_minimal()Exercise 6 + 7 + 8
weight_cover_fit <- lm(weight ~ volume + cover, data = allbacks)
tidy(weight_cover_fit)Because coverhb service as a baseline category. And coverpb showes how it differs from the coverhb.
Intercept in this case is a baseline and slope showes how it differs from the baseline by each unit of volume.
Exercise 9
summary(weight_cover_fit)$r.squared[1] 0.9274776
It means that 0.9274 this prediction will be right in 92.74% of the cases.
Exercise 10
Second model is better because it has higher R-squared value. It means that 92.74% of the variation in weight can be explained by the volume and cover type of the allbacks.
Exercise 11
new_data <- data.frame(volume = 1000, cover = "hb")
predicted_weight <- predict(weight_cover_fit, newdata = new_data)
predicted_weight 1
915.9166