2024-04-05

Introduction

  • Waiting vs. Eruptions
  • Faithful dataset
  • Importance of sample size and line of best fit
  • ggplotx2
  • plotlyx1

Waiting vs. Eruptions

model: \(\text{Waiting } = y = \beta_0 + \beta_1\cdot \text{eruptions} + \varepsilon\), where \(\varepsilon \sim \mathcal{N}(\mu=0, \sigma^2)\)

Fitted: \(\text{Waiting } = y = \hat{\beta}_0\) - \(\text{estimate of } \beta_0\) + \(\hat{\beta}_1 - \text{estimate of } \beta_1 \cdot \text{eruptions}\)

Datset faithful

##   eruptions waiting
## 1     3.600      79
## 2     1.800      54
## 3     3.333      74
## 4     2.283      62
## 5     4.533      85
## 6     2.883      55

ggplot of faithful [ggplot 1 of 2]

ggplot of faithful [ggplot 2 of 2]

As you can imagine, the slope and position of the line of best fit would be different between the two groups. Choosing the best sample for the model would make our linear function of “Waiting time” stronger. It is also important the assume that the randomness or noise of our data is normally distributed.

Linear model plot for faithful [plotly]

This is the R code for the previous slide

suppressMessages(library(plotly))
library(magrittr)

data(faithful)

mod = lm(waiting ~ eruptions, data=faithful)
x = faithful$eruptions;y = faithful$waiting

xax <- list(
  title = "Eruptions",
  titlefont = list(family="Modern Computer Roman")
)
yax <- list(
  title = "Waiting",
  titlefont = list(family="Modern Computer Roman")
)

fig <- plot_ly(x=x, y=y, type="scatter", mode="markers") %>%
   add_lines(x = x, y = fitted(mod)) %>%
   plotly::layout(xaxis = xax, yaxis = yax)