-
Notifications
You must be signed in to change notification settings - Fork 2
/
seasonality.Rmd
524 lines (421 loc) · 18.5 KB
/
seasonality.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
```{r global_options,include=FALSE}
knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='figure/seasonality_',
cache=FALSE,cache.path='cache/seasonality_',
echo=TRUE, eval=TRUE, warning=FALSE, message=FALSE)
require(timeDate)
require(lubridate)
require(reshape2)
require(ggplot2)
require(DBI)
require(RMySQL)
require(dplyr)
```
# Seasonality of theatrical release via IMDb data
## The data
The IMDb [ftp data](ftp://ftp.fu-berlin.de/pub/misc/movies/database/)
includes information about budget, box office, rentals, and admissions.
Here we will check the data, to see if we can find any
oddities or insights. First, we attach ourselves to our movie database,
and poke around:
```{r docon,cache=FALSE}
library(DBI)
library(RMySQL)
library(dplyr)
dbcon <- src_mysql(host='0.0.0.0',user='moe',password='movies4me',dbname='IMDB',
port=23306)
dbGetQuery(dbcon$con,'SET NAMES utf8')
src_tbls(dbcon)
```
(These blogs work like call and response. The code is first, the response
follows.)
First, let us check for movies. Note that we tried to remove porn, TV
shows, video games, and so on. We are left with around 250K titles (many
of these are probably shorts):
```{r first}
nmovies <- tbl(dbcon,'title') %>%
summarize(count=n())
print(nmovies)
# can we find Raiders of the lost ark?
rotla <- tbl(dbcon,'title') %>%
filter(title == "Raiders of the Lost Ark")
print(rotla)
```
## Opening weekend
Now, how many movies report opening weekend numbers in the USA, in dollars?
```{r rep1}
nreport <- tbl(dbcon,'movie_opening_weekend') %>%
filter(units == '$',locale=='USA') %>%
summarize(count=n())
print(collect(nreport))
```
Not a whole lot. Just around 10K. Let us see how many there are over time. Here
we collect movies opening weekend performance, aggregated at month end. Here we
are plotting the total number of movies reporting, the total screens
represented (in thousands), and the total gross revenue reported (in tens of
millions).
We see an increase in the number of movies and screens reporting around 1997,
and a light tailoff in the last two years. I suspect (or, rather, hope) that
box office numbers for the recent years have simply not been tabulated yet, and
these will eventually find their way into imdb. The alternative is that fewer
big picture movies are being made.
```{r rep2}
openw <- tbl(dbcon,'movie_opening_weekend') %>%
filter(units == '$',locale=='USA')
openw <- collect(openw) %>%
mutate(end_date=as.Date(end_date)) %>%
filter(end_date > as.Date('1990-01-01'))
require(timeDate)
openw$week_end <- as.Date(timeDate::timeNdayOnOrAfter(as.timeDate(openw$end_date),nday=3))
openw$month_end <- as.Date(timeDate::timeLastDayInMonth(as.timeDate(openw$end_date)))
aggr <- openw %>%
group_by(month_end) %>%
summarize(movie_count=n(),
totscreens_thousands=1e-3*sum(screens,na.rm=TRUE),
totamount_tens_millions=1e-7*sum(amount,na.rm=TRUE))
require(reshape2)
magg <- melt(aggr,id.vars='month_end',variable.name='metric')
require(ggplot2)
ph <- ggplot(magg,aes(x=month_end,y=value,group=metric)) + geom_point() + stat_smooth() + facet_grid(metric ~ .)
print(ph)
```
## Seasonality?
Is there a seasonality effect to revenue? First, let us look at the total gross
revenue _per screen_, as aggregated over a monthly basis. These numbers are in
tens of thousands of dollars. We see a slight increase in this number over
time, likely due to inflationary effects. (_i.e._ the price of a movie ticket
is simply increasing.) However, if we adjust for this baseline increase, we
should then see some seasonality.
```{r rep3}
require(dplyr)
aggr <- openw %>%
filter(month_end > as.Date('1996-07-01')) %>%
group_by(month_end) %>%
summarize(movie_count=n(),
totscreens_thousands=1e-3*sum(screens,na.rm=TRUE),
totamount_tens_millions=1e-7*sum(amount,na.rm=TRUE)) %>%
mutate(aps_tenk=totamount_tens_millions / totscreens_thousands) %>%
filter(movie_count > 5)
ph <- ggplot(aggr,aes(x=month_end,y=aps_tenk)) + geom_point() + stat_smooth(method="lm")
print(ph)
```
Now we remove that //linear// trend and look for non-linear and seasonality
effects. Below is the plot of gross receipts per screen over time.
```{r rep3too}
# normalize for linear trend
mod0 <- lm(aps_tenk ~ month_end,data=aggr)
aggr$residuals <- mod0$residuals
ph <- ggplot(aggr,aes(x=month_end,y=residuals)) + geom_point() + stat_smooth()
print(ph)
```
And here is the per-month receipts per screen, in tens of thousands of dollars,
with adjustments for (linear) inflation:
```{r rep3tree}
aggr <- openw %>%
filter(month_end > as.Date('1996-07-01')) %>%
group_by(month_end) %>%
summarize(movie_count=n(),
totscreens_thousands=1e-3*sum(screens,na.rm=TRUE),
totamount_tens_millions=1e-7*sum(amount,na.rm=TRUE)) %>%
mutate(aps_tenk=totamount_tens_millions / totscreens_thousands) %>%
filter(movie_count > 5)
aggr$month <- factor(base::months(aggr$month_end,abbreviate=FALSE),levels=month.name)
# normalize for linear trend
mod0 <- lm(aps_tenk ~ month_end,data=aggr)
aggr$residuals <- mod0$residuals
ph <- ggplot(aggr,aes(x=month,y=residuals)) + geom_boxplot()
print(ph)
```
We can break this down even finer and aggregate by weeks, again removing the
linear trend, and then looking at boxplots. Because the labels are hard to read, I plot
again, removing the first part of the year.
```{r rep4}
aggr <- openw %>%
group_by(week_end) %>%
summarize(movie_count=n(),
totscreens_thousands=1e-3*sum(screens,na.rm=TRUE),
totamount_tens_millions=1e-7*sum(amount,na.rm=TRUE)) %>%
mutate(aps_tenk=totamount_tens_millions / totscreens_thousands) %>%
filter(week_end > as.Date('1996-07-01')) %>%
filter(movie_count > 5) %>%
filter(totscreens_thousands > 3)
ph <- ggplot(aggr,aes(x=week_end,y=aps_tenk)) + geom_point() + stat_smooth()
print(ph)
# normalize for linear trend
mod0 <- lm(aps_tenk ~ week_end,data=aggr)
aggr$residuals <- mod0$residuals
aggr$yday <- lubridate::yday(aggr$week_end)
weekifier <- function(x) { as.Date(7 * ceiling((x-3)/7), origin=as.Date("1960-01-01")) }
aggr$week <- factor(as.character(weekifier(aggr$yday),format='%b %d'),
levels=as.character(sort(unique(weekifier(aggr$yday))),format='%b %d'),
ordered=TRUE)
ph <- ggplot(aggr,aes(x=week,y=residuals)) + geom_boxplot()
print(ph)
# and the summer detail:
ph <- ggplot(aggr %>% filter(week > 'Apr 22') %>% filter(week < "Dec 09"),
aes(x=week,y=residuals)) + geom_boxplot()
print(ph)
# mental note: you should be able to make the plot larger!
# except: bitbucket + ggvis + knitr = fail
#aggr %>% ggvis(x=~week,y=~residuals) %>%
#layer_boxplots()
```
## Does this hold for small films?
We see the classic 'summer blockbuster' effect starting in early May and running through July, with
another pickup in November. This effect is real and has to be corrected for if we are going to look at
studio films. One wonders if this effect persists for independent films. It appears there is a different
seasonality effect. Here we grab the opening weekend gross receipts from films reporting to have shown at
fewer 50 screens. (I suspect that some of these are due to bad data--sneak previews, fat finger errors, and
so on--but I have performed this analysis using a cap of $1M opening weekend as a filter, and have seen
essentially the same pattern.)
Again, we compute the gross per screen, remove linear trend, then plot per month. There are two bumps here:
one likely due to 'Oscar Season', where showcase films are presented at the end of the year, and then a bump
in July and August. The latter, I would speculate, is due to movie-goers arriving at the theater to find they
cannot get tickets to the big show, instead choosing the also-ran (or potentially sneak preview effect or bad
data).
```{r smallfilm}
smal <- openw %>%
filter(month_end > as.Date('1996-07-01')) %>%
filter(screens < 50) %>%
group_by(month_end) %>%
summarize(movie_count=n(),
totscreens_thousands=1e-3*sum(screens,na.rm=TRUE),
totamount_tens_millions=1e-7*sum(amount,na.rm=TRUE)) %>%
mutate(aps_tenk=totamount_tens_millions / totscreens_thousands) %>%
filter(movie_count > 5) %>%
filter(aps_tenk < 4)
# or cap at $1M opening gross. same ting.
#smal <- openw %>%
#filter(month_end > as.Date('1996-07-01')) %>%
#filter(amount < 1e6) %>%
#group_by(month_end) %>%
#summarize(movie_count=n(),
#totscreens_thousands=1e-3*sum(screens,na.rm=TRUE),
#totamount_tens_millions=1e-7*sum(amount,na.rm=TRUE)) %>%
#mutate(aps_tenk=totamount_tens_millions / totscreens_thousands) %>%
#filter(movie_count > 5) %>%
#filter(aps_tenk < 4)
smal$month <- factor(base::months(smal$month_end,abbreviate=FALSE),levels=month.name)
# normalize for linear trend
mod0 <- lm(aps_tenk ~ month_end,data=smal)
smal$residuals <- mod0$residuals
ph <- ggplot(smal,aes(x=month,y=residuals)) + geom_boxplot()
print(ph)
```
## All gross receipts
The IMDb data also holds weekend gross figures for films beyond their opening weekend.
Here I grab weekend gross numbers for films which opened on more than 100 screens. I
compute the gross receipts per screen, then normalize to opening weekend,
then plot versus days since opening, with a single line per film.
The y axis is in logscale, and since the opening weekend is _numeraire_, we expect to
(and do) see numbers smaller than one and decreasing over time.
I plot this again with a mean smooth and bands at the 0.25 and 0.75 quantiles. We see
that at around 11 weeks after opening, the average film has gross receipts (per screen)
one tenth of opening weekend. We also see that the number of screens is steadily decreasing
after opening weekend.
```{r allgross}
allg <- tbl(dbcon,'movie_weekend_gross') %>%
filter(units == '$',locale=='USA') %>%
mutate(aps=amount/screens) %>%
collect()
firstw <- collect(allg) %>%
arrange(days_open) %>%
group_by(movie_id) %>%
summarize(first_weekend=first(amount),
first_aps=first(aps),
first_screens=first(screens))
# interesting bimodal effect:
# hist(log10(1+firstw$first_weekend),50)
normalized <- left_join(collect(allg),firstw,by='movie_id') %>%
mutate(prop_open=aps / first_aps) %>%
filter(days_open < 200) %>%
filter(first_screens > 100) %>%
filter(!is.na(prop_open)) %>%
mutate(weeks_open = ceiling(days_open / 7))
nquants <- normalized %>%
group_by(weeks_open) %>%
summarize(q25=quantile(prop_open,0.25),
q5=quantile(prop_open,0.5),
q75=quantile(prop_open,0.75))
normalized <- left_join(normalized,nquants,by='weeks_open')
ph <- ggplot(normalized,aes(x=days_open,y=prop_open,group=movie_id,colour=screens)) + geom_line() + scale_y_log10()
print(ph)
ph <- ggplot(normalized,aes(x=weeks_open,y=prop_open)) +
stat_smooth() +
geom_point(aes(size=screens)) +
geom_ribbon(aes(x=weeks_open,ymin=q25,ymax=q75),alpha=0.5,fill='lightblue') +
scale_y_log10(limits=c(0.0001,10))
print(ph)
```
## Screens for budget
The IMDb data also holds budget data. Here we look at the number of screens reported for opening weekend
versus the reported budget, in millions, for films with US opens and budgets given in dollars.
There seems to be two effects here: a somewhat linear effect for big studio productions, then a whole
bunch of sneak preview or limited release movies with somewhat random budgets. We cut out the latter in the
second plot and capture the linear effect.
```{r spbudget}
budgets <- tbl(dbcon,'movie_budgets') %>%
filter(units=='$') %>%
select(-units,-id) %>%
rename(budget=amount)
openw <- tbl(dbcon,'movie_opening_weekend') %>%
filter(units == '$',locale=='USA') %>%
select(-units,-locale,-id)
bcombo <- left_join(budgets,openw,by='movie_id') %>%
filter(!is.na(screens)) %>%
mutate(budget_millions=1e-6*budget)
ph <- ggplot(bcombo %>% collect(),aes(x=budget_millions,y=screens)) +
geom_point() +
scale_x_log10() +
scale_y_log10()
print(ph)
ph <- ggplot(bcombo %>% filter(screens > 1000) %>% collect(),aes(x=budget_millions,y=screens)) +
geom_point() +
scale_x_log10() +
scale_y_log10() +
geom_smooth(method='lm')
print(ph)
```
This is only really 'linear' in a log plot. By performing linear regression in log space, we are actually
capturing a [power law relationship](https://en.wikipedia.org/wiki/Power_law) between the two. This is a bit
odd because one would expect that screens (and, thereby, projected revenue) should scale _linearly_ with budget.
However, the fit we find is more like screens scale with budget to the 1/5th power. Part of this may be due
to oddities around our filtering of data, and some of this is due to the limited total number of theaters colliding
with the apparently unlimited budget of the studios.
```{r spbud2}
mod0 <- lm(log(screens)~log(budget_millions),data=bcombo %>% filter(screens > 1000) %>% collect())
print(mod0$coefficients)
```
## By genre?
We have genre information in the IMDb data. Genres are not exclusive: a movie has multiple genres listed. To deal with
this problem, I compute the number of genres listed for a movie, then create a weighting for that movie which is
one over the total number of genres. Multiplying quantities by this weight then summing over all movies gives weighted
quantities on a per-genre basis. For example, we can compute the _weighted_ number of screens opening in a given weekend
for a given genre; when summing this quantity over genres, the total of should equal the sum of screens over all movies
because we defined the weights to sum to one over each movie.
Here I compute the weighted gross recipts on opening weekend, divided by the weighted total screens, aggregated over months,
then plotted for the most popular genres. There is not a huge difference between them, which feels a bit strange, although
`Action` movies appear to dominate slightly, with `Romance` showing the weakest returns.
```{r bygenre,cache=TRUE}
mgenres <- tbl(dbcon,sql("SELECT movie_id,info FROM movie_info WHERE info_type_id IN (SELECT info_type_id FROM info_type WHERE info='genres')")) %>%
rename(genre=info)
totgenres <- mgenres %>%
group_by(movie_id) %>%
summarize(gcount=n()) %>%
mutate(gweight=1/gcount)
gentable <- left_join(mgenres,totgenres,by='movie_id')
# now join on opening weekend
openw <- tbl(dbcon,'movie_opening_weekend') %>%
filter(units == '$',locale=='USA') %>%
select(-units,-locale,-id)
openw <- left_join(openw,gentable,by='movie_id')
openw <- collect(openw) %>%
mutate(end_date=as.Date(end_date)) %>%
filter(end_date > as.Date('1996-01-01'))
require(timeDate)
openw$week_end <- as.Date(timeDate::timeNdayOnOrAfter(as.timeDate(openw$end_date),nday=3))
openw$month_end <- as.Date(timeDate::timeLastDayInMonth(as.timeDate(openw$end_date)))
# now weight and group by genres
gopen <- openw %>%
mutate(wamount=amount * gweight,
wscreens=screens * gweight) %>%
group_by(genre,month_end) %>%
summarize(totfilms = n(),
nfilms = sum(gweight,na.rm=TRUE),
wtscreens_thousands=1e-3 * sum(wscreens,na.rm=TRUE),
wamount_tenmillions=1e-7 * sum(wamount,na.rm=TRUE)) %>%
filter(totfilms > 2) %>%
filter(wtscreens_thousands > 0.01) %>%
mutate(aps_tenk=wamount_tenmillions / wtscreens_thousands) %>%
filter(!is.na(aps_tenk))
# select by popular genres
ngen <- gopen %>%
group_by(genre) %>%
summarize(count=n()) %>%
filter(count > 200)
gopen <- gopen %>%
filter(genre %in% ngen$genre)
ph <- ggplot(gopen,aes(x=month_end,y=aps_tenk,group=genre,colour=genre)) +
geom_point() +
scale_y_log10() +
stat_smooth(method='loess')
print(ph)
```
## Seasonality by genre?
Do we see the same seasonality patterns when breaking out by genre? Let us consider that.
First remove the linear trend, then boxplot the (weighted) gross per screen
against month with a strip for each genre. The numbers have been normalized geometrically
so the long term average is 1, and plotted in logscale.
There is a pronounced 'summer blockbuster' effect for `Action` and, to a lesser extent, for
`Comedy` and `Thriller`, but nothng of the sort for `Romance` and `Drama`, which tend to
have bumps in November.
The other way to view this, though typically frowned upon by statisticians, is via a stacked
bar chart. I compute one of those as well, giving the total proportion of gross receipts
attributed to each of five main genres, all normalized. The averaging might be a little funny
here (weighted by month, not by dollar), but I think the story will remain the same if we
slice a different way: May is huge for `Action`, `Thriller`s perform well in September and into
October, and `Drama`s pick up later in the year. A case might also be had for `Romance` in February.
```{r seas_bygenre,cache=TRUE}
mgenres <- tbl(dbcon,sql("SELECT movie_id,info FROM movie_info WHERE info_type_id IN (SELECT info_type_id FROM info_type WHERE info='genres')")) %>%
rename(genre=info)
totgenres <- mgenres %>%
group_by(movie_id) %>%
summarize(gcount=n()) %>%
mutate(gweight=1/gcount)
gentable <- left_join(mgenres,totgenres,by='movie_id')
# now join on opening weekend
openw <- tbl(dbcon,'movie_opening_weekend') %>%
filter(units == '$',locale=='USA') %>%
select(-units,-locale,-id)
openw <- left_join(openw,gentable,by='movie_id')
openw <- collect(openw) %>%
mutate(end_date=as.Date(end_date)) %>%
filter(end_date > as.Date('1996-08-31'))
require(timeDate)
openw$week_end <- as.Date(timeDate::timeNdayOnOrAfter(as.timeDate(openw$end_date),nday=3))
openw$month_end <- as.Date(timeDate::timeLastDayInMonth(as.timeDate(openw$end_date)))
# now weight and group by genres
gopen <- openw %>%
mutate(wamount=amount * gweight,
wscreens=screens * gweight) %>%
group_by(genre,month_end) %>%
summarize(totfilms = n(),
nfilms = sum(gweight,na.rm=TRUE),
wtscreens_thousands=1e-3 * sum(wscreens,na.rm=TRUE),
wamount_tenmillions=1e-7 * sum(wamount,na.rm=TRUE)) %>%
filter(totfilms > 2) %>%
filter(wtscreens_thousands > 0.01) %>%
mutate(aps_tenk=wamount_tenmillions / wtscreens_thousands) %>%
filter(!is.na(aps_tenk))
# select by popular genres
ngen <- gopen %>%
group_by(genre) %>%
summarize(count=n()) %>%
filter(count > 190)
gopen <- gopen %>%
filter(genre %in% ngen$genre)
gopen$month <- factor(base::months(gopen$month_end,abbreviate=FALSE),levels=month.name)
mod0 <- lm(log(aps_tenk) ~ month_end,data=gopen)
gopen$residuals <- exp(mod0$residuals)
ph <- ggplot(gopen,aes(x=month,y=residuals)) +
geom_boxplot() +
facet_grid(genre ~ .) +
scale_y_log10(limits=c(0.1,10))
print(ph)
moammt <- gopen %>%
group_by(month_end) %>%
mutate(prop_amount=wamount_tenmillions / sum(wamount_tenmillions,na.rm=TRUE)) %>%
ungroup() %>%
group_by(month,genre) %>%
summarize(prop_prop=mean(prop_amount)) %>%
ungroup() %>%
group_by(month) %>%
mutate(tot_prop=prop_prop / sum(prop_prop)) %>%
ungroup()
ph <- ggplot(moammt,aes(x=month,weight=100*tot_prop,fill=genre)) +
geom_bar() +
labs(y='% gross receipts')
print(ph)
```
```{r cleanup,include=FALSE}
# dbDisconnect(dbcon)
```