0

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.

user438383
  • 5,716
  • 8
  • 28
  • 43
HarshUp
  • 17
  • 5
  • In your for loop you should assign the output of `acf` to a list. then you can extract the estimated acf using `name_of_list[[i]]$acf` (see `?acf` ). P.S. If you add a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) it's much easier for others to help you – dario Oct 03 '21 at 13:33
  • Please don't add the RStudio tag when your question isn't anything to do with Rstudio. Thanks. – user438383 Oct 03 '21 at 15:48
  • The point of the MRE and expected output is to make it as clear and easy for others to help you. – dario Oct 03 '21 at 16:13
  • @dario From what I gathered from your solution, this is what I did: `for (i in 1:3) {list_name <- acf(data[ ,i], lag.max = 1, plot = FALSE)} ARr[[i]]$acf` and obtained `, , 1 [,1] [1,] NA` which isn't right – HarshUp Oct 03 '21 at 16:16
  • @user438383 Sorry. I am new to this and thought that since I am using Rstudio so it must be a relevant tag. – HarshUp Oct 03 '21 at 16:18
  • @HarshUp no worries, its an easy mistake to make. – user438383 Oct 03 '21 at 16:58

1 Answers1

0

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
dario
  • 6,415
  • 2
  • 12
  • 26
  • Thank for this elaborate answer. This helped a lot. I still have a few doubts though. 1. Can the first step be skipped by directly inserting the name of the imported excel dataset in the third step? 2. In the returns section, what do the `, , 1` and `[1,] 1.0000000` mean? 3. Is it possible to write a for loop for the returns section too so that I can get returns for each column in a single list? – HarshUp Oct 03 '21 at 16:35
  • 1. What ? 2. The , , 1 refers to the first element in the 3. dim. and [1, ] is the correlation with itself. 3. Yes. P.S. The comment section is not for discussion of other questions If you have further questions please open a **new** post (and provide an MRE and enough information so that it is clear what you need help with) – dario Oct 03 '21 at 16:38
  • P.S. if this answer answered your question about how to retrieve the " mathematical values generated for the autocorrelation coefficient. " please consider accepting the answer (clicking on the checkmark). That way others will be able to ind the answer... – dario Oct 03 '21 at 16:43
  • The excel filename is used as the name for the imported dataset in the dataframe. So instead of doing `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))`, can I directly go `for(i in 1:3) { results[[i]] <- acf(imported_excel_filename[ ,i], lag.max = 1, type = "correlation", plot = TRUE , na.action = na.exclude) }` – HarshUp Oct 03 '21 at 16:45
  • yes, inputdata was only needed to find an answer. In your code you should use the actual data object (just to be clear :You cannot use the filename (character string) but you need the data.frame object), – dario Oct 03 '21 at 16:47
  • So I used this loop `for (i in 1:15) {results[[i]]$acf}` to generate results for each column in a single list. But the results weren't generated in the console and instead in Environment tab in the Data section. – HarshUp Oct 03 '21 at 17:01
  • Ok, last answer: If you want to do that with a for loop you need to use `for(i in 1:3) { print(results[[i]]$acf) }` Alternatively you can store only the value of interest when you run `acf` and then use `unlist` or use `unlist` on the object created by the code above, but then you have some additional values to filter through – dario Oct 03 '21 at 17:04
  • Thanks a lot. Appreciate the help. – HarshUp Oct 03 '21 at 17:06