library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
hours_studied <- c(2, 3, 5, 7, 9, 10, 12, 15, 16, 18)
exam_scores <- c(45, 50, 60, 70, 80, 85, 90, 95, 100, 110)
ggplot(data = data.frame(hours_studied = hours_studied, exam_scores = exam_scores),
aes(x = hours_studied, y = exam_scores)) +
geom_point() +
geom_smooth(method = "lm", color = "blue") +
labs(x = "Hours Studied", y = "Exam Scores") +
ggtitle("Regression Line")
## `geom_smooth()` using formula = 'y ~ x'

reg_model <- lm(exam_scores ~ hours_studied)
summary(reg_model)
##
## Call:
## lm(formula = exam_scores ~ hours_studied)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.049 -2.825 -0.479 2.429 5.337
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 40.8910 2.3699 17.25 1.30e-07 ***
## hours_studied 3.8772 0.2148 18.05 9.12e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.57 on 8 degrees of freedom
## Multiple R-squared: 0.976, Adjusted R-squared: 0.973
## F-statistic: 325.7 on 1 and 8 DF, p-value: 9.116e-08