In order to learn ioslides, I want to create a very simple slideshow using a simple simulated dataframe about study time and exam scores.
In order to learn ioslides, I want to create a very simple slideshow using a simple simulated dataframe about study time and exam scores.
students <- data.frame( hours = runif(100, 1, 10) ) students$score <- 50 + 5 * students$hours + rnorm(100, 0, 8) head(students)
hours score 1 5.567304 74.63142 2 3.760917 66.12813 3 4.842169 85.15448 4 7.237919 103.29173 5 1.766224 62.87767 6 3.028930 71.43539
summary(students)
hours score Min. :1.130 Min. : 42.91 1st Qu.:3.132 1st Qu.: 63.74 Median :4.947 Median : 74.89 Mean :5.008 Mean : 75.41 3rd Qu.:6.652 3rd Qu.: 83.89 Max. :9.652 Max. :115.72
ggplot (Plot will be on the next slide)plot1 <- ggplot(students, aes(hours, score)) +
geom_point(size=0.5, color="tomato3") +
labs(title = "Study Hours vs. Exam Score",
x = "Hours Studied",
y = "Exam Score")
Here’s the equation that we could use: \[ Exam Score = \beta_0 + \beta_1 \cdot Study Hours \] We’re going to create a plot of the regression line, so let’s build the model using R.
model <- lm(score ~ hours, data=students) summary(model)
Call:
lm(formula = score ~ hours, data = students)
Residuals:
Min 1Q Median 3Q Max
-16.588 -6.136 1.481 6.038 15.866
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 48.4207 1.8405 26.31 <2e-16 ***
hours 5.3890 0.3334 16.16 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 7.743 on 98 degrees of freedom
Multiple R-squared: 0.7272, Adjusted R-squared: 0.7244
F-statistic: 261.2 on 1 and 98 DF, p-value: < 2.2e-16
`geom_smooth()` using formula = 'y ~ x'
We can analyze the regression line by using this equation: \[ \sum_{i=1}^{n}{(y_i - \bar{y})^2} \] This equation is used to find the variance.
plot2 <-plot_ly(data = students,
x = ~hours,
y = ~score,
type = "scatter",
mode = "markers") %>%
add_lines(x = ~hours, y = fitted(model),
name = "Regression Line") %>%
layout(title = "Interactive Study Hours vs. Exam Score",
xaxis = list(title = "Hours Studied"),
yaxis = list(title = "Exam Score"),
showlegend = FALSE)