I had imported my data from excel, containing 64 rows and 15 columns, into R in order to calculate the autocorrelation using the acf(). I've been to other similar threads and the only workable code I could find was:
structure(list(V1 = c(24.1, 6.3, 39.1, 11.97, 23.47), V2 = c(5.63, 9.91, 19.98, 16.14, 15.76), V3 = c(21.08, 5.82, 23.5, 27.71, 3.54)), class = "data.frame", row.names = c(NA, -5L))
for(i in 1:3) {acf(data[ ,i], lag.max = 1, type = "correlation", plot = TRUE , na.action = na.exclude)}
Using this loop, I was able to obtain the autocorrelation plots but there were no mathematical values generated for the autocorrelation coefficient. I tried assigning a variable name to this loop but after the run, it only returned as NULL.
I also used the following code in place of the for loop:
lapply(split(A,col(A)), function(ts) acf(ts, lag.max=1))
where, A is a matrix with 64 rows and 15 columns. But it doesn't work either because when I convert my imported data into a matrix and run this code, it shows the error "A is not numeric" and also, I think ts stands for time series but when I insert the data in place of ts, there is an error.
CodePudding user response:
1.It's better to avoid using R keywords like "data".It's not always a problem but can lead to confusion.
inputdata <- structure(list(V1 = c(24.1, 6.3, 39.1, 11.97, 23.47),
V2 = c(5.63, 9.91, 19.98, 16.14, 15.76),
V3 = c(21.08, 5.82, 23.5, 27.71, 3.54)),
class = "data.frame", row.names = c(NA, -5L))
2.We prepare an empty list to collect the statistics from acf
:
results <- list()
3.You can use a loop or something from the apply family. But I'd say a loop makes it easier to see what is going on. In the loop, on each iteration we put the result in the list.
for(i in 1:3) {
results[[i]] <- acf(inputdata[ ,i], lag.max = 1, type = "correlation", plot = TRUE , na.action = na.exclude)
}
The help for acf
(accessed using ?acf
tells us where it puts its statistics etc.
Now we can access the results from the analysis the following way:
results[[1]]$acf
returns:
, , 1
[,1]
[1,] 1.0000000
[2,] -0.7761198
and
results[[2]]$acf
returns:
, , 1
[,1]
[1,] 1.000000
[2,] 0.218416
results[[3]]$acf
returns:
, , 1
[,1]
[1,] 1.0000000
[2,] -0.3962866