0

I am having a bit of a holdup in my workflow as I am trying to make a double loop to fill a matrix work. At the start of my script I define:

Tijd_SAT_i <- c("08", "09", "10", "11", "12", "13", "14") 
D <- c(0, 0.01, 0.02, 0.03, 0.04, 0.05)
POD <- matrix(1:(length(Tijd_SAT_i)*length(D)), c(length(Tijd_SAT_i), length(D)))
FAR <- matrix(1:(length(Tijd_SAT_i)*length(D)), c(length(Tijd_SAT_i), length(D)))

After that a load of stuff to create "BASE" (86 by 42 by 7 array) and "SAT" (86 by 42 by 7 array). Both BASE and SAT contain values between 0 and 22 as doubles and have NA values (the NA values make an outline of the Netherlands and everything beyond that). Next, I need to fill the two matrices POD (probability of detection) and FAR (False Alarm Ratio) For every hour (Tijd_SAT_i) I want to calculate 6 different threshold values and their influence on POD and FAR. Later I will put these values into a dataframe and then use RGL to plot them. The code I have written to do fill POD and FAR is:

for (k in 1:(length(Tijd_SAT_i))) {
  for (l in 1:(length(D))) {
    pre_C_BASE <- BASE[,,k]
    pre_C_SAT <- SAT[,,k]
    pre_C_BASE[pre_C_BASE >= D[l]] <- 1
    pre_C_BASE[pre_C_BASE < D[l]] <- 0
    pre_C_SAT[pre_C_SAT >= D[l]] <- 1
    pre_C_SAT[pre_C_SAT < D[l]] <- 0
    C_SAT <- as.vector(SAT)
    C_BASE <- as.vector(BASE)
    C_table <- (with(warpbreaks, table(C_SAT, C_BASE)))      
    print(C_table)
    print(dim(C_table))
    #p <- C_table[1, 1]/(C_table[1, 1]+C_table[2, 1])
    #f <- C_table[1, 2]/(C_table[1, 1]+C_table[1, 2])
    #POD[k, l] <- p
    #FAR[k, l] <- f
  }
}

But the C_table does not become a 2 by 2 matrix but rather a 1237 by 3351 one for some vague reason. The stuff behind the #s can thus not be performed. Where do I go wrong in this?

Edit: I have found the cause for the 2 by 2 matrix not showing: a wrong reference SAT should be pre_C_SAT. Now my contingency matrices are made (all 42 of them). The problem now is that if I run the bit with the #s, I get a "subscript out of bounds" error. Why is that?

Edit: I think the trouble lies in that 'table' not always outputs a 2 by 2 matrix.

Edit: Pffffrwt. I fixed it. There was an incredible annoying niggle in the table command in that it wouldn't return a 2 by 2 matrix when no zeros were present in one of the input vectors. I have fixed it by appending 1, 0, 0, 1 and 1, 0, 1, 0 to the subsequent vectors, forcing all 4 values to be created and then substracting 1 from the subsequent equation.

2 Answers2

0

In each iteration of the loop, you define

C_SAT <- as.vector(SAT)
C_BASE <- as.vector(BASE)

So C_SAT and C_BASE will always be vectors with length equal to the product of the dimensions of SAT and BASE respectively.

Then you define

C_table <- (with(warpbreaks, table(C_SAT, C_BASE)))

What does the warpbreaks data frame have to do with your analysis? Have you copied and pasted this from somewhere? It has no effect on the result.

Anyway, table(C_SAT, C_BASE) will have as many rows as the number of unique values in C_SAT and as many columns as the number of unique values in C_BASE.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • Yes, I have copied the warpbreaks, table command from this example: [link](http://stackoverflow.com/questions/7442207/contingency-table-with-r). It should create a 2 by 2 contingence table, but it doesn't. Probably because the pre_C_BASE[pre_C_BASE >= D[l]] <- 1 etc. does not create new matrices with only 1s and 0s. Maybe I should delete the NAs? – user2807278 Oct 02 '13 at 10:42
0

I have fixed the issue by appending 1, 0, 0, 1 and 1, 0, 1, 0 to the subsequent vectors, forcing all 4 values to be created and then substracting 1 from the subsequent equation.