To fit a simple linear regression model to our data, we can use R’s lm() function after we load in our data into a data frame.
## Load data
data <- data.frame(
TestScores = c(60, 65, 70, 75, 80, 85, 88, 90, 92, 95, 96, 97, 98, 99, 100),
HoursReading = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
)
# Fit the linear model
model <- lm(TestScores ~ HoursReading, data = data)
summary(model)
##
## Call:
## lm(formula = TestScores ~ HoursReading, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.4 -2.9 0.2 3.3 4.8
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 63.600 2.146 29.64 2.54e-13 ***
## HoursReading 2.800 0.236 11.86 2.40e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.95 on 13 degrees of freedom
## Multiple R-squared: 0.9154, Adjusted R-squared: 0.9089
## F-statistic: 140.7 on 1 and 13 DF, p-value: 2.399e-08