library(ggplot2)
library(plotly)
midpred.lm <- lm(FinalExam ~ Midterm + I(Midterm^2) + I(Midterm^3), data = M425P2)
b <- coef(midpred.lm)
predintv <- predict(midpred.lm, data.frame(Midterm = 68), interval = "prediction")
confintv <- predict(midpred.lm, data.frame(Midterm = 68), interval = "confidence")
midterm.ggplot <- ggplot(M425P2, aes(y = FinalExam, x = Midterm, color = FinalExam)) +
geom_point(pch = 16, bg = "white", size = 3) +
stat_function(fun=function(x) b[1] + b[2]*x + b[3]*x^2 + b[4]*x^3, color = "seagreen3", size = 1.5) +
scale_color_gradient(low = "cadetblue1", high = "cadetblue4") +
labs(
title = "How Well Does My Midterm Predict My Final Exam?<br><sup>Let's Goooo<sup>",
x = "Past Midterm Scores (2019-2024)",
y = "Past Final Exam Scores (2019-2024)"
) +
ylim(min(M425P2$FinalExam) - 5, max(M425P2$FinalExam) + 5) +
theme_minimal() +
geom_point(
aes(x = 68, y = predict(midpred.lm, data.frame(Midterm = 68))),
color = "firebrick", size = 4
) +
geom_text(
aes(x = 68, y = predict(midpred.lm, data.frame(Midterm = 68)), label = "Predicted Final Exam Score"),
color = "firebrick", nudge_x = -11.25, size = 3
) +
geom_segment(aes(x=68, xend=68, y=confintv[2], yend=confintv[3]),
alpha=0.04, color="goldenrod2", lwd=2) +
geom_segment(aes(x=68, xend=68, y=predintv[2], yend=predintv[3]),
alpha=0.009, color="firebrick", lwd=1.5)
ggplotly(midterm.ggplot, tooltip = c("x", "y"))