\[height = \beta_0 + \beta_1 sex \]
\[height = \beta_0 + \beta_1 medal \]
\[\Rightarrow height = \beta_0 + \beta_1 medal_1 + \beta_2 medal_2 \]
| medal1 | medal2 | |
|---|---|---|
| bronze | 0 | 0 |
| silver | 0 | 1 |
| gold | 1 | 0 |
# A tibble: 3 × 7
term estimate std.error statistic p.value conf.low conf.high
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 177. 0.107 1657. 0 177. 178.
2 medalGold 0.600 0.151 3.98 0.0000680 0.305 0.895
3 medalSilver 0.160 0.152 1.05 0.293 -0.138 0.458
\[weight = \beta_0 + \beta_1 sex + \beta_2 height \]
mod_sex <- lm(weight ~ sex, data = olympics)
mod_height <- lm(weight ~ height, data = olympics)
g1 <- broom::glance(mod_sex)
g2 <- broom::glance(mod_height)
g3 <- broom::glance(multi_mod)
rbind(g1, g2, g3)# A tibble: 3 × 12
r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 0.262 0.262 12.3 73761. 0 1 -818582. 1637171. 1.64e6
2 0.634 0.634 8.67 358247. 0 1 -740289. 1480585. 1.48e6
3 0.654 0.654 8.43 195183. 0 2 -734571. 1469151. 1.47e6
# ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>
# A tibble: 1 × 12
r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 0.645 0.645 8.55 186933. 0 2 -734938. 1469884. 1.47e6
# ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>
# A tibble: 3 × 7
term estimate std.error statistic p.value conf.low conf.high
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) -122. 0.316 -387. 0 -123. -122.
2 height 1.06 0.00180 590. 0 1.06 1.07
3 age 0.265 0.00347 76.3 0 0.258 0.272
a <- 10:70
h <- 120:220
new <- expand.grid(height = h, age = a, KEEP.OUT.ATTRS = FALSE)
new$weight <- predict(mod_age, newdata = new)
r <- reshape2::acast(new, age ~ height, value.var = "weight")
plotly::plot_ly(olympics, x = ~height, y = ~age, z = ~weight, type = "scatter3d", marker = list(size = 2), mode = "markers") |>
plotly::add_trace(x = h, y = a, z = r, type = "surface")