Thursday, September 11, 2014

Basic Time Series with R



Currently play with the Port Jervis, New York annual maximum and minimum winter temperature data, provided with the extRemes R package

Extreme value deal with rare events which are unlikely to follow patterns and assumptions common with complete event view, but standard initial analysis tools can still provide insight.

library(extremes)
data("PORTw")

plot(PORTw$TMX1, type = "l", xlab = "Year", ylab = "Maximum winter temperature", col = "red")
plot(PORTw$TMX0, type = "l", xlab = "Year", ylab = "Maximum winter temperature", col = "darkblue")


Visual inspection

  • Trend - none
  • Cycle - none
  • Clustering- none
  • Pair wise correlation - maybe


Both are non-cyclic, and can probably be described using an additive model, since the random fluctuations in the data are roughly constant in size over time:

Exponential Model

Lets try fitting a Simple Exponential Smoothing
convert to a time series object
> maxts <- ts(PORTw$TMX1, start=c(1927))
Fit  model  with no trend or cycle
> fit1 <- HoltWinters(maxts, beta=FALSE, gamma=FALSE)
> fit1
Holt-Winters exponential smoothing without trend and without seasonal component.
Call:
HoltWinters(x = maxts, beta = FALSE, gamma = FALSE)
Smoothing parameters:
 alpha: 0.1368945
 beta : FALSE
 gamma: FALSE
Coefficients:
      [,1]
a 16.49764
plot(fit1)
Simple measure of fit. sum-of-squared-errors 
fit1$SSE
[1] 763.3207 

ARIMA Model

Difference the times series
maxtsdiff <- diff(maxts, differences = 1)

acf(maxtsdiff, lag.max = 20)


Basic visual inspection, may something at 1 year lag.

 maxtsarima <- arima(maxts, order=c(0,1,1))
> maxtsarima
Series: maxts
ARIMA(0,1,1)                  
Coefficients:
          ma1
      -1.0000

s.e.   0.0532
sigma^2 estimated as 9.636:   
log likelihood=-173.07 
AIC=350.15   
AICc=350.33  

BIC=354.56 
And let R work out if and what order ARIMA would be appropriate

> mtsARIMA <- auto.arima(maxts)
> mtsARIMA
Series: maxts
ARIMA(0,0,0) with non-zero mean
Coefficients:
      intercept
        16.3154
s.e.     0.3737
sigma^2 estimated as 9.494:
 
log likelihood=-173.01
AIC=350.02  
AICc=350.21  
          BIC=354.46

No moving average happening

Pair Wise Correlation

Start with scatter plots




Check correlation coefficients

    > cor(PORTw$TMX1, PORTw$TMN0)
[1] 0.1802413> cor(PORTw$TMX1, PORTw$AOindex)[1] 0.3944286> cor(PORTw$TMN0, PORTw$AOindex)[1] 0.01206764> cor(PORTw$TMX1, PORTw$AOindex, method="kendall")[1] 0.3019692

Possibly something to work with Arctic Oscillation Index and Maximium winter temperature 

No comments:

Post a Comment