1

I am a beginner with R, so hopefully this will be an easy fix.

I am trying to use a for loop on a dataset for neuron firing direction in order to:

  • Incrementally add the next value from the dataset to a vector
  • Run a Rayleigh test on that vector and save it to a variable
  • Test if the Rayleigh test I just ran has a larger statistic than the the Rayleigh test in the last loop just before it, as well as having a p-value of less than .05
  • If the value is larger, save the statistic value, so that the next loop can compare to it
  • If the value is larger, save the vector

So far I have this for the code, and after going through it for a long time I'm at a loss for why it's not working. Every time I run it, the for loop goes all the way to the end and just reports the rayleigh value and vector for the whole dataset, which I know for sure isn't correct. (I'm using the circular package for the rayleigh test function)

# This first line is just to create an initial rayleigh statistic to compare to in the loop that is low

best_rayleigh <- rayleigh.test(1:10)
data_vector <- c()

for (i in firing_directions) {
  data_vector <- append(data_vector, i) 
  ray_lee_test <- rayleigh.test(data_vector) 
  if ((ray_lee_test$statistic>best_rayleigh$statistic)&(ray_lee_test$p.value<=.05)) {
    best_rayleigh <- ray_lee_test 
    best_rayleigh_vector <- data_vector
  } else {
    NULL
  }
}

Any help is appreciated. Thank you!

Update: I tried using && instead of single & in the if statement, however it returned the same result

2 Answers2

0

I think the NULL is throwing up some issues. Not sure what will have if your throw a NULL. You only update the vector if it passes your criteria:

library(circular)
firing_directions= rvonmises(n=25, mu=circular(pi), kappa=2)

best_rayleigh <- rayleigh.test(1:10)
data_vector <- c()    

for (i in firing_directions){
  data_vector <- c(data_vector, i) 
  ray_lee_test <- rayleigh.test(data_vector) 
     if ((ray_lee_test$statistic>best_rayleigh$statistic)&(ray_lee_test$p.value<=.05)) {
        best_rayleigh <- ray_lee_test 
        best_rayleigh_vector <- data_vector
     } 
  }
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
0

The following code doesn't give warnings and selects the vector with highest test statistic and "significant" p-value.

library(circular)

set.seed(2020)
firing_directions <- rvonmises(n = 25, mu = circular(pi), kappa = 2)
plot(firing_directions)

best_rayleigh <- rayleigh.test(circular(1:10))
for(i in seq_along(firing_directions)){
  dv <- firing_directions[seq_len(i)]
  rltest <- rayleigh.test(dv)
  if((rltest$statistic > best_rayleigh$statistic) && (rltest$p.value <= 0.05)){
    best_rayleigh <- rltest 
    best_rayleigh_vector <- dv
  }
}

best_rayleigh
#
#       Rayleigh Test of Uniformity 
#       General Unimodal Alternative 
#
#Test Statistic:  0.8048 
#P-value:  0.0298 

best_rayleigh_vector
#Circular Data: 
#Type = angles 
#Units = radians 
#Template = none 
#Modulo = asis 
#Zero = 0 
#Rotation = counter 
#[1] 4.172219 2.510826 2.997495 4.095335 3.655613
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66