-
Notifications
You must be signed in to change notification settings - Fork 7
/
ACF.R
659 lines (529 loc) · 22 KB
/
ACF.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
################################
### Theoretical ACF / PACF
################################
# hidden casting function
cast_acf = function(object, n, name_ = "Empirical", type = "Autocorrelation",
class = "theo_arma"){
# Force to array
if(is.null(nrow(object))){ dim(object) = c(length(object), 1, 1)}
# Make pretty names
ids = seq_len(nrow(object))
if(type == "Autocorrelation" || type == "Autocovariance"){
ids = ids - 1
}
if("PACF" %in% class){ ids = seq_len(nrow(object)) }
dimnames(object) = list(ids, name_, name_)
structure(object, type = type, n = n, class = class)
}
# Work horse of the two functions for theoretical ACF & PACF
theo_arma_ = function(model, lagmax = 20, pacf = FALSE){
if(!is.ts.model(model)){ stop("`model` must be a `ts.model` object.")}
if(model$starting){ stop("`model` must have specific parameter values.")}
if(length(model$desc) != 1 && model$desc != "SARIMA"){ stop("`model` must contain only 1 process.")}
objdesc = model$objdesc[[1]]
ar = model$theta[model$process.desc == "AR"]
ma = model$theta[model$process.desc == "MA"]
if(is.null(lagmax)){ lagmax = max(length(ar), length(ma) + 1) + 2}
if(pacf == TRUE){
myclass = c("PACF", "array")
}else{
myclass = c("simtsACF", "array")
}
cast_acf(ARMAacf(ar = ar, ma = ma, lag.max = lagmax, pacf = pacf),
n = lagmax,
name_ = "Theoretical",
type = "Autocorrelation",
class = myclass)
}
#' @title Theoretical Autocorrelation (ACF) of an ARMA process
#' @description This function computes the theoretical Autocorrelation (ACF) of an ARMA process.
#' @param ar A \code{vector} containing the AR coefficients.
#' @param ma A \code{vector} containing the MA coefficients.
#' @param lagmax An \code{integer} indicating the maximum lag up to which to compute the theoretical ACF.
#' @author Yuming Zhang
#' @importFrom stats ARMAacf
#' @examples
#' # Compute the theoretical ACF for an ARMA(1,0) (i.e. a first-order autoregressive model: AR(1))
#' theo_acf(ar = -0.25, ma = NULL)
#' # Computes the theoretical ACF for an ARMA(2, 1)
#' theo_acf(ar = c(.50, -0.25), ma = 0.20, lagmax = 10)
#' @export
theo_acf = function(ar, ma = NULL, lagmax = 20){
model = ARMA(ar=ar, ma=ma)
theo_arma_(model = model, lagmax = lagmax, pacf = FALSE)
}
#' @title Theoretical Partial Autocorrelation (PACF) of an ARMA process
#' @description This function computes the theoretical Partial Autocorrelation (PACF) of an ARMA process.
#' @param ar A \code{vector} containing the AR coefficients.
#' @param ma A \code{vector} containing the MA coefficients.
#' @param lagmax An \code{integer} indicating the maximum lag up to which to compute the theoretical PACF.
#' @author Yuming Zhang
#' @export
#' @examples
#' # Computes the theoretical ACF for an ARMA(1,0) (i.e. a first-order autoregressive model: AR(1))
#' theo_pacf(ar = -0.25, ma = NULL, lagmax = 7)
#' # Computes the theoretical ACF for an ARMA(2, 1)
#' theo_pacf(ar = c(.50, -0.25), ma = .20, lagmax = 10)
theo_pacf = function(ar, ma = NULL, lagmax = 20){
model = ARMA(ar=ar, ma=ma)
theo_arma_(model = model, lagmax = lagmax, pacf = TRUE)
}
################################
### Empirical ACF / PACF
################################
# ----- wrapper function
#' @title Empirical ACF and PACF
#' @description This function can estimate either the autocovariance / autocorrelation for univariate time series,
#' or the partial autocovariance / autocorrelation for univariate time series.
#' @author Yuming Zhang
#' @param x A \code{vector} or \code{ts} object (of length \eqn{N > 1}).
#' @param lag.max An \code{integer} indicating the maximum lag up to which to compute the empirical ACF / PACF.
#' @param pacf A \code{boolean} indicating whether to output the PACF.
#' If it's \code{TRUE}, then the function will only estimate the empirical PACF. If it's \code{FALSE} (the default),
#' then the function will only estimate the empirical ACF.
#' @param type A \code{character} string giving the type of acf to be computed. Allowed values are "correlation" (the default) and "covariance".
#' @param demean A \code{boolean} indicating whether the data should be detrended (\code{TRUE}) or not (\code{FALSE}). Defaults to \code{TRUE}.
#' @param robust A \code{boolean} indicating whether a robust estimator should be used (\code{TRUE}) or not (\code{FALSE}). Defaults to \code{FALSE}.
#' This only works when the function is estimating ACF.
#' @return An \code{array} of dimensions \eqn{N \times 1 \times 1}{N x 1 x 1}.
#' @details
#' \code{lagmax} default is \eqn{10*log10(N/m)} where \eqn{N} is the number of
#' observations and \eqn{m} is the number of time series being compared. If
#' \code{lagmax} supplied is greater than the number of observations N, then one
#' less than the total will be taken (i.e. N - 1).
#' @importFrom stats acf pacf mad
#' @importFrom robcor robacf
#' @export
#'
#' @examples
#' m = auto_corr(datasets::AirPassengers)
#' m = auto_corr(datasets::AirPassengers, pacf = TRUE)
auto_corr = function(x, lag.max = NULL, pacf = FALSE, type = "correlation", demean = TRUE, robust = FALSE){
if(pacf == FALSE){
ACF(x, lag.max = lag.max, type = type, demean = demean, robust = robust)
}else{
PACF(x, lag.max = lag.max, type = type, demean = demean)
}
}
# ------ empirical ACF
ACF = function(x, lag.max = NULL, type = "correlation", demean = TRUE, robust = FALSE){
# Change the data to matrix form
if(is.ts(x) || is.atomic(x)){
x2 = data.matrix(x)
}
if (type != "correlation" && type != "covariance"){
stop("Type not authorized. Allowed values correlation and covariance.")
}
if (demean){
x3 = as.matrix(x)
x3 = sweep(x3, 2, colMeans(x3, na.rm = TRUE), check.margin = FALSE)
}
# Get the acf value of the data
if (robust == FALSE){
acfe = acf(x3, lag.max = lag.max, type = type, plot = FALSE)
}else{
if (type == "correlation"){
acfe = robacf(x3, lag.max = lag.max, plot = FALSE)
}else{
sig2 = mean(abs(x3 - mean(x3)))*sqrt(pi/2)
acfe = sig2*robacf(x3, lag.max = lag.max, plot = FALSE)
}
}
acfe = acfe$acf
# Get the data name
varName = deparse(substitute(x))
# Adjust the name for data
dimnames(acfe) = list(seq_len(nrow(acfe))-1, "ACF", varName)
if (is.null(attr(x, "data_name"))){
acfe = structure(acfe, n = nrow(x2), class = c("simtsACF", "array"))
}else{
acfe = structure(acfe, n = nrow(x2), data_name = attr(x, "data_name"), class = c("simtsACF", "array"))
}
unit_time = attr(x, "unit_time")
if (!is.null(unit_time)){
attr(acfe, "unit_time") = unit_time
}
if(!is.null(names(x))){
attr(acfe, "data_name") = names(x)
}
acfe
}
#' @title Plot Auto-Covariance and Correlation Functions
#' @description The function plots the output of the \code{theo_acf} and \code{auto_corr} functions (autocovariance or autocorrelation functions).
#' @author Yunxiang Zhang, Stéphane Guerrier and Yuming Zhang
#' @param x An \code{"ACF"} object output from \code{theo_acf} and \code{auto_corr}.
#' @param xlab A \code{string} indicating the label of the x axis: the default name is 'Lags'.
#' @param ylab A \code{string} indicating the label of the y axis: the default name is 'ACF'.
#' @param show.ci A \code{bool} indicating whether to show the confidence region. Defaults to \code{TRUE}.
#' @param alpha A \code{double} indicating the level of significance for the confidence interval. By default \code{alpha = 0.05} which gives a 1 - \code{alpha} = 0.95 confidence interval.
#' @param col_ci A \code{string} that specifies the color of the region covered by the confidence intervals (confidence region).
#' @param transparency A \code{double} between 0 and 1 indicating the transparency level of the color defined in \code{col_ci}.
#' Defaults to 0.25.
#' @param main A \code{string} indicating the title of the plot. Default name is "Variable name ACF plot'.
#' @param parValue A \code{vector} defining the margins for the plot.
#' @param ... Additional parameters
#' @rdname plot.simtsACF
#' @export
#' @importFrom grDevices col2rgb
#' @examples
#' # Calculate the Autocorrelation
#' m = auto_corr(datasets::AirPassengers)
#'
#' # Plot with 95% CI
#' plot(m)
#'
#' # Plot with 90% CI
#' plot(m, alpha = 0.1)
#'
#' # Plot without 95% CI
#' plot(m, show.ci = FALSE)
#'
#' # More customized CI
#' plot(m, xlab = "my xlab", ylab = "my ylab", show.ci = TRUE,
#' alpha = NULL, col_ci = "grey", transparency = 0.5, main = "my main")
plot.simtsACF = function(x, xlab = NULL, ylab = NULL, show.ci = TRUE, alpha = NULL, col_ci = NULL, transparency = NULL, main = NULL, parValue = NULL, ...){
# TO ADD AS INPUTS
lag_unit = attr(x, "unit_time")
# add xlab
if (!is.null(xlab)){
xlab = xlab
}else{
if (!is.null(lag_unit)){
xlab = paste("Lags (", lag_unit,")", sep = "")
}else{
xlab = "Lags"
}
}
# add ylab
if (!is.null(ylab)){
ylab = ylab
}else{
ylab = "ACF"
}
# add alpha
if (!is.null(alpha)){
alpha = alpha
}else{
alpha = 0.05
}
# add transparency
if (!is.null(transparency)){
transparency = transparency
}else{
transparency = 0.25
}
# add color of CI
if (!is.null(col_ci)){
col_ci = col2rgb(col_ci)
col_ci = rgb(col_ci[1], col_ci[2], col_ci[3], transparency*255, maxColorValue = 255)
}else{
col_ci = rgb(red = 0, green = 0.6, blue = 1, transparency)
}
# Quiet the warnings...
Lag = xmin = xmax = ymin = ymax = NULL
# Wide to long array transform
x2 = as.data.frame.table(x, responseName = "ACF")
colnames(x2) = c("Lag", "Signal X", "Signal Y", "ACF")
# Remove character cast
x2$Lag = as.numeric(x2$Lag)
# Range
x_range = range(x2$Lag)-1
# Remove confidence intervals for theoretical ACF
if (attr(x, "dimnames")[[2]] == "Theoretical"){
show.ci = FALSE
}
if (show.ci == TRUE){
n = attr(x,"n")
mult = qnorm(1-alpha/2)
y_range = range(c(x2$ACF, 1/sqrt(n)*mult*c(-1,1)))
}else{
y_range = range(x2$ACF)
}
x_ticks = seq(x_range[1], x_range[2], by = 1)
y_ticks = seq(y_range[1], y_range[2], by = 0.05)
if (!is.null(parValue)){
par(mar = parValue)
}else{
par(mar = c(5.1, 5.1, 1, 2.1))
}
# Title
if (is.null(main)){
if (is.null(attr(x,"data_name"))){
main = paste0(as.character((x2$`Signal Y`)[1]), " ACF plot")
}else{
main = paste0(attr(x,"data_name")[1], " ACF plot")
}
}else {
main = main
}
# Main plot
plot(NA, xlim = c(0, max(x2$Lag)), ylim = y_range,
xlab = xlab, ylab = ylab, xaxt = 'n',
yaxt = 'n', bty = "n", ann = FALSE)
win_dim = par("usr")
par(new = TRUE)
plot(NA, xlim = c(0, max(x2$Lag)), ylim = c(win_dim[3], win_dim[4] + 0.09*(win_dim[4] - win_dim[3])),
xlab = xlab, ylab = ylab, xaxt = 'n', yaxt = 'n', bty = "n")
win_dim = par("usr")
# Add grid
grid(NULL, NULL, lty = 1, col = "grey95")
# Add title
x_vec = c(win_dim[1], win_dim[2], win_dim[2], win_dim[1])
y_vec = c(win_dim[4], win_dim[4],
win_dim[4] - 0.09*(win_dim[4] - win_dim[3]),
win_dim[4] - 0.09*(win_dim[4] - win_dim[3]))
polygon(x_vec, y_vec, col = "grey95", border = NA)
text(x = mean(c(win_dim[1], win_dim[2])),
y = (win_dim[4] - 0.09/2*(win_dim[4] - win_dim[3])),
main)
# Add axes and box
lines(x_vec[1:2], rep((win_dim[4] - 0.09*(win_dim[4] - win_dim[3])),2), col = 1)
box()
axis(1, padj = 0.3)
y_axis = axis(2, labels = FALSE, tick = FALSE)
y_axis = y_axis[y_axis < (win_dim[4] - 0.09*(win_dim[4] - win_dim[3]))]
axis(2, padj = -0.2, at = y_axis)
abline(h = 0, lty = 1, lwd = 2)
# Plot CI
if(show.ci){
clim0 = 1/sqrt(n)*mult
rect(xleft = -2, ybottom = -clim0, xright = 2*x_range[2], ytop = clim0,
col = col_ci, lwd = 0)
}
# Plot ACF
segments(x0 = x_ticks, y0 = rep(0, x_range[2]), x1 = x_ticks, y1 = x2$ACF, lty = 1, lwd = 1)
}
# ----- empirical PACF
PACF = function(x, lag.max = NULL, type = "correlation", demean = TRUE){
if (type != "correlation" && type != "covariance"){
stop("Type not authorized. Allowed values correlation and covariance.")
}
# Change the data to matrix form
if(is.ts(x) || is.atomic(x)){
x2 = data.matrix(x)
}
if (demean){
x3 = as.matrix(x)
x3 = sweep(x3, 2, colMeans(x3, na.rm = TRUE), check.margin = FALSE)
}
#Get the pacf value of the data
pacfe = pacf(x3, lag.max = lag.max, type = type, plot = FALSE)
pacfe = pacfe$acf
# Get the data name
varName = deparse(substitute(x))
# Adjust the name for data
dimnames(pacfe) = list(seq_len(nrow(pacfe))-1, "PACF", varName)
if (is.null(attr(x, "data_name"))){
pacfe = structure(pacfe, n = nrow(x2), class = c("PACF", "array"))
}else{
pacfe = structure(pacfe, n = nrow(x2), data_name = attr(x, "data_name"), class = c("PACF", "array"))
}
unit_time = attr(x, "unit_time")
if (!is.null(unit_time)){
attr(pacfe, "unit_time") = unit_time
}
if(!is.null(names(x))){
attr(pacfe, "data_name") = names(x)
}
pacfe
}
#' @title Plot Partial Auto-Covariance and Correlation Functions
#' @description The function plots the output of the \code{\link{theo_pacf}} and \code{\link{auto_corr}} functions (partial autocovariance or autocorrelation functions).
#' @author Yunxiang Zhang and Yuming Zhang
#' @param x A \code{"PACF"} object output from \code{\link{theo_pacf}} or \code{\link{auto_corr}}.
#' @param xlab A \code{string} indicating the label of the x axis: the default name is 'Lags'.
#' @param ylab A \code{string} indicating the label of the y axis: the default name is 'PACF'.
#' @param show.ci A \code{bool} indicating whether to show the confidence region. Defaults to \code{TRUE}.
#' @param alpha A \code{double} indicating the level of significance for the confidence interval. By default \code{alpha = 0.05} which gives a 1 - \code{alpha} = 0.95 confidence interval.
#' @param col_ci A \code{string} that specifies the color of the region covered by the confidence intervals (confidence region).
#' @param transparency A \code{double} between 0 and 1 indicating the transparency level of the color defined in \code{col_ci}.
#' Defaults to 0.25.
#' @param main A \code{string} indicating the title of the plot. Default name is "Variable name PACF plot'.
#' @param parValue A \code{vector} defining the margins for the plot.
#' @param ... Additional parameters
#' @rdname plot.PACF
#' @importFrom grDevices col2rgb
#' @export
#' @examples
#' # Plot the Partial Autocorrelation
#' m = auto_corr(datasets::AirPassengers, pacf = TRUE)
#' plot(m)
#'
#' # More customized CI
#' plot(m, xlab = "my xlab", ylab = "my ylab", show.ci = TRUE,
#' alpha = NULL, col_ci = "grey", transparency = 0.5, main = "my main")
plot.PACF = function(x, xlab = NULL, ylab = NULL, show.ci = TRUE, alpha = NULL, col_ci = NULL, transparency = NULL, main = NULL, parValue = NULL, ...){
# TO ADD AS INPUTS
lag_unit = attr(x, "unit_time")
# add xlab
if (!is.null(xlab)){
xlab = xlab
}else{
if (!is.null(lag_unit)){
xlab = paste("Lags (", lag_unit,")", sep = "")
}else{
xlab = "Lags"
}
}
# add ylab
if (!is.null(ylab)){
ylab = ylab
}else{
ylab = "PACF"
}
# add alpha
if (!is.null(alpha)){
alpha = alpha
}else{
alpha = 0.05
}
# add transparency
if (!is.null(transparency)){
transparency = transparency
}else{
transparency = 0.25
}
# add color of CI
#col_ci = rgb(0, 0.6, 1, 0.2)
if (!is.null(col_ci)){
col_ci = col2rgb(col_ci)
col_ci = rgb(col_ci[1], col_ci[2], col_ci[3], transparency*255, maxColorValue = 255)
}else{
col_ci = rgb(red = 0, green = 0.6, blue = 1, transparency)
}
# Quiet the warnings...
Lag = xmin = xmax = ymin = ymax = NULL
# Wide to long array transform
x2 = as.data.frame.table(x, responseName = "PACF")
colnames(x2) = c("Lag", "Signal X", "Signal Y", "PACF")
# Remove character cast
x2$Lag = as.numeric(x2$Lag)
# Range
x_range = range(x2$Lag)
# Remove confidence intervals for theoretical ACF
if (attr(x, "dimnames")[[2]] == "Theoretical"){
show.ci = FALSE
}
if (show.ci == TRUE){
n = attr(x,"n")
mult = qnorm(1-alpha/2)
y_range = range(c(x2$PACF, 1/sqrt(n)*mult*c(-1,1)))
}else{
y_range = range(x2$PACF)
}
x_ticks = seq(x_range[1], x_range[2], by = 1)
y_ticks = seq(y_range[1], y_range[2], by = 0.05)
if (!is.null(parValue)){
par(mar = parValue)
}else{
par(mar = c(5.1, 5.1, 1, 2.1))
}
# Title
if (is.null(main)){
if (is.null(attr(x,"data_name"))){
main = paste0(as.character((x2$`Signal Y`)[1]), " PACF plot")
}else{
main = paste0(attr(x,"data_name")[1], " PACF plot")
}
}else {
main = main
}
# Main plot
plot(NA, xlim = x_range, ylim = y_range,
xlab = xlab, ylab = ylab, xaxt = 'n',
yaxt = 'n', bty = "n", ann = FALSE)
win_dim = par("usr")
par(new = TRUE)
plot(NA, xlim = x_range, ylim = c(win_dim[3], win_dim[4] + 0.09*(win_dim[4] - win_dim[3])),
xlab = xlab, ylab = ylab, xaxt = 'n', yaxt = 'n', bty = "n")
win_dim = par("usr")
# Add grid
grid(NULL, NULL, lty = 1, col = "grey95")
# Add title
x_vec = c(win_dim[1], win_dim[2], win_dim[2], win_dim[1])
y_vec = c(win_dim[4], win_dim[4],
win_dim[4] - 0.09*(win_dim[4] - win_dim[3]),
win_dim[4] - 0.09*(win_dim[4] - win_dim[3]))
polygon(x_vec, y_vec, col = "grey95", border = NA)
text(x = mean(c(win_dim[1], win_dim[2])),
y = (win_dim[4] - 0.09/2*(win_dim[4] - win_dim[3])),
main)
# Add axes and box
lines(x_vec[1:2], rep((win_dim[4] - 0.09*(win_dim[4] - win_dim[3])),2), col = 1)
box()
axis(1, padj = 0.3)
y_axis = axis(2, labels = FALSE, tick = FALSE)
y_axis = y_axis[y_axis < (win_dim[4] - 0.09*(win_dim[4] - win_dim[3]))]
axis(2, padj = -0.2, at = y_axis)
abline(h = 0, lty = 1, lwd = 2)
# Plot CI
if(show.ci){
clim0 = 1/sqrt(n)*mult
rect(xleft = -2, ybottom = -clim0, xright = 2*x_range[2],
ytop = clim0, col = col_ci, lwd = 0)
}
# Plot PACF
segments(x0 = x_ticks, y0 = rep(0, x_range[2]), x1 = x_ticks, y1 = x2$PACF, lty = 1, lwd = 1)
}
#' @title Correlation Analysis Functions
#' @description Correlation Analysis function computes and plots both empirical ACF and PACF
#' of univariate time series.
#' @author Yunxiang Zhang
#' @param x A \code{vector} or \code{"ts"} object (of length \eqn{N > 1}).
#' @param lag.max A \code{integer} indicating the maximum lag up to which to compute the ACF and PACF functions.
#' @param type A \code{character} string giving the type of acf to be computed. Allowed values are "correlation" (the default) and "covariance".
#' @param demean A \code{bool} indicating whether the data should be detrended (\code{TRUE}) or not (\code{FALSE}). Defaults to \code{TRUE}.
#' @param show.ci A \code{bool} indicating whether to compute and show the confidence region. Defaults to \code{TRUE}.
#' @param alpha A \code{double} indicating the level of significance for the confidence interval. By default \code{alpha = 0.05} which gives a 1 - \code{alpha} = 0.95 confidence interval.
#' @param plot A \code{bool} indicating whether a plot of the computed quantities should be produced. Defaults to \code{TRUE}.
#' @param ... Additional parameters.
#' @return Two \code{array} objects (ACF and PACF) of dimension \eqn{N \times S \times S}{N x S x S}.
#' @rdname corr_analysis
#' @export
#' @examples
#' # Estimate both the ACF and PACF functions
#' corr_analysis(datasets::AirPassengers)
corr_analysis = function(x, lag.max = NULL, type = "correlation", demean = TRUE, show.ci = TRUE, alpha = 0.05, plot = TRUE, ...){
if (type != "correlation" && type != "covariance"){
stop("Type not authorized. Allowed values correlation and covariance.")
}
# Compute ACF and PACF
acfe = auto_corr(x, lag.max = lag.max, type = type, demean = demean)
pacfe = PACF(x, lag.max = lag.max, type = type, demean = demean)
# Plots
if (plot){
par(mfrow = c(1,2))
plot(acfe, show.ci = TRUE, alpha = 0.05, main = "Empirical ACF", parValue = c(5.1, 4.5, 1,2))
plot(pacfe, show.ci = TRUE, alpha = 0.05, main = "Empirical PACF", parValue = c(5.1,3.85,1,2.1))
}
par(mfrow = c(1,1))
invisible(list("ACF" = acfe, "PACF" = pacfe))
}
#' @title Comparison of Classical and Robust Correlation Analysis Functions
#' @description Compare classical and robust ACF
#' of univariate time series.
#' @author Yunxiang Zhang
#' @param x A \code{vector} or \code{"ts"} object (of length \eqn{N > 1}).
#' @param lag.max A \code{integer} indicating the maximum lag up to which to compute the ACF and PACF functions.
#' @param demean A \code{bool} indicating whether the data should be detrended (\code{TRUE}) or not (\code{FALSE}). Defaults to \code{TRUE}.
#' @param show.ci A \code{bool} indicating whether to compute and show the confidence region. Defaults to \code{TRUE}.
#' @param alpha A \code{double} indicating the level of significance for the confidence interval. By default \code{alpha = 0.05} which gives a 1 - \code{alpha} = 0.95 confidence interval.
#' @param plot A \code{bool} indicating whether a plot of the computed quantities should be produced. Defaults to \code{TRUE}.
#' @param ... Additional parameters.
#' @rdname compare_acf
#' @export
#' @examples
#' # Estimate both the ACF and PACF functions
#' compare_acf(datasets::AirPassengers)
compare_acf = function(x, lag.max = NULL, demean = TRUE, show.ci = TRUE, alpha = 0.05, plot = TRUE, ...){
# Compute ACF and PACF
acfe = auto_corr(x, lag.max = lag.max, demean = demean)
pacfe = auto_corr(x, lag.max = lag.max, demean = demean, robust = TRUE)
# Plots
if (plot){
par(mfrow = c(1,2))
plot(acfe, show.ci = TRUE, alpha = 0.05, main = "Empirical Classical ACF", parValue = c(5.1, 4.5, 1,2))
plot(pacfe, show.ci = TRUE, alpha = 0.05, main = "Empirical Robust ACF", parValue = c(5.1, 4.5, 1,2))
}
par(mfrow = c(1,1))
}