-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess-data.R
428 lines (387 loc) · 17.1 KB
/
process-data.R
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
# Process food prices data for app
# This version includes conversion from price per kg to price per unit for some items
# -----------------------------------------------------------------------------
# Setup
rm(list = ls())
library(magrittr)
library(lubridate)
library(tidyverse)
library(ggplot2)
library(ggthemes)
library(scales)
source("clean-ggplot-theme.R")
# Directories
data_dir <- "data/"
template_dir <- "template/"
html_dir <- "html/"
img_dir <- "img/"
# Styles
chart_width <- 1600
chart_height <- 500
# Other stuff
current_date <- Sys.Date()
month_names <- tibble(
month_number = 1:12,
month_name = c("January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December")
)
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Load data and cleaning
# Food price index
prices <- read_csv(paste0(data_dir, "CPI324701_20180404_111551_5.csv"), skip = 1) %>%
rename(original_date = X1) %>%
mutate_at(vars(-original_date), as.numeric)
prices_table_junk_row <- which(prices$original_date == "Table information:")
prices <- prices[1:(prices_table_junk_row - 1), ]
prices %<>%
separate(col = original_date,
into = c("year", "month"),
sep = "M",
remove = FALSE,
convert = TRUE) %>%
mutate(date = ymd(paste(year, month, "1", sep = "-"))) %>%
filter(year > 2006) %>%
gather(key = "food",
value = "price",
-original_date, -year, -month, -date)
# Categories and correspondence between foods and categories
categories <- read_csv(paste0(data_dir, "categories.csv")) %>%
arrange(category_order)
food_categories <- read_csv(paste0(data_dir, "food-categories.csv")) %>%
arrange(food) %>%
mutate(food_id = paste0("F", 1:nrow(.)))
# Item weights
item_weights <- read_csv(paste0(data_dir, "item-weights.csv"))
# Data joins and filtering
food_categories %<>% left_join(categories, by = "category") %>%
left_join(item_weights, by = "food") %>%
mutate(units = ifelse(!is.na(weight), "per unit (estimated from price per kg)", units))
prices %<>% left_join(food_categories, by = "food") %>%
filter(!is.na(category)) %>%
arrange(category_id, food_id, date) %>%
left_join(month_names, by = c("month" = "month_number"))
# Unit price conversion for selected items
prices %<>% mutate(price = ifelse(!is.na(weight),
round(price * (weight / 1000), digits = 2),
price))
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# HTML output helper functions
output_html <- function(content, output_filename) {
# Load HTML template parts
template_header <- read_file(paste0(template_dir, "header.html"))
template_footer <- read_file(paste0(template_dir, "footer.html"))
output <- paste(template_header, content, template_footer, sep = "\n")
write_file(output, paste0(html_dir, output_filename))
}
wrap_html_tag <- function(x, tag, params = NULL) {
output <- paste0("<", tag)
if (!is.null(params)) output <- paste(output, paste(params, collapse = " "))
output <- paste0(output, ">", x, "</", tag, ">")
return(output)
}
build_content <- function(current = NULL, new) {
return(paste(current, new, sep = "\n"))
}
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Clean up output folders
do.call(file.remove, list(list.files(paste0(html_dir, img_dir), full.names = TRUE)))
do.call(file.remove, list(list.files(html_dir, full.names = TRUE)))
dir.create(paste0(html_dir, "img"))
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Generate index page (categories menu)
content <- "<h1>Food prices in New Zealand</h1>" %>%
build_content(wrap_html_tag("Select a category:", "p"))
for (k in 1:nrow(categories)) {
content %<>% build_content(
wrap_html_tag(categories[k, "category"],
"a",
params = c(paste0("href = '",
categories[k, "category_id"],
".html'"),
paste0("class = 'pure-button category-button ",
categories[k, "category_id"],
"'"))) %>%
wrap_html_tag("p")
)
}
content %<>%
wrap_html_tag("div", params = "class = 'category-list'") %>%
build_content(
wrap_html_tag("Made by <a href = 'http://schiff.co.nz'>Aaron Schiff</a> using data from <a href = 'http://www.stats.govt.nz/browse_for_stats/economic_indicators/prices_indexes/food-price-index-info-releases.aspx'>Statistics New Zealand</a>.", "p")
) %>%
build_content(
wrap_html_tag(paste0("Prices are averages across many retailers and geographic locations. ",
"Prices have not been adjusted for inflation. ",
"The most recent data is for <b>", month.name[month(max(prices$date))], " ", year(max(prices$date)), "</b>."),
"p")
)
output_html(content, "index.html")
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Generate menu for each category
for (k in 1:nrow(categories)) {
content <- wrap_html_tag(categories[k, "category"], "h1")
items <- food_categories %>%
filter(category == as.character(categories[k, "category"])) %>%
arrange(short_name)
# List of items
for (i in 1:nrow(items)) {
content %<>% build_content(
wrap_html_tag(items[i, "short_name"],
"a",
params = c(paste0("href = '",
items[i, "food_id"],
".html'"),
paste0("class = 'pure-button category-button ",
categories[k, "category_id"],
"'"))) %>%
wrap_html_tag("p")
)
}
# Back button
content %<>% build_content(
wrap_html_tag("‹ Categories list",
"a",
params = c("href = 'index.html'",
"class = 'pure-button category-button back-button'"))
)
output_html(content, paste0(categories[k, "category_id"], ".html"))
}
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Generate page for each food item
valid_food <- food_categories %>%
filter(!is.na(category))
for (f in 1:nrow(valid_food)) {
# Setup
current_month_name <- month_names %>%
filter(month_number == month(current_date)) %>%
pull(month_name)
# Filter price data
item_prices <- prices %>%
filter(food == as.character(valid_food[f, "food"])) %>%
arrange(date)
item_prices_12_months <- tail(item_prices, 12)
# Create ts object of prices, loess trend, and detrended values
item_prices_ts <- ts(item_prices$price,
start = c(year(min(item_prices$date)), month(min(item_prices$date))),
end = c(year(max(item_prices$date)), month(max(item_prices$date))),
frequency = 12)
item_prices_ts <- na.omit(item_prices_ts) # Remove any NA values at beginning/end of series
# NB this will fail if any NA inside the series (shouldn't be)
item_prices_trend <- loess(item_prices_ts ~ time(item_prices_ts),
span = 3/12)
item_prices_trend_ts <- ts(predict(item_prices_trend),
start = start(na.omit(item_prices_ts)),
end = end(na.omit(item_prices_ts)),
frequency = frequency(item_prices_ts))
item_prices_detrended <- item_prices_ts - item_prices_trend_ts
# Set up extra plotting data
plot_dat <- tibble(price_trend = as.numeric(item_prices_trend_ts),
price_detrended = as.numeric(item_prices_detrended),
date = seq(from = ymd(paste(start(item_prices_trend_ts)[1],
start(item_prices_trend_ts)[2],
"1",
sep = "-")),
to = ymd(paste(end(item_prices_trend_ts)[1],
end(item_prices_trend_ts)[2],
"1",
sep = "-")),
by = "1 month")) %>%
mutate(date_factor = factor(month(date),
levels = 1:12,
labels = c("Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec")))
# Create price trend chart
item_chart <- ggplot(item_prices) +
geom_line(aes(x = date, y = price),
size = 3,
col = rgb(200/255, 200/255, 200/255)) +
geom_line(aes(x = date, y = price_trend),
size = 3,
col = "black",
data = plot_dat) +
xlab("") +
ylab("") +
scale_x_date(breaks = date_breaks("2 years"),
labels = date_format("%Y")) +
scale_y_continuous(limits = c(0, NA),
labels = scales::dollar) +
clean_theme(base_size = 46,
axis.ticks.x = element_blank(),
axis.text = element_text(face = "bold",
size = rel(1.1)),
panel.grid.major.x = element_line(colour = "#bbbbbb"),
panel.grid.minor.x = element_line(colour = "#bbbbbb"),
panel.grid.major.y = element_line(colour = "#bbbbbb"),
plot.margin = unit(c(1, 3, 0, 1), "lines"))
png(paste0(html_dir, img_dir, valid_food[f, "food_id"], ".png"), width = chart_width, height = chart_height)
print(item_chart)
dev.off()
# Create monthly price deviation chart
seasonality_chart <- ggplot(plot_dat) +
geom_hline(yintercept = 0, colour = "black", size = 2) +
geom_point(aes(x = date_factor, y = price_detrended),
colour = rgb(0, 0, 0, 0.3),
size = 8,
shape = 16) +
geom_point(aes(x = date_factor, y = mean_price_detrended),
colour = rgb(40/255, 40/255, 40/255),
size = 56,
shape = "-",
data = plot_dat %>%
group_by(date_factor) %>%
summarise(mean_price_detrended = mean(price_detrended, na.rm = TRUE))) +
xlab("") +
ylab("") +
scale_y_continuous(labels = scales::dollar) +
clean_theme(base_size = 46,
axis.ticks.x = element_blank(),
axis.text = element_text(face = "bold",
size = rel(1.1)),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(colour = "#bbbbbb"),
plot.margin = unit(c(1, 3, 0, 1), "lines"))
png(paste0(html_dir, img_dir, valid_food[f, "food_id"], "-seas.png"), width = chart_width, height = chart_height)
print(seasonality_chart)
dev.off()
# Title and subtitle
content <- wrap_html_tag(valid_food[f, "short_name"], "h1") %>%
build_content(
paste0(valid_food[f, "units"],
ifelse(!is.na(valid_food[f, "subtitle"]),
paste0(", ", valid_food[f, "subtitle"]),
"")) %>%
wrap_html_tag("h2")
)
# Price in same month last year and range over the past year
content %<>%
build_content(
wrap_html_tag(
paste0("<b>",
sprintf("$%0.2f",
item_prices %>%
filter(year == year(current_date) - 1,
month == month(current_date)) %>%
pull(price)),
"</b> last ", current_month_name),
"p",
params = "class = 'previous-price'")
) %>%
build_content(
wrap_html_tag(
paste0("Between ",
sprintf("$%0.2f", min(item_prices_12_months$price, na.rm = TRUE)),
" and ",
sprintf("$%0.2f", max(item_prices_12_months$price, na.rm = TRUE)),
" in the past 12 months."),
"p",
params = "class = 'previous-price'"
)
)
# # Price variability table
# content %<>%
# build_content(
# paste(wrap_html_tag("", "th"),
# wrap_html_tag("Lowest",
# "th",
# params = "class = 'align-right'"),
# wrap_html_tag("Highest",
# "th",
# params = "class = 'align-right'")) %>%
# wrap_html_tag("tr") %>%
# wrap_html_tag("thead") %>%
# build_content(
# paste(wrap_html_tag(current_month_name, "td"),
# wrap_html_tag(sprintf("$%0.2f",
# item_prices %>%
# filter(month == month(current_date)) %>%
# summarise(min_price = min(price, na.rm = TRUE)) %>%
# pull(min_price)),
# "td",
# params = "class = 'align-right'"),
# wrap_html_tag(sprintf("$%0.2f",
# item_prices %>%
# filter(month == month(current_date)) %>%
# summarise(max_price = max(price, na.rm = TRUE)) %>%
# pull(max_price)),
# "td",
# params = "class = 'align-right'")) %>%
# wrap_html_tag("tr") %>%
# build_content(
# paste(wrap_html_tag("Past 12 months", "td"),
# wrap_html_tag(sprintf("$%0.2f", min(item_prices_12_months$price, na.rm = TRUE)),
# "td",
# params = "class = 'align-right'"),
# wrap_html_tag(sprintf("$%0.2f", max(item_prices_12_months$price, na.rm = TRUE)),
# "td",
# params = "class = 'align-right'")) %>%
# wrap_html_tag("tr")
# ) %>%
# wrap_html_tag("tbody")
# ) %>%
# wrap_html_tag("table",
# params = "class = 'pure-table pure-table-horizontal'")
# )
# Price charts
content %<>%
build_content(
wrap_html_tag("<b>Price trend (not adjusted for inflation)</b>",
"p",
params = "class = 'chart-title'")
) %>%
build_content(
wrap_html_tag(
paste0("<img class = 'pure-img price-chart' src = '", img_dir, valid_food[f, "food_id"], ".png' />"),
"div",
params = "class = 'chart'"
)
) %>%
build_content(
wrap_html_tag("<b>Monthly variation around the trend</b>",
"p",
params = "class = 'chart-title'")
) %>%
build_content(
wrap_html_tag(
paste0("<img class = 'pure-img price-chart' src = '", img_dir, valid_food[f, "food_id"], "-seas.png' />"),
"div",
params = "class = 'chart'"
)
)
# Back buttons
content %<>%
build_content(
wrap_html_tag(paste0("‹ ", valid_food[f, "category"]),
"a",
params = c(paste0("href = '", valid_food[f, "category_id"], ".html'",
paste0("class = 'pure-button category-button back-button ",
valid_food[f, "category_id"],
"'")))) %>%
wrap_html_tag("p")
) %>%
build_content(
wrap_html_tag("‹ Categories list",
"a",
params = c("href = 'index.html'",
"class = 'pure-button category-button back-button'")) %>%
wrap_html_tag("p")
)
# Write output
output_html(content, paste0(valid_food[f, "food_id"], ".html"))
}
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Copy template items to html output folder
file.copy(from = paste0(template_dir, "style.css"),
to = paste0(html_dir, "style.css"),
overwrite = TRUE)
file.copy(from = paste0(template_dir, "apple-touch-icon.png"),
to = paste0(html_dir, "apple-touch-icon.png"),
overwrite = TRUE)
# -----------------------------------------------------------------------------