Wednesday, February 10, 2016

Master R 12 - Analyzing Time-series

Creating time-series objects

library(hflights)
library(data.table)
dt <- data.table(hflights)
dt[, date := ISOdate(Year, Month, DayofMonth)]

daily <- dt[, list(
     N         = .N,
     Delays    = sum(ArrDelay, na.rm = TRUE),
     Cancelled = sum(Cancelled),
     Distance  = mean(Distance)
 ), by = date]
str(daily)


Visualizing time-series

plot(ts(daily))
setorder(daily, date)
plot(ts(daily))
plot(ts(daily$N, start = 2011, frequency = 365), main = 'Number of flights from Houston in 2011')


Seasonal decomposition

plot(decompose(ts(daily$N, frequency = 7)))
setNames(decompose(ts(daily$N, frequency = 7))$figure, weekdays(daily$date[1:7]))
decompose(ts(daily$N, frequency = 365))


Holt-Winters filtering

nts <- ts(daily$N, frequency = 7)
fit <- HoltWinters(nts, beta = FALSE, gamma = FALSE)
plot(fit)
fit <- HoltWinters(nts)
plot(fit)
library(forecast)
forecast(fit)
plot(forecast(HoltWinters(nts), 31))


Autoregressive Integrated Moving Average models

auto.arima(nts)
auto.arima(nts, approximation = FALSE)
plot(forecast(auto.arima(nts, D = 1, approximation = FALSE), 31))


Outlier detection

cts <- ts(daily$Cancelled)
fit <- auto.arima(cts)
auto.arima(cts)

library(tsoutliers)
outliers <- tso(cts, tsmethod = 'arima',
   args.tsmethod  = list(order = c(1, 1, 2)))
plot(outliers)
plot(tso(ts(daily$Cancelled)))
dfc <- as.data.frame(daily[, c('date', 'Cancelled'), with = FALSE])

library(AnomalyDetection)
AnomalyDetectionTs(dfc, plot = TRUE)$plot


More complex time-series objects

library(zoo)
zd <- zoo(daily[, -1, with = FALSE], daily[[1]])
plot(zd)

plot(cumsum(zd))

No comments:

Post a Comment

Blog Archive