Looping a Rayleigh test to find highest value

Isak Aarrestad

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 {

Any help is appreciated. Thank you!

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

Rui Barradas

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


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

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

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

#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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment


Find the second highest value

Find the highest value in a dataframe

Find highest value in multidimensional array and keep highest

Javascript: Find variable with highest and second highest value

find element with highest value with XPath?

array find the second highest value

Find the row with the highest value in SQL

how to find highest value in a record in Google bigquery

PHP find object has highest value in array

How to find highest char value in ruby?

Find the highest and lowest value for a time frame

How to find the highest value for a column nested in JSON?

C# find highest array value and index

Find highest value in Get-ADOrganizationalUnit Name

PHP Find the Index of the highest key value

find the row with highest number of NA value in R

Find the highest value in an array of hashes in Ruby

jQuery/JavaScript find highest value in table column

mongon: Find the ID with the highest value in another column

Find ALL keys of highest value in dictionary

How to find the object containing the highest value in an array

Sequeilize find highest value of table in where clause

Find the highest value and replace the lower with zero

how to find the key with the highest value in a dict of dicts?

SQL to find highest value for repeated records

4 Functions find lowes/highest position and value

Find a highest value in a set of columns and retrieve value in the respective row

How do I find the highest value and swap it with the end value in an array?

SQLAlchemy - Find highest value less than specified value

TOP Ranking