basal area: how much wood does a woodland produce?
BAI: basal area increment (how much additional wood does this area produce)
P: precipitaton (rain)
T: temperature
In [18]:
### Load time series packages
library(tseries)
library(TSA)
library(forecast)
In [9]:
bai <- read.table("http://www.escet.urjc.es/biodiversos/R/BAI.csv", header=T, sep="\t")
In [10]:
head(bai)
In [14]:
bai.ts <- ts(data=bai$BAI, start=min(bai$year), frequency=1)
In [16]:
plot(bai.ts, ylab="basal area increase")
In [40]:
dendro.ts <- ts(data=bai[2:4], start=min(bai$year))
plot(dendro.ts)
In [42]:
acf(bai.ts, lag=40) # slow decrease -> me might have to integrate first
In [43]:
bai.ts.diff <- diff(bai.ts, 1)
In [44]:
acf(bai.ts.diff, lag=40) # we have success. converted this into a stationary ts
In [46]:
# we don't expect seasonality
# what do acf / pacf tell us?
# acf: one signif. corr
# pacf: expo decrease -> MA(1)
pacf(bai.ts.diff, lag=40)
In [28]:
## Time series plot
par(mfcol=c(2,1)) # plot 4 graphs in a 2x2 matrix
plot(bai.ts, ylab="", xlab="")
title(main="Original, non-stationary time series")
# plot(diff(bai.ts, lag=12), ylab="", xlab="")
# title(main="One seasonal difference")
plot(diff(bai.ts, lag=1), ylab="", xlab="")
title(main="One regular difference (i.e. detrended)")
# plot(diff(diff(bai.ts, lag=1), lag=12), ylab="", xlab="")
# title(main="One regular + one\n seasonal difference")
In [39]:
par(mfcol=c(3,1))
plot(bai.ts, main="bai")
acf(bai.ts, ylab="", xlab="", main="acf of bai")
pacf(bai.ts, ylab="", xlab="", main="pacf of bai")
In [20]:
best.arima<-auto.arima(bai.ts, d=1, D=1, max.p=5, max.q=5, max.P=2, max.Q=2)
best.arima # MA(1) w/out seasonal component
In [29]:
### ACF and PACF of the standardized residuals
par(mfcol=c(2,1))
# We should not see any pattern in the residuals.
acf(rstandard(best.arima), xlab="", ylab="", main="ACF of standardized residuals")
pacf(rstandard(best.arima), xlab="", ylab="", main="PACF of standardized residuals")
In [30]:
### Plot of standardized residuals
# most residuals are within the SD, so the residuals are normaly distributed
plot(rstandard(best.arima), xlab="", ylab="", main="", type="o", ylim=c(-3,3))
abline(h=2, lty=3, col="red")
abline(h=-2, lty=3, col="red")
In [34]:
# there's another function called plot.Arima, so we'll have to use the namespace
TSA::plot.Arima(best.arima, n.ahead=10, type='l')
In [33]:
class(best.arima)
In [35]:
str(best.arima)
In [57]:
arima.dendro <- arima(bai.ts, order=c(0,1,1))
In [58]:
arima.dendro
In [49]:
# we could add a regression b/c there's collection with rain and temperature
In [55]:
arima.dendroreg <- arima(bai.ts, order=c(0,1,1), xreg=bai$P)
In [56]:
arima.dendroreg
In [59]:
tsdiag(arima.dendro)
In [60]:
tsdiag(arima.dendroreg)
In [61]:
# forecasting
In [62]:
# we have no idea about future precipitation
# we simply make it up, but we could use another model for that
TSA::plot.Arima(arima.dendroreg, n.ahead = 20, newxreg=rnorm(20, mean=-0.5, sd=0.25))
In [ ]: