1

I have the following code to calculate ADF statistics across ts data in a matrix raw.

raw <- matrix(rnorm(25000), nrow=1000,ncol=25)
for (i in 1:(ncol(raw)-1)) {
    for (j in (i+1):ncol(raw)) {

        df <- data.frame(N = integer(0), DFStat = numeric(0), PVAL =     numeric(0))
        name <- paste(i,j, sep="_")
        dir <- paste("C:/Data/rOUT/", name,".txt",sep="")
        for (t in 1:20) {
             N <- 50*t
             xtail <- tail(raw[,i],N)
             ytail <- tail(raw[,j],N)
             reg <- lm(ytail ~ xtail)
             test <- adf.test(reg$residuals, k = 1)
             df[t,] <- c(N, test$statistic, test$p.value)
             write.table(df, dir, sep="\t", row.names=FALSE)
             rm(df)

        }
    }
}

The block in the first two loops runs perfectly when not looping through the columns in raw, but when I try to traverse raw, I get the closure error below:

Error in df[t, ] <- c(N, test$statistic, test$p.value) : 
  object of type 'closure' is not subsettable

Does anyone know why I am encountering this difference and steps I can take to correct it?

Ray Foo
  • 39
  • 8
  • Can you share some example data? – Phil Jan 18 '17 at 12:08
  • What do you mean? The data itself is just prices – Ray Foo Jan 18 '17 at 12:50
  • 1
    Your code doesn't do anything without data, so it's practically impossible to debug without it. You should provide minimal code to reproduce your problem (see http://stackoverflow.com/help/mcve and http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Phil Jan 18 '17 at 13:20
  • Have a look at class(test$statistic) and class(test$p.value). Is it a number which fits in your df? – Christoph Jan 18 '17 at 13:48
  • @phil added the raw matrix – Ray Foo Jan 18 '17 at 16:18
  • @christoph yes the tests return a Dickey-Fuller score and a p.value, both of which are numerics as defined when i initialized the data frame. I think R is treating df as a function when I run it as the loop, I have no idea why though. – Ray Foo Jan 18 '17 at 16:26

1 Answers1

1

I guess your rm(df) is at the wrong position. The following code runs:

raw <- matrix(rnorm(250), nrow=10,ncol=25)
for (i in 1:(ncol(raw)-1)) {
  for (j in (i+1):ncol(raw)) {

    df <- data.frame(N = integer(0), DFStat = numeric(0), PVAL =     numeric(0))
    name <- paste(i,j, sep="_")
    dir <- paste("C:/Data/rOUT/", name,".txt",sep="")
    for (t in 1:20) {
      N <- 50*t
      xtail <- tail(raw[,i],N)
      ytail <- tail(raw[,j],N)
      reg <- lm(ytail ~ xtail)
      # test <- adf.test(reg$residuals, k = 1)
      # df[t,] <- c(N, test$statistic, test$p.value)
      df[t,] <- c(N, 1, 1)
      # write.table(df, dir, sep="\t", row.names=FALSE)


    }
    rm(df)
  }
}
Christoph
  • 6,841
  • 4
  • 37
  • 89