/
2-time-series.r
74 lines (56 loc) · 2.22 KB
/
2-time-series.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
library(ggplot2)
load("city-summary.rdata")
ggplot(bigsum, aes(date, price / 1e6)) +
geom_line() +
facet_wrap(~ city)
ggsave("cities-price.png", width = 8, height = 6, dpi = 128)
# Smoothing ------------------------------------------------------------------
library(mgcv)
smooth <- function(y, x) {
as.numeric(predict(gam(y ~ s(x), na.action = na.exclude)))
}
bigsum <- ddply(bigsum, .(city), transform,
price_s = smooth(price, as.numeric(date)))
ggplot(bigsum, aes(date, price_s / 1e6)) +
geom_line() +
facet_wrap(~ city)
ggsave(file = "cities-smooth.png", width = 8, height = 6, dpi = 128)
index <- function(y, x) {
y / y[order(x)[1]]
}
bigsum <- ddply(bigsum, .(city), transform,
price_si = index(price_s, date))
ggplot(bigsum, aes(date, price_si)) +
geom_line() +
facet_wrap(~ city)
ggsave(file = "cities-index.png", width = 8, height = 6, dpi = 128)
# Simplify ---------------------------------------------------------
# Show location of peak and plummet
brentwood <- subset(bigsum, city == "Brentwood")
brentwood_sum <- subset(brentwood, as.character(date) %in% c("2006-01-15", "2006-06-04"))
ggplot(brentwood, aes(date, price_si)) +
geom_line() +
geom_point(data = brentwood_sum, colour = "red", size = 5)
ggsave("brentwood.png", width = 8, height = 6, dpi = 128)
covar <- ddply(bigsum, "city", summarise,
peak = price_si[date == "2006-01-15"],
plummet = price_si[date == "2006-06-04"]
)
ggplot(covar, aes(peak, plummet)) +
geom_point()
ggsave(file = "cities-2d.png", width = 6, height = 6, dpi = 128)
ggplot(covar, aes(peak, plummet)) +
geom_point() +
geom_text(aes(label = city), size = 4, hjust = -0.05)
ggsave(file = "cities-2d-labelled.png", width = 6, height = 6, dpi = 128)
# Try and explain with covariates from the census data ----------------------
covar$delta <- with(covar, plummet - peak)
census <- read.csv("census-city.csv")
covar <- join(covar, census, by = "city")
base <- ggplot(covar, aes(y = delta)) + geom_point()
base + aes(grads)
ggsave(file = "covar-grads.png", width = 6, height = 6, dpi = 128)
base + aes(income)
ggsave(file = "covar-income.png", width = 6, height = 6, dpi = 128)
base + aes(housesold_size)
ggsave(file = "covar-household.png", width = 6, height = 6, dpi = 128)