set.seed(301)
n <- 50
study_hours <- runif(n, min = 1, max = 10)
exam_score <- 50 + 5 * study_hours + rnorm(n, sd = 8)
df <- data.frame(study_hours, exam_score)
fit <- lm(exam_score ~ study_hours, data = df)
p1 <- ggplot(df, aes(x = study_hours, y = exam_score)) +
geom_point(color = "#8C1D40", size = 2) +
geom_smooth(method = "lm", formula = y ~ x, color = "black", se = TRUE) +
labs(x = "Study Hours", y = "Exam Score",
title = "Exam Score vs. Study Hours") +
theme_minimal()
Since I don’t have a real dataset handy, I generated a fake but realistic one: study_hours between 1 and 10, and exam_score that goes up with study hours plus some random noise. Then lm() fits the model for us, and this same chunk builds the plot on the next slide.