Você está na página 1de 3

Time Series Laboratory Exercise I1

ASAAD, Al-Ahmadgaid B. June 29, 2012


I. Look for a data that will follow a rst order autoregressive model, AR(1). Source of the Data: Cowpertwait, P. S.P. and Metcalfe, A. V., "Introductory Time Series with R (Use R)", 2009. website: http://www.massey.ac.nz/~pscowper/ts/ Data: http://www.massey.ac.nz/~pscowper/ts/pounds_nz.dat And here are the rst ve observations taken from the data.
xrate 2.9243 2.9422 3.1719 3.2542 3.3479

ASAAD in the cloud website: www.alstat.weebly.com blog: www.alstatr.blogspot.com email: alstated@gmail.com


1

R Computation
To obtain a nice historical plot of the data, it is better to add another variable "time", which is divided into four quarters a year. And heres the addition to the above rst ve data.
xrate 2.9243 2.9422 3.1719 3.2542 3.3479 time 1991-03-01 00:00:00 1991-06-01 00:00:00 1991-09-01 00:00:00 1991-12-01 00:00:00 1992-03-01 00:00:00

To plot it just enter the following codes,


library(ggplot2) Data <- read.csv("TimeSData.csv", header = TRUE) DataWT <- as.Date(Data$time) qplot(DataWT, Data$xrate, data=Data) + geom_line(size=1, colour="red") + xlab(expression(bold("Time: Four Quarter a Year"))) + ylab(expression(bold("Exchange Rate"))) +

Note that the data was imported rst from a CSV le format to R, with a le name "TimeSData.csv".

time series laboratory exercise i

opts(title=expression(bold("Exchange Rate (New Zealand Dollar per UK Pound)")), plot.title=theme_text(size = 12, colour = "darkblue"))

Exchange Rate (New Zealand Dollar per UK Pound)


3.5
q q q q q q q q q q q q

Figure 1: Historical Plot of the Exchange Rate (New Zealand Dollar per UK Pound) Data.

q q

Exchange Rate

3.0
q q

q q

q
1.0

q q q q
Autocorrelation
0.5

q q q q q q q q q q q q q

2.5

0.0

0.5 0 5 10 15

Lag

1992

1994

1996

1998

2000

Time: Four Quarter a Year

Significant at the 0.95 level

False

True

What is the order of the AR?


Data.ar <- ar(Data$xrate) Data.ar$order [1] 1

Figure 2: The Autocorrelation Function Plot of the Exchange Rate (New Zealand Dollar per UK Pound) Data, generated with the following codes
qacf(xrate) + theme_bw() + xlab(expression(bold("Lag"))) + ylab(expression(bold(" Autocorrelation"))) + opts(panel.border = theme_rect(size = 2, colour = "red"), legend.position="bottom")

Since there is no functions for acf and pacf in ggplot2 package of R. Then, the codes below will build a function qacf (ggplot2 acf plot function) and qpacf (ggplot2 pacf plot function).
qacf <- function(x, conf.level = 0.95, max.lag = NULL, min.lag = 0, title = "") { ciline <- qnorm((1 - conf.level)/2)/sqrt(length(x)) bacf <- acf(x, plot = FALSE, lag.max = max.lag) bacfdf <- with(bacf, data.frame(lag, acf)) if (min.lag > 0) { bacfdf <- bacfdf[-seq(1, min.lag), ] } significant <- (abs(bacfdf[, 2]) > abs(ciline))^2 bacfdf <- cbind(bacfdf, significant) q <- qplot(lag, acf, data = bacfdf, geom = "bar", stat = "identity", position = "identity", ylab = "Autocorrelation", main = title,

time series laboratory exercise i

fill = factor(significant)) q <- q + geom_hline(yintercept = -ciline, color = "blue", size = 0.2) q <- q + geom_hline(yintercept = ciline, color = "blue", size = 0.2)
0.8

Partial Autocorrelation

q <- q + geom_hline(yintercept = 0, color = "red", size = 0.3) q <- q + scale_fill_hue(name = paste("Significant at the\n", conf.level, "level"), breaks = 0:1, labels = c("False", "True")) return(q) }

0.6

0.4

0.2

0.0

0.2

10

15

Lag
Significant at the 0.95 level False True

qpacf <- function(x, conf.level = 0.95, max.lag = NULL, min.lag = 0, title = "") { ciline <- qnorm((1 - conf.level)/2)/sqrt(length(x)) bacf <- pacf(x, plot = FALSE, lag.max = max.lag) bacfdf <- with(bacf, data.frame(lag, acf)) if (min.lag > 0) { bacfdf <- bacfdf[-seq(1, min.lag), ] } significant <- (abs(bacfdf[, 2]) > abs(ciline))^2 bacfdf <- cbind(bacfdf, significant) q <- qplot(lag, acf, data = bacfdf, geom = "bar", stat = "identity", position = "identity", ylab = "Autocorrelation", main = title, fill = factor(significant)) q <- q + geom_hline(yintercept = -ciline, color = "blue", size = 0.2) q <- q + geom_hline(yintercept = ciline, color = "blue", size = 0.2) q <- q + geom_hline(yintercept = 0, color = "red", size = 0.3) q <- q + scale_fill_hue(name = paste("Significant at the\n", conf.level, "level"), breaks = 0:1, labels = c("False", "True")) return(q)}

Figure 3: The Partial Autocorrelation Function Plot of the Exchange Rate (New Zealand Dollar per UK Pound) Data, generated with the following codes
qpacf(xrate) + theme_bw() + xlab(expression(bold("Lag"))) + ylab(expression(bold(" Partial Autocorrelation"))) + opts(panel.border = theme_rect(size = 2, colour = "red"), legend.position="bottom")

Você também pode gostar