Escolar Documentos
Profissional Documentos
Cultura Documentos
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
Note that the data was imported rst from a CSV le format to R, with a le name "TimeSData.csv".
opts(title=expression(bold("Exchange Rate (New Zealand Dollar per UK Pound)")), plot.title=theme_text(size = 12, colour = "darkblue"))
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
False
True
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,
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")