library(tidyverse)
## ── Attaching packages ───────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1 ✔ purrr 0.3.2
## ✔ tibble 2.1.3 ✔ dplyr 0.8.3
## ✔ tidyr 0.8.3 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ──────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(Stat2Data)
library(broom)
data("PorschePrice")
model <- lm(Price ~ Mileage, data = PorschePrice)
model
##
## Call:
## lm(formula = Price ~ Mileage, data = PorschePrice)
##
## Coefficients:
## (Intercept) Mileage
## 71.0905 -0.5894
model %>%
tidy()
## # A tibble: 2 x 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 71.1 2.37 30.0 7.87e-23
## 2 Mileage -0.589 0.0566 -10.4 3.98e-11
obs_stat <- model %>%
tidy() %>%
filter(term == "Mileage") %>%
pull(statistic)
pt(obs_stat, df = nrow(PorschePrice) - 2) * 2
## [1] 3.981734e-11
model %>%
tidy(conf.int = TRUE)
## # A tibble: 2 x 7
## term estimate std.error statistic p.value conf.low conf.high
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 71.1 2.37 30.0 7.87e-23 66.2 75.9
## 2 Mileage -0.589 0.0566 -10.4 3.98e-11 -0.705 -0.473
t_star <- qt(0.025, df = nrow(PorschePrice) - 2, lower.tail = FALSE)
b1 <- model %>%
tidy() %>%
filter(term == "Mileage") %>%
pull(estimate)
stderr <- model %>%
tidy() %>%
filter(term == "Mileage") %>%
pull(std.error)
b1 + t_star * stderr
## [1] -0.4733618
b1 - t_star * stderr
## [1] -0.7054401
\(\beta_1 = 0\) \(\beta_1 \neq 0\)
We reject the null hypothesis!