-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmedpolish-textbook.Rmd
119 lines (81 loc) · 2.39 KB
/
medpolish-textbook.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
---
title: "medpol"
author: "John D. Smith"
date: '2022-03-21'
output: md_document
---
## Libraries
```{r setup, include=FALSE}
source("https://raw.githubusercontent.com/smithjd/mpx/main/R/mp_fit.R")
source("https://raw.githubusercontent.com/smithjd/mpx/main/R/mp_plot.R")
source("https://raw.githubusercontent.com/smithjd/mpx/main/R/mp_rotate_plot.R")
library(tidyverse)
library(grid)
library(ggplotify)
library(cowplot)
# library(mpx)
theme_set(theme_light())
```
## First steps of a worked example
p 168 from Mosteller and Tukey, *Data Analysis and Regression, A Second Course in Statistics*, Reading, MA: Addison-Wesley, 1977.

## Median temperature data
Set up the data in R
```{r}
med_temp_tibble <- tibble(
month = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"),
Caribou = c(8.7, 9.8, 21.7, 34.7, 48.5, 58.4, 64.0),
Washington = c(36.2, 37.1, 45.3, 54.4, 64.7, 73.4, 77.3),
Laredo = c(57.6, 61.9, 68.4, 75.9, 81.2, 85.8, 87.7)
)
med_temp_array <- t(med_temp_tibble[,2:4])
dimnames(med_temp_array)[[2]] <- t(med_temp_tibble[,1])
med_temp_longer <- med_temp_tibble %>%
pivot_longer(cols = c("Caribou", "Washington", "Laredo"),
values_to = "temps" , names_to = "place") %>%
mutate(month = factor(month, levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"), ordered = TRUE))
array_row_names <- med_temp_array[,1]
```
## Techniques described by Hadley Wickham
Hadley Wickham, R for Data Science chapter on Exploratory Data Analysis: <https://r4ds.had.co.nz/exploratory-data-analysis.html>
### Variant number one
```{r}
med_temp_longer %>% ggplot(aes(month, y = place)) +
geom_point(aes(size = temps)) +
coord_flip()
```
### Variant number two
```{r}
med_temp_longer %>% ggplot(aes(month, y = place)) +
geom_tile(mapping = aes(fill = temps)) +
coord_flip()
```
## Save medpolish output in a list and demonstrate basic additivity plot
```{r}
lp <- medpolish(med_temp_array)
```
### Standard output
```{r}
lp
```
### Additivity plot
``` {plot(lp)}
```
## Augment *medpolish* output to produce an augmented plot
Aiming to reproduce the plot on p 176
```{r}
lp_out <- mp_fit(med_temp_longer,
temps,
place,
month)
```
### Graph the median polish fit
```{r}
lp_plot <- mp_plot(lp_out)
lp_plot
```
### Rotate the fit plot
```{r}
vp <- mp_rotate_plot(lp_plot)
pushViewport(vp)
```