September 23, 2019

Time Series (ts, xts, zoo) packages

Time Series, ts packages, in R

ts(data = NA, start = 1, end = numeric(), frequency = 1, deltat = 1, ts.eps = getOption("ts.eps"), class = , names = )
ts(1:20)
ts(vector1)
time_series <- ts(data_vector, start=2013, frequency=1)
ts(1:10, frequency = 4, start = c(1989, 2))
print( ts(1:10, frequency = 7, start = c(12, 2)), calendar = TRUE)
ts(df[, col], frequency=24, start=1)
gnp <- ts(cumsum(1 + round(rnorm(100), 2)), start = c(1972, 7), frequency = 12)
z <- ts(matrix(rnorm(300), 100, 3), start = c(1981, 1), frequency = 12)
X <- timeSeries(matrix(rnorm(24), 12), timeCalendar())
tx <- timeSeries(1:10, 1:10)

print(Nile)
length(Nile)
head(Nile, n=11)
tail(Nile, n=12)

is.ts(data_series)
as.ts(x, ...)

start(series) # return the time index of the first observation
start(AirPassengers)
head(AirPassengers)
end(series) # return the time index of the last observation
end(AirPassengers)
tail(AirPassengers)
frequency(series) # returns the number of observations per unit time
frequency(z)
summary(AirPassengers)
deltat(series) # returns the fixed time interval between observations
deltat(z)
time(series)
cycle(series)
cycle(presidents)
cycle(z[-(3:4)])
aggregate(y.ts, FUN=mean)
aggregate(y.ts, nf=7, FUN=mean)


ts.plot(..., gpars = list())
ts.plot(ldeaths, mdeaths, fdeaths, gpars=list(xlab="year", ylab="deaths", lty=c(1:3)))
ts.plot(eu_stocks, col = 1:4, xlab = "Year", ylab = "Index Value", main = "Major European Stock Indices, 1994-1999")

plot(Nile)
plot(Nile, xlab = "Year", ylab = "River Volume (1e9 m^{3})")
plot(diff(log(AirPassengers)))
diff(df$count, 4)
plot(Nile, xlab="Year", ylab="River Volume (1e9 m^{3})", main="Annual River Nile Volume, 1871-1970", type="b", sub)
plot.ts(c(5,7,8,2,9))
lines(my_ts, col="red", lwd=2)
qplot(date, count, data=ts_data_frame)
boxplot(AirPassengers, cycle(AirPassengers))

acf(ts, main=paste('ACF of', col)) # auto correlation function
acf(localpred)
acf(ml, main='ACF for simulated sample data')
acf(noise, level=90, lag.max=30, main="ACF Plot for White Noise")
acf(ar3, type="correlation", level=95, lag.max=50, plot=T, main='ACF Plot for AR(3)')
acf(arima.sim(list(order=c(1,0,0), ar=-0.7), n=100), lag.max=15, main='ACF plot')
acf(diff(log(AirPassengers)))

pacf(ts, main=paste('PACF of', col)) # partial auto correlation function
pacf(noise, level=85, lag.max=45, main="PACF Plot for White Noise")
acf(localpred, type="partial")
pacf(diff(log(AirPassengers)))

arima(x, order = c(0L, 0L, 0L), seasonal = list(order = c(0L, 0L, 0L), period = NA), xreg = NULL, include.mean = TRUE, transform.pars = TRUE, fixed = NULL, init = NULL, method = c("CSS-ML", "ML", "CSS"), n.cond, SSinit = c("Gardner1980", "Rossignol2011"), optim.method = "BFGS", optim.control = list(), kappa = 1e6)
arima(lh, order = c(1,0,1))
arima(USAccDeaths, order = c(0,1,1), seasonal = list(order = c(0,1,1)), method = "CSS")
arima(LakeHuron, order = c(2,0,0), xreg = time(LakeHuron) - 1920)
arima(log(AirPassengers), c(0,1,1), seasonal=list(order=c(0,1,1), period=12))

fcast_auto_arima <- predict(autoarima, n.ahead = 10)
pre <- predict(fit, n.ahead=5*12)
tsdiag(armafit)
ma_smoothedseries <- filter(timeser, filter=rep(1/width, width), method="convolution", sides=2)
filter(timeser, filter=rep(1/(2*w+1),(2*w+1)), method='convolution', sides=2)
(m <- HoltWinters(co2))
exp_smoothedseries <- HoltWinters(timeser, alpha=alphas[i], beta=FALSE, gamma=FALSE)
(m <- HoltWinters(AirPassengers, seasonal = "mult"))
series <- HoltWinters(timeser, gamma=FALSE)
fcast <- holt(airmiles)
deaths.fcast <- hw(USAccDeaths,h=48)
forecasts2 <- forecast.HoltWinters(souvenirtimeseriesforecasts, h=48)
fit3 <- ses(oildata, h=3)
fit2 <- ses(oildata, alpha=0.6, initial="simple", h=3)

acf2(xerie)
acf2(diff(ts(scan("coloradoflow.dat")), 12), 48)
Box.test(noise, lag = 40, type = "Ljung") # Ljung-Box Test or Portmanteau Test 
ks.test(noise, "pnorm", mean(noise), sd(noise)) # Kolmogorov-Smirnoff test 
shapiro.test(noise) # Shapiro Test
stl(x, s.window, s.degree = 0, t.window = NULL, t.degree = 1, l.window = nextodd(period), l.degree = t.degree, s.jump = ceiling(s.window/10), t.jump = ceiling(t.window/10), l.jump = ceiling(l.window/10), robust = FALSE, inner = if(robust)  1 else 2, outer = if(robust) 15 else 0, na.action = na.fail)
plot(stl(nottem, "per"))
plot(stl(log(co2), s.window = "periodic", t.window = 1000))
summary(stmR <- stl(mdeaths, s.window = "per", robust = TRUE))
BIC(fit1, fit3)

lowess - locally weighted scatterplot smother
loess - local regress
stationarity - time series which have no trend

library(tseries)
adf.test(x, alternative = c("stationary", "explosive"), k = trunc((length(x)-1)^(1/3))) # Augmented Dickey-Fuller test
adf.test(rnorm(1000))
adf.test(exc$Exchange.Rate.TWI, alternative="stationary")
adf.test(residual, alternative = "stationary")
adf.test(diff(log(AirPassengers)), alternative="stationary", k=0)
kpss.test(x, null = c("Level", "Trend"), lshort = TRUE) # Kwiatkowski-Phillips-Schmidt-Shin test
kpss.test(exc$Exchange.Rate.TWI)
kpss.test(residual)
kpss.test(cumsum(rnorm(1000)))
ts_quote = get.hist.quote(instrument="sbux", start="1995-03-01", end="2005-03-01", quote="AdjClose", provider="yahoo", origin="1970-01-01", compression="d", retclass="zoo") 

library(forecast)
armafit <- Arima(timeseries, order=order, method="ML")
air.model <- Arima(window(AirPassengers, end=1956+11/12), order=c(0,1,1), seasonal=list(order=c(0,1,1), period=12), lambda=0)
air.model2 <- Arima(window(AirPassengers, start=1957), model=air.model)
fc <- forecast.Arima(model, h=10)
arma22 <- arima.sim(model=list(ar=c(.9,-.2),ma=c(-.7,.1)), n=200)
ma5 <- arima.sim(model=list(ma=c(0.5, 0.3, 0.2, 0.1, 0.01)), n=10)
ar3 <- arima.sim(model=list(ar=c(.9,-.2, 0.1)), n=20)
acf(arima.sim(list(order=c(1,0,0), ar=0.9), n=100), lag.max=11, main='ACF')
plot(arima.sim(list(order=c(1,0,0), ar=0.9), n=100), ylab='x', main=(expression(AR(1)~~~phi==+0.9)))
armafit <- auto.arima(localpred)
sarima(data, 1, 0, 0, )
sarima(flow, 1,0,0,0,1,1,12)
adjreg = sarima (lvarve, 1,0,0, xreg=cbind(trend,trend2))
sarima.for(flow, 24, 1,0,0,0,1,1,12)
themodel = arima(flow, order = c(1,0,0), seasonal = list(order = c(0,1,1), period = 12))

accuracy(air.model)
accuracy(fcast, outdata)
MAPE_class_dec <- accuracy(fcast, outdata[,2])[5]
MAPE_auto_arima <- accuracy(fcast_auto_arima$pred, outdata)[5]
accuracy(forecast(air.model, h=48, lambda=NULL), log(window(AirPassengers, start=1957)))
fcast <- forecast(fit2, h=30)
decompose(x, type = c("additive", "multiplicative"), filter = NULL)
decompbeer = decompose(beerprod, type="additive")
decombeermult = decompose(beerprod, type = "multiplicative")
ap.decom = decompose(AirPassengers, type="mult")
ap.decom$trend
ap.decom$seasonal

plot(decompose_beer)
plot(as.ts(decompose_beer$seasonal))
plot(as.ts(decompose_beer$trend))
plot(as.ts(decompose_beer$random))
plot.forecast(fc)
plot.ts(fc$residuals)
hist(resid(model))
tsclean(x, replace.missing = TRUE, lambda = NULL)
tsdisplay(residuals(fit), lag.max=45, main='(1,1,1) Model Residuals')
daily_data$clean_cnt = tsclean(count_ts)
daily_data$cnt_ma30 = ma(daily_data$clean_cnt, order=30)
decomp = stl(count_ma, s.window="periodic")
deseasonal_cnt <- seasadj(decomp)
kingstimeseriesSMA3 <- SMA(kingstimeseries,n=3)
Box.test(rainseriesforecasts2$residuals, lag=20, type="Ljung-Box")

require(timeSeries)
plot(tS3, plot.type="s") # s - single
plot(tS3, plot.type="m") # m - multiple
plot(tS3, plot.type="m", mar=c(gap=0.3, 5.1, gap=0.3, 2.1))
plot(tS3, plot.type="s", format="%b/%y", at=at)
plot(tS6, plot.type="s", ann=FALSE, at="pretty")
plot(tS1, plot.type="s", minor.ticks="month", at = "chic")
plot(tS3, at="chic", plot.type="s", cex.axis=1.25, lwd=3:1)
plot(tS3, plot.type="m",  type=c("l", "p", "h"), yax.flip = TRUE)
plot(tS6, plot.type="s", col=heat.colors(n=6, alpha = 1), at="chic", format = "%B\n%Y")
plot(tS3, plot.type="s", type="p", col=1:3, pch=21:23, cex.pch=c(0.2, 0.2, 0.2), at="pretty")
plot(tS3, plot.type="s", frame.plot=FALSE, grid=FALSE)
plot(returns(tS6), plot.type="m", col = .colorwheelPalette(6), panel=panel_function, at="pretty")

library(PerformAnalytics)
chart.TimeSeries(R, auto.grid = TRUE, xaxis = TRUE, yaxis = TRUE, yaxis.right = FALSE, type = "l", lty = 1, lwd = 2, las = par("las"), main = NULL, ylab = NULL, xlab = "", date.format.in = "%Y-%m-%d", date.format = NULL, xlim = NULL, ylim = NULL, element.color = "darkgray", event.lines = NULL, event.labels = NULL, period.areas = NULL, event.color = "darkgray", period.color = "aliceblue", colorset = (1:12), pch = (1:12), legend.loc = NULL, ylog = FALSE, cex.axis = 0.8, cex.legend = 0.8, cex.lab = 1, cex.labels = 0.8, cex.main = 1, major.ticks = "auto", minor.ticks = TRUE, grid.color = "lightgray", grid.lty = "dotted", xaxis.labels = NULL, ...)
chart.TimeSeries(Return.cumulative)
chart.TimeSeries(ts, period.areas=c("2015-01/2017-07", main="My TS")
chart.TimeSeries(data[,3], period.areas=period, period.color = "lightgrey")
chart.TimeSeries(Return.cumulative, colorset = "darkblue", legend.loc = "bottomright", period.areas = cycles.dates, period.color = "lightblue", event.lines = risk.dates, event.labels = risk.labels, event.color = "red", lwd = 2)


xts  # eXtensible Time Series package in R, xts objects as matrices
library(xts)
xts(x = NULL, order.by = index(x), frequency = NULL, unique = TRUE, tzone = Sys.getenv("TZ"), ...)
X <- xts(x, order.by=idx)
index must be increasing order of date/time
hayek <- xts(x = data, order.by = dates, born = bday)
a <- xts(x = 1:2, as.Date("2017-01-01") + 0:1)

coredata(x, fmt=FALSE)
hayek_core <- coredata(hayek)
xcoredata(x,...)
xcoredata(x) <- value
xcoredata(x)

index(x, ...)
index(x) <- value
hayek_index <- index(hayek)
index(x) <- 1:5
a[index(a)]

is.xts(x)
as.xts()
sample.xts <- as.xts(sample_matrix, descr='my new xts object')
x <- as.xts(sample_matrix, myattr=100)
sss_xts <- as.xts(sunspots)
as.xts(read.table("file"))
xtsible(x)

read.zoo(file, format = "", tz = "", FUN = NULL, regular = FALSE, index.column = 1, drop = TRUE, FUN2 = NULL, split = NULL, aggregate = FALSE, ..., text)
as.xts(read.zoo("file"))
dat_zoo <- read.zoo(tmp_file, index.column = 0, sep = ",", format = "%m/%d/%Y")
sun <- read.zoo(tmp, sep = ",", FUN = as.yearmon)
read.zoo(text = Lines)
read.zoo(text = Lines, index = 1:2, tz = "")

write.zoo(x, file = "", index.name = "Index", row.names = FALSE, col.names = NULL, ...)
write.zoo(x,"file")
write.zoo(sunspots_xts, sep = ",", file = tmp)

saveRDS(object, file = "", ascii = FALSE, version = NULL, compress = TRUE, refhook = NULL)
saveRDS(x, "file")
saveRDS(women, "women.rds")
readRDS(file, refhook = NULL)
women2 <- readRDS("women.rds")
identical(women, readRDS("women2"))

con <- gzfile("women.rds")
con <- gzfile("women.rds", "rb")
con <- file("women32", "w")
con <- bzfile("women8", "w")
identical(unserialize(wm), women)

Reclass(x)
try.xts(x, ..., error = TRUE)
str( try.xts(x) )
reclass(x, match.to, error = FALSE, ...)
str( reclass(as.xts(x)) )
attr(sample.xts,'descr')
sample.xts['2007-07-06']

plot.xts()

zoo Package in R
zoo - [Zeileis’ Ordered Observations]
zoo(x = NULL, order.by = index(x), frequency = NULL)
x <- zoo(rnorm(5), x.date)
x <- zoo(rnorm(5), c(1, 3, 7, 9, 14))
z0 <- zoo(, 1:4)
z2 <- zoo(matrix(1:12, 4, 3), as.Date("2013-08-01") + 0:3)
z20 <- zoo(matrix(nrow = 4, ncol = 0), 1:4)
x2 <- zoo(2:6, 2:6)
z <- zoo(cbind(foo = rnorm(5), bar = rnorm(5)))
z4 <- zoo(11:15, complex(real = c(1, 3, 4, 5, 6), imag = c(0, 1, 0, 0, 1)))
z <- zoo(1:10, yearqtr(seq(1969.25, 1972.5, by = 0.25)), frequency = 4)
z[-(3:4)]
as.zoo(ts(1:10, frequency = 4, start = c(1969, 2)))

start(x)
end(x)
getTime(x)        
setTime(x) <- value
setTime(X) <- time(X) - 24*3600
"time"(x, ...)
"time"(x) <- value
time(x)
 time(X) <- time(X) + 24*3600

"merge"(..., all = TRUE, fill = NA, suffixes = NULL, check.names = FALSE, retclass = c("zoo", "list", "data.frame"), drop = TRUE)
merge(z5, lag(z5))
merge(y1, y2, all = TRUE)
merge(z, coredata(z),  1)
Data <- do.call(merge, lapply(split(DF, DF$site), tozoo))

zooreg(data, start = 1, end = numeric(), frequency = 1,  deltat = 1, ts.eps = getOption("ts.eps"), order.by = NULL)
zzr <- as.zooreg(zz)
zz <- zooreg(cbind(a = 1:10, b = 11:20), start = as.yearqtr(2010), freq = 4)
zooreg(1:10, frequency = 4, start = c(1989, 2))
zooreg(1:5, end = yearmon(2010))
zr <- as.zooreg(zz)
zrd <- as.zooreg(zoo(1:4, dd))
lag(zr, k = -1, na.pad = TRUE)

Related aRticles: R dplyr package     R Pipe operator (%>%)


No comments:

Post a Comment