library(tidyverse)
library(tidymodels)
library(DAAG)
Lab 07
Import
allbacks
Exercise 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
<- lm(weight ~ volume, data = allbacks)
weight_fit
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
<- lm(weight ~ volume + cover, data = allbacks)
weight_cover_fit
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
<- data.frame(volume = 1000, cover = "hb")
new_data <- predict(weight_cover_fit, newdata = new_data)
predicted_weight
predicted_weight
1
915.9166