ex p l a n a to ry va ri a b l e ad d i n g a n u m eri ca l · 2019-11-07 · ad d i n g a n u m...
TRANSCRIPT
![Page 1: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/1.jpg)
Adding a numericalexplanatory variable
M U LT I P L E A N D LO G I S T I C R E G R E S S I O N I N R
Ben BaumerInstructor
![Page 2: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/2.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
Adding a second numeric explanatory variableMathematical:
= + ⋅ gestation+ ⋅ age
Syntactical:
lm(bwt ~ gestation + age, data = babies)
bwt^ β̂0 β̂1 β̂2
![Page 3: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/3.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
No longer a 2D problem# doesn't work
ggplot(data = babies, aes(x = gestation, y = age, z = bwt)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)
![Page 4: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/4.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
Data space is 3Ddata_space <- ggplot(babies, aes(x = gestation, y = age)) +
geom_point(aes(color = bwt))
data_space
![Page 5: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/5.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
Tiling the plane
grid <- babies %>%
data_grid(
gestation = seq_range(gestation, by = 1),
age = seq_range(age, by = 1)
)
mod <- lm(bwt ~ gestation + age, data = babies)
bwt_hats <- augment(mod, newdata = grid)
![Page 6: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/6.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
Tiles in the data spacedata_space +
geom_tile(data = bwt_hats, aes(fill = .fitted, alpha = 0.5)) +
scale_fill_continuous("bwt", limits = range(babies$bwt))
![Page 7: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/7.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
3D visualizationplot_ly(data = babies, z = ~bwt, x = ~gestation, y = ~age, opacity = 0.6) %>%
add_markers(text = ~case, marker = list(size = 2)) %>%
add_surface(x = ~x, y = ~y, z = ~plane, showscale = FALSE,
cmax = 1, surfacecolor = color1, colorscale = col1)
![Page 8: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/8.jpg)
Let's practice!M U LT I P L E A N D LO G I S T I C R E G R E S S I O N I N R
![Page 9: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/9.jpg)
Conditionalinterpretation of
coef�cientsM U LT I P L E A N D LO G I S T I C R E G R E S S I O N I N R
Ben BaumerInstructor
![Page 10: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/10.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
Two slope coef�cients
lm(bwt ~ gestation + age, data = babies)
## Coefficients:
## (Intercept) gestation age
## -15.5226 0.4676 0.1657
![Page 11: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/11.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
Tiled planemodel_space
![Page 12: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/12.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
Tiled plane plus �rst slopemodel_space +
geom_hline(yintercept = 30, color = "red")
![Page 13: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/13.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
Tiled plane plus second slopemodel_space +
geom_vline(xintercept = 280, color = "red")
![Page 14: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/14.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
Coef�cient interpretation
lm(bwt ~ gestation + age, data = babies)
## Coefficients:
## (Intercept) gestation age
## -15.5226 0.4676 0.1657
![Page 15: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/15.jpg)
Let's practice!M U LT I P L E A N D LO G I S T I C R E G R E S S I O N I N R
![Page 16: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/16.jpg)
Adding a third(categorical) variable
M U LT I P L E A N D LO G I S T I C R E G R E S S I O N I N R
Ben BaumerInstructor
![Page 17: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/17.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
How could we forget about smoking?Mathematical:
= + ⋅ gestation+ ⋅ age+ ⋅ smoke
Syntactical:
lm(bwt ~ gestation + age + smoke, data = babies
bwt^ β̂0 β̂1 β̂2 β̂3
![Page 18: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/18.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
Geometry1 numeric + 1 categorical:
parallel lines
2 numeric:
a plane
2 numeric + 1 categorical:
parallel planes!
![Page 19: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/19.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
Drawing parallel planes in 3Dplot_ly(data = babies, z = ~bwt, x = ~gestation, y = ~age, opacity = 0.6) %>% add_markers(color = ~factor(smoke), text = ~case, marker = list(size = 2)) %> add_surface(x = ~x, y = ~y, z = ~plane0, showscale = FALSE, cmin = 0, cmax = 1, surfacecolor = color1, colorscale = col1) %>% add_surface(x = ~x, y = ~y, z = ~plane1, showscale = FALSE, cmin = 0, cmax = 1, surfacecolor = color2, colorscale = col1)
![Page 20: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/20.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
Coef�cient interpretationlm(bwt ~ gestation + age, data = babies)
## Coefficients:
## (Intercept) gestation age
## -15.5226 0.4676 0.1657
lm(bwt ~ gestation + age + smoke, data = babies)
## Coefficients:
## (Intercept) gestation age smoke
## -4.6037 0.4455 0.1069 -8.0143
![Page 21: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/21.jpg)
Let's practice!M U LT I P L E A N D LO G I S T I C R E G R E S S I O N I N R
![Page 22: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/22.jpg)
Higher dimensionsM U LT I P L E A N D LO G I S T I C R E G R E S S I O N I N R
Ben BaumerInstructor
![Page 23: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/23.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
Adding more variablesMathematical:
= + ⋅ gestation+ ⋅ age+ ⋅ smoke+
+ ⋅ height+ ⋅weight+ ⋅ parity
Syntactical:
lm(bwt ~ gestation + age + smoke + height + weight + parity, data = babies)
Syntactical (same model, but note order of coef�cients)
lm(bwt ~ . - case, data = babies)
bwt^ β̂0 β̂1 β̂2 β̂3
β̂4 β̂5 β̂6
![Page 24: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/24.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
Higher dimensional geometry(Parallel) hyperplanes, etc.
![Page 25: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/25.jpg)
MULTIPLE AND LOGISTIC REGRESSION IN R
Interpretation in large modelslm(bwt ~ gestation + age + smoke + height + weight + parity,
data = babies)
## Coefficients:
## (Intercept) gestation age smoke heig
## -80.41085 0.44398 -0.00895 -8.40073 1.154
## weight parity
## 0.05017 -3.32720
![Page 26: ex p l a n a to ry va ri a b l e Ad d i n g a n u m eri ca l · 2019-11-07 · Ad d i n g a n u m eri ca l ex p l a n a to ry va ri a b l e M U L T I P L E A N D L O G I S T I C R](https://reader033.vdocuments.mx/reader033/viewer/2022041514/5e2a00dc31871b02fe4ee6a6/html5/thumbnails/26.jpg)
Let's practice!M U LT I P L E A N D LO G I S T I C R E G R E S S I O N I N R