-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
281 lines (251 loc) · 14.3 KB
/
README.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
```{r setup,include=FALSE}
# set the knitr options ... for everyone!
# if you unset this, then vignette build bonks. oh, joy.
#opts_knit$set(progress=TRUE)
opts_knit$set(eval.after='fig.cap')
# for a package vignette, you do want to echo.
# opts_chunk$set(echo=FALSE,warning=FALSE,message=FALSE)
opts_chunk$set(warning=FALSE,message=FALSE)
#opts_chunk$set(results="asis")
opts_chunk$set(cache=TRUE,cache.path="cache/")
#opts_chunk$set(fig.path="figure/",dev=c("pdf","cairo_ps"))
opts_chunk$set(fig.path="tools/figure/",dev=c("png"))
opts_chunk$set(fig.width=7,fig.height=6,dpi=100,out.width='700px',out.height='600px')
# doing this means that png files are made of figures;
# the savings is small, and it looks like shit:
#opts_chunk$set(fig.path="figure/",dev=c("png","pdf","cairo_ps"))
#opts_chunk$set(fig.width=4,fig.height=4)
# for figures? this is sweave-specific?
#opts_knit$set(eps=TRUE)
# this would be for figures:
#opts_chunk$set(out.width='.8\\textwidth')
# for text wrapping:
options(width=124,digits=2)
opts_chunk$set(size="small")
#opts_chunk$set(tidy=TRUE,tidy.opts=list(width.cutoff=50,keep.blank.line=TRUE))
library(ggplot2)
library(SPYvsSPY)
library(dplyr)
```
# SPYvsSPY
[![Build Status](https://github.com/shabbychef/SPYvsSPY/workflows/R-CMD-check/badge.svg)](https://github.com/shabbychef/SPYvsSPY/actions)
[![CRAN](http://www.r-pkg.org/badges/version/SPYvsSPY)](https://cran.r-project.org/package=SPYvsSPY)
[![Downloads](http://cranlogs.r-pkg.org/badges/SPYvsSPY?color=green)](http://www.r-pkg.org/pkg/SPYvsSPY)
[![Total](http://cranlogs.r-pkg.org/badges/grand-total/SPYvsSPY?color=green)](http://www.r-pkg.org/pkg/SPYvsSPY)
```{r spyplot,cache=TRUE,eval=TRUE,echo=FALSE,dpi=200,out.width='600px',out.height='500px'}
library(dplyr)
library(tidyr)
library(tibble)
library(ggplot2)
# see http://undocumentedmatlab.com/blog/spy-easter-egg-take-2
spyguy <- tibble::tribble(~x,~yv,
1,"44",
2,"43,44,45,46,47",
3,"42,43,45,46,47,48",
4,"41,42,45,46,47,48,49",
5,"41,44,45,46,47,48,49",
6,"40,41,44,45,46,47,48",
7,"39,40,43,44,45,46,47,48",
8,"38,39,43,44,45,46,47,48",
9,"37,38,42,43,44,45,46,47,48",
10,"36,37,42,43,44,45,46,47",
11,"36,42,43,44,45,46,47,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",
12,"35,36,41,42,43,44,45,46,47,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",
13,"34,35,41,42,43,44,45,46,47,70,71,72,73,74,75,76,77,78,79,80,81,82",
14,"33,34,41,42,43,44,45,46,65,66,67,68,69,70,71,72,80,81,82,83,84,85,86,87,88",
15,"32,33,40,41,42,43,44,45,46,61,62,63,64,65,66,67,87,88,89,90,91,92",
16,"31,32,40,41,42,43,44,45,46,57,58,59,60,61,62,63,91,92,93,94,95",
17,"31,32,40,41,42,43,44,45,55,56,57,58,59,60,95,96,97,98",
18,"8,9,10,11,30,31,39,40,41,42,43,44,45,53,54,55,56,97,98,99,100",
19,"7,8,9,13,14,15,16,17,18,19,20,29,30,39,40,41,42,43,44,45,51,52,53,99,100",
20,"6,7,8,19,20,21,22,23,24,25,26,27,28,29,39,40,41,42,43,44,45,49,50,100",
21,"6,7,24,25,26,27,28,38,39,40,41,42,43,44,47,48,99,100",
22,"5,6,24,25,26,27,38,39,40,41,42,43,44,45,89,99,100",
23,"4,5,23,24,25,26,27,37,38,39,40,41,42,43,89,99",
24,"3,4,5,6,23,24,25,26,37,38,39,40,41,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,98,99",
25,"3,4,5,6,7,22,23,24,25,26,37,38,39,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,98",
26,"2,3,4,5,6,7,8,21,22,23,24,25,36,37,38,39,55,56,57,58,59,60,61,88,97,98",
27,"1,2,3,4,5,6,7,8,9,10,21,22,23,24,25,36,37,38,51,52,53,54,55,59,60,87,97,98",
28,"1,2,20,21,22,23,24,25,36,37,38,58,59,87,97",
29,"1,20,21,22,23,24,35,36,37,38,39,40,41,58,86,96,97",
30,"2,19,20,21,22,23,24,35,36,39,40,41,42,43,44,45,46,85,86,96",
31,"2,3,4,19,20,21,22,23,24,35,36,44,45,46,47,48,84,85,95,96",
32,"2,3,4,5,6,18,19,20,21,22,23,35,36,37,47,48,49,50,51,83,84,95",
33,"4,5,6,7,18,19,20,21,22,23,34,35,36,37,38,51,52,53,54,82,83,84,85,94,95",
34,"6,7,8,9,17,18,19,20,21,22,23,34,35,39,52,53,54,55,56,81,85,86,87,93,94",
35,"7,8,9,10,17,18,19,20,21,22,34,35,46,47,48,49,50,53,54,55,56,57,58,80,81,87,88,93,94",
36,"9,10,11,12,16,17,18,19,20,21,22,33,34,35,36,37,38,45,46,53,54,55,56,57,58,59,60,61,79,80,81,88,89,92,93",
37,"10,11,12,13,14,16,17,18,19,20,21,22,33,34,35,36,37,38,39,40,45,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,82,83,89,92",
39,"14,15,16,17,18,19,20,21,33,35,36,37,38,39,40,41,42,47,53,81,82,85,86,91",
40,"15,16,17,18,19,20,21,33,36,37,40,41,42,43,47,48,52,56,59,80,81,82,83,87,90,91",
41,"17,18,19,20,32,33,36,37,41,42,43,44,48,49,52,56,57,60,63,80,81,83,84,85,88,89,90",
42,"19,20,21,32,33,36,37,38,41,42,43,44,49,50,51,52,56,57,60,64,67,79,80,85,86,87,88,89,90",
43,"20,21,32,36,37,38,39,42,43,44,45,50,51,52,58,61,64,68,69,70,78,79,80",
44,"20,21,31,32,36,37,38,39,40,41,43,44,45,52,56,58,61,65,68,71,72,73,74,75,76,77,78,79",
45,"21,31,32,36,37,38,39,40,41,42,43,44,45,46,52,56,58,61,65,71,72,73,74,75",
46,"21,31,32,37,38,39,40,41,42,43,44,45,46,52,57,58,61,62,66,70,71,72,73,74,75,76",
47,"21,22,31,34,35,36,37,38,39,40,41,42,43,44,45,46,51,52,58,59,62,66,70,71,74,75,76,77",
48,"21,22,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,52,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,75,76,77,78",
49,"21,22,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,53,54,55,56,59,60,61,62,63,64,65,66,67,68,69,77,78,79",
50,"21,22,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,54,55,56,57,60,61,62,78,79,80,81,83",
51,"21,22,30,31,32,33,34,35,37,38,39,40,41,42,43,44,45,46,47,54,55,79,80,81,82,83,84,85,86,87,88,89",
52,"21,22,30,31,32,33,34,35,38,39,40,41,42,43,44,45,46,47,54,55,77,78,79,80,84,85,86,87,88,89,90,91",
53,"22,30,31,32,33,34,35,36,39,40,42,43,44,45,46,47,54,55,76,77,78,82,83,84,91,92",
54,"22,29,30,31,32,33,34,35,36,39,40,43,44,45,46,54,55,75,76,81,82,92,93,94",
55,"22,29,30,31,32,33,34,35,36,37,40,41,55,73,74,75,79,80,86,87,88,89,90,91,92,93,94,95,96,97",
56,"22,23,29,30,31,32,33,34,35,36,37,38,39,40,41,55,72,73,74,78,79,85,86,94,95,96,97,108",
57,"22,23,29,30,31,32,33,34,35,36,37,38,39,40,41,42,55,73,74,78,83,84,90,91,92,93,94,95,96,97,98,107,108",
58,"22,23,29,30,31,32,33,34,35,36,37,38,39,40,41,42,55,56,73,74,75,76,82,83,89,90,98,99,107",
59,"22,23,28,29,30,31,32,33,34,36,37,38,39,40,41,42,55,56,74,75,76,77,81,82,88,89,99,100,105,106",
60,"22,23,28,29,30,31,32,33,34,37,38,39,40,41,42,55,56,75,76,77,87,88,93,94,95,96,97,98,99,100,104,105",
61,"23,28,29,30,31,32,33,38,39,40,41,42,55,56,76,77,86,87,92,93,94,95,96,97,98,103,104",
62,"22,23,28,29,30,31,32,33,39,40,41,55,56,76,77,85,90,91,92,102,103",
63,"22,23,28,29,30,31,32,33,40,41,55,56,77,78,79,80,81,90,91,92,93,94,95,96,97,98,99,101,102",
64,"22,23,28,29,30,31,32,41,42,55,56,79,80,82,91,92,96,97,98,99,101",
65,"22,23,24,28,29,30,31,32,41,42,56,81,83,84,85,90,92,96,100,101",
66,"22,23,24,27,28,29,30,31,32,42,43,56,83,84,85,96,100",
67,"22,23,24,27,28,29,30,31,42,43,44,56,85,86,87,96,100",
68,"23,24,27,28,29,30,31,42,43,44,56,87,88,89,90,91,92,93,94,95,96,97,98,99,100",
69,"23,24,27,28,29,30,43,44,45,56,95,96,97,98,99",
70,"23,24,27,28,29,30,44,45,56",
71,"23,24,27,28,29,44,45,46,56",
72,"23,24,26,27,28,29,45,46,47,56,57",
73,"23,24,26,27,28,46,47,56,57",
74,"23,24,25,26,27,28,46,47,48,57",
75,"23,24,25,26,27,47,48,57",
76,"23,24,25,26,27,48,49,57",
77,"23,24,25,26,49,50,57",
78,"24,25,26,49,50,57",
79,"24,25,50,51,57",
80,"24,25,51,57",
81,"24,51,52,57",
82,"24,52,53,57",
83,"53,57",
84,"53,54,57",
85,"54,57",
86,"55,58",
87,"56,58",
88,"56,57,58",
89,"57,58",
90,"58",
91,"58") %>%
group_by(x) %>%
summarize(y=strsplit(yv,',')) %>%
ungroup() %>%
tidyr::unnest() %>%
mutate(y=as.numeric(y))
ph <- spyguy %>%
mutate(black_spy=runif(length(x)) > 0.5) %>%
mutate(x=ifelse(black_spy,x,-x)) %>%
mutate(black_spy=factor(ifelse(black_spy,'black spy','white spy'))) %>%
arrange(x,y) %>%
ggplot(aes(x=x,y=-y,color=black_spy)) +
facet_grid(.~black_spy,scales='free') +
scale_color_grey(start=0.05,end=0.99) +
labs(color='spy') +
geom_jitter() +
theme(panel.background = element_rect(fill = "grey70", colour = NA),
legend.key = element_rect(fill = "grey70", colour = NA),
strip.text = element_text(colour = "grey50", size = rel(0.8)),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())
print(ph)
```
_Joke and Data_
Data on the 248 installments of the Spy vs Spy comic by Antonio Prohias
appearing in Mad Magazine: date, issue number, outcome, appearance of
Grey Spy, V for Victory. Not affiliated or endorsed by the Estate of
Antonio Prohias or Mad Magazine, a fan-curated dataset for the promotion
of Spy vs Spy scholarship.
-- Steven E. Pav, shabbychef@gmail.com
## Installation
This package can be installed
from CRAN,
via [drat](https://github.com/eddelbuettel/drat "drat"), or
from github:
```{r install,eval=FALSE,echo=TRUE}
# via CRAN:
install.packages("SPYvsSPY")
# via drat:
if (require(drat)) {
drat:::add("shabbychef")
install.packages("SPYvsSPY")
}
# get snapshot from github:
if (require(devtools)) {
install_github('shabbychef/SPYvsSPY')
}
```
## Use it
First get a basic look at the data. The data are documented in the package. For
each row we have the issue of Mad, the release date (as a character with format `%Y-%m`),
the page in the Spy vs. Spy Omnibus, the author and artist of the strip,
whether White Spy and Black Spy received a 'comeuppance' (_i.e._ were clubbed,
eaten by lion, shot, _etc._), whether the Grey Spy appears (only in the 60's),
and whether the victorious Spy flashes the 'V for Victory' sign (mostly in the
80's).
```{r basic_usage,eval=TRUE,echo=TRUE}
library(dplyr)
library(knitr)
library(SPYvsSPY)
# this is it:
data(svs)
head(svs) %>%
kable()
```
Let's check on the appearances per year with a Stem-and-Leaf plot:
```{r no_per_year,eval=TRUE,echo=TRUE}
library(SPYvsSPY)
# this is it:
data(svs)
stem(as.numeric(gsub('^(\\d{4})-.+','\\1',svs$yrmo)),scale=0.5)
```
Compute a running net score of Black Spy victories:
```{r net_victory,eval=TRUE,echo=TRUE}
library(SPYvsSPY)
# this is it:
data(svs)
svs %>%
mutate(black_victory=as.numeric(white_comeuppance) - as.numeric(black_comeuppance)) %>%
mutate(net_black_victory=cumsum(black_victory)) %>%
arrange(Mad_no) %>%
mutate(decade=10 * as.numeric(gsub('^(\\d{3}).+$','\\1',yrmo))) %>%
group_by(decade) %>%
summarize(running_tally_net_black_victory=last(net_black_victory)) %>%
ungroup() %>%
kable()
```
Nearly equal.
## Wald Wolfowitz
The [Wald Wolfowitz test](https://en.wikipedia.org/wiki/Wald%E2%80%93Wolfowitz_runs_test)
is a non-parametric test for the presence of serial correlation that is appropriate for
binary series like this. The test is performed by computing the number of 'runs', which
is to say the number of clusters of consecutive victories by one of the Spies. When the
test statistic is too high
(compared to what would be observed if the data were serially independent),
then the data are too 'flippy', often reversing.
This would be the case if Prohias tried to keep score balance by always reversing the previous outcome.
If the test statistic is too low, the data are too 'sticky', with long periods of one Spy prevailing
over the other. This could happen if Prohias got moody and picked favorites, perhaps.
The test is easy enough to run using the `randtests` package:
```{r "ww",eval=TRUE,echo=TRUE,warning=FALSE,message=FALSE}
library(randtests)
subdata <- svs %>%
filter(Mad_no > 152) %>%
mutate(black_victory=as.numeric(white_comeuppance) - as.numeric(black_comeuppance)) %>%
filter(abs(black_victory) > 0.5)
set.seed(1234)
resu <- randtests::runs.test(subdata$black_victory,threshold=0)
print(resu)
```
We get back a test statistic of `r signif(resu$statistic,2)`, indicating slightly
greater than random amount of reversal. However, this is not statistically
significantly different than the expected value of 0, with a p-value of
`r signif(resu$p.value,2)`.
In conclusion, we have no evidence that Prohias kept running tally of Black
and White Spy victories, and the data are consistent with the victor being
chosen independently of the previous victories.