02 - Circular statistics

DOI

Circular statistics are an analytic approach to deal with time data. Here we will use the circular package to deal with animal activity data, and the plotrix package to produce some plots.

#Loading the libraries needed to work with circular data and do plots
library("circular")
## 
## Attaching package: 'circular'
## The following objects are masked from 'package:stats':
## 
##     sd, var
library("plotrix")

#Loading an example dataset
activ<-read.table("./data/activity.txt", header= T)

Data (time) transformation to radians

A given time, say 15:26 h in a 24h clock can be also expressed as a radian in a circle. Therefore, before start analyzing activity data we should transform time data to radians using the following formula:

\[ rad=time*2*{\pi} \] Important: Prior to transform time data to radians you must convert your time figures to a number.

radian <- activ$Time_2*2*pi
data <- radian[activ$Condition == "A"]

Then, we have to convert our radian data into a circular class object in order to be used with the circular package.

as.circular(data)
## Circular Data: 
## Type = angles 
## Units = radians 
## Template = none 
## Modulo = asis 
## Zero = 0 
## Rotation = counter 
##   [1] 2.146755 2.635447 3.490659 4.118977 4.708026 2.033309 2.238385 2.779437
##   [9] 3.032510 3.281219 3.464479 3.608468 3.796091 3.948807 4.084070 4.293510
##  [17] 4.476770 2.037672 2.203478 2.548181 2.696534 3.647738 4.372050 2.103122
##  [25] 2.356194 2.583087 2.072578 3.154683 3.652101 3.852814 4.053527 4.271693
##  [33] 4.598943 4.319690 3.241949 4.114614 4.393866 4.625123 2.347468 3.490659
##  [41] 3.708825 3.926991 4.507313 1.994039 2.146755 2.286381 3.508112 3.660828
##  [49] 3.817908 3.953171 4.293510 4.568399 4.747296 2.539454 2.792527 3.058690
##  [57] 3.337942 3.577925 3.752458 4.101524 4.354596 4.564036 4.712389 1.954769
##  [65] 2.194752 2.351831 2.500184 2.958333 3.150319 3.294309 3.455752 3.700098
##  [73] 3.870268 4.018621 4.236787 4.402593 4.542219 4.782202 2.072578 2.351831
##  [81] 2.692170 2.849250 3.023783 3.368485 3.704461 4.040437 3.351032 4.193154
##  [89] 4.411320 2.015855 2.412918 2.740167 3.032510 3.333579 3.678281 3.974987
##  [97] 4.223697 4.485496 3.582288 3.809181 4.433136 3.569198 3.974987 4.219333
## [105] 3.604105 3.800454 4.319690 3.756821 4.463680 2.661627 3.512475 4.472406
## [113] 2.247111 3.316126 4.581489 2.980150 3.455752 3.647738 3.839724 1.854412
## [121] 3.700098 2.962696 3.787364 4.101524 3.726278 1.985312 3.281219 3.813544
## [129] 3.495022 4.197517 3.477569 4.228060 3.883358 3.726278 4.276057 3.625922
## [137] 3.900811 3.756821 4.358960 4.228060 3.961897 3.796091 3.761185 2.975786
## [145] 3.922627 3.372849 2.910337 3.564835 4.127704 2.897247 3.316126 3.900811
## [153] 3.049963 2.247111 2.543817 3.316126 4.454953 2.037672 2.421644 2.731440
## [161] 3.333579 3.800454 2.404191 3.935717 2.823070 3.726278 4.171337 2.124938
## [169] 2.330015 3.735005 3.883358 4.542219 3.543018 3.761185 4.228060 4.642576
## [177] 2.103122 2.430371 2.587451 2.857977 3.102323 3.324852 3.538655 3.905174
## [185] 4.197517 4.367686 4.537856 2.020219 2.299471 2.613631 2.923426 3.381575
## [193] 3.543018 3.826634 3.979351 4.175700 4.358960 3.041236 3.259402 3.438299
## [201] 3.796091 3.294309 3.394665 4.533493 2.871067 3.556108 4.228060 4.367686
## [209] 3.268129 3.735005 3.970624 4.254240 4.424410 2.129302 2.081305 2.748894
## [217] 3.128503 3.800454 4.057891 4.411320 4.594579 2.020219 2.212205 2.596177
## [225] 2.744530 2.923426 3.076143 3.237586 3.486295 3.713188 3.896448 4.088434
## [233] 4.276057 4.428773 4.607669 2.055125 2.238385 2.478368 2.648537 2.984513
## [241] 3.189589 3.425209 3.835361 4.092797 4.345870 4.524766 2.142392 2.395464
## [249] 2.552544 2.696534 2.849250 3.041236 3.372849 3.730641 4.145157 4.398230
## [257] 2.207842 2.556907 2.731440 2.940880 3.569198 3.721915 4.009894 4.180064
## [265] 4.324053 4.616396 2.037672 2.290745 3.246312 3.883358 4.057891 4.280420
## [273] 4.472406 4.738569 1.976585 2.133665 2.613631 3.019420 3.172136 3.355395
## [281] 3.573562 3.765548 3.979351 4.136430 4.289147 4.542219 4.677482 1.967859
## [289] 2.364921 2.574361 2.788163 2.932153 3.067416 3.298672 3.512475 3.691371
## [297] 3.826634 4.005531 4.236787 4.411320 4.646939 2.011492 2.164208 2.696534
## [305] 2.936516 3.207043 3.420845 3.708825 3.913901 4.066617 4.310963 4.485496
## [313] 4.651302 1.972222 2.369284 2.840523 3.198316 3.451389 3.621558 3.809181
## [321] 4.005531 4.241150 4.485496 4.651302 1.954769 2.172935 2.316925 2.478368
## [329] 2.670354 3.019420 3.372849 3.538655 3.682645 3.874631 4.079707 4.232423
## [337] 4.372050 4.577126 4.769112 1.985312 2.138028 2.474004 2.683444 2.844887
## [345] 3.041236 3.250676 3.433935 3.647738 3.870268 4.027347 3.093596 4.062254
## [353] 2.517637 2.652900 2.923426 2.277655 2.600541 2.007129 2.216568 2.478368
## [361] 3.049963 3.250676 3.407755 3.556108 3.730641 3.931354 4.079707 4.420046
## [369] 4.664392 2.923426 3.796091 1.954769 2.164208 2.299471 2.591814 2.801253
## [377] 3.159046 3.316126 3.512475 3.769911 4.468043 2.098758 2.373648 2.522001
## [385] 2.670354 2.914700 3.084869 3.276856 3.438299 3.591015 3.735005 3.974987
## [393] 4.118977 4.376413 4.568399 4.830199 1.889319 2.124938 2.504547 2.687807
## [401] 2.827433 3.084869 3.224496 3.464479 3.735005 3.878994 4.031711 4.245513
## [409] 4.402593 4.577126 1.832596 2.635447 2.958333 3.346669 3.534292 3.721915
## [417] 3.878994 4.123340 4.441863 4.729842 1.924226 2.587451 2.919063 3.822271
## [425] 4.677482 2.443461 2.796890 3.028146 3.399029 3.647738 3.905174 4.053527
## [433] 4.201880 2.347468 2.487094 2.727077 4.118977 3.730641 4.437500 3.682645
## [441] 4.385140 4.127704 1.959132 3.390302 3.861541 4.188790 3.551745 1.880592
## [449] 2.923426 3.220132 4.306600 4.031711 4.660029 2.151118 2.351831 2.652900
## [457] 2.827433 4.009894 2.124938 4.516039 3.508112 2.159845 3.381575 2.347468
## [465] 3.076143 3.543018 2.399828 3.660828 4.398230 2.295108 3.521202 3.735005
## [473] 2.290745 3.058690 3.407755 4.232423 2.844887 3.730641 4.031711 4.332780
## [481] 4.629486 3.420845 3.573562 4.088434 4.079707 4.402593 3.049963 3.385939
## [489] 4.079707 4.376413 1.946042 2.862340 3.036873 3.390302 4.001167 4.175700
## [497] 4.406956 4.603306 3.172136 3.844088 2.836160 3.128503 2.517637 3.015056
## [505] 3.817908 3.992441 2.020219 1.880592 2.024582 3.390302 3.582288 3.778638
## [513] 3.333579 3.516838 3.887721 4.681846 3.093596 2.679080 2.823070 3.468842
## [521] 4.267330 3.093596 3.276856 3.503748 3.678281 3.957534 4.184427 4.681846
## [529] 2.635447 2.831797 3.054326 3.202679 3.425209 3.918264 4.507313 1.867502
## [537] 2.199115 1.985312 3.106686
n_a <- circular(data%%24, 
                     units="radians", template="clock24")

Rose diagrams

A simple representation of activity data is the rose diagram, which is obtained using the function rose.diag to represent a circular dataset into a 24h clock.

rose.diag(n_a, bin = 24, col = "lightblue", main = "Rose diagram", 
          prop = 2)

Frequency plots

A more elaborated representation of activity periods than the rose diagram is the frequency plot. On a frequency plot, the lenght of each bar is proportional to .

To make a decent frequency plot we need to arrange our data in a table with frequency of records per hour. We’ll load a new file (ready to go) as example.

Then, we will use the clock24.plot function of the plotrix package.

#Loading new data file
activ_table<-read.table("./data/records_hour.txt", header= T)

#Making the plot
clock<-c(0:23) 
clock24.plot(activ_table$A,clock, minutes=T, label.prop=c(1.15,1.15),show.grid.labels = 3,  show.grid=T, grid.bg="white", grid.left=T,grid.col="black",rad.col="black", rp.type="r",lwd=5,line.col="blue",lty=1, main="",add=F)

Another way to represent the frequency plot is with the magnitudes represented by the positions of the vertices of the polygon. In this plot, the positions of the vertices indicate the frequency of the records.

#Making the plot
clock<-c(0:23) 
mi.azul <- rgb(0, 0, 1, 0.6) # "#0000FF99"
clock24.plot(activ_table$A,clock,minutes=T,label.prop=c(1.15,1.15),show.grid.labels = 3, rp.type="s",point.symbols=16,point.col="#0000FF99",add=F)
clock24.plot(activ_table$A,clock, rp.type="p",poly.col="#0000FF99",lwd=4,line.col="#0000FF99",lty=1, main="",add=T) 

Formal tests

Now that we have made such beautiful rose diagrams and frequency plots, we maybe will be interested into test some hypotheses about activity patterns. The most simple question here will be if animal activity is homogeneous or not along the day. Of course our null hypothesis will be that animal activity is uniform along the day (like a faculty researcher, for example). The circular package has two common tests to do that: Rao’s test for homogeneity of angular data (rao.test function), and Rayleight test of Unifortmity (rayleigh.test function). Both tests do basically the same, the difference is that Rayleigh test needs only one circular.class element whereas Rao’s test need two samples to be compared.

Let’s see how they work:

#Rao's test
x <- rvonmises(100, circular(0), kappa=10)
y <- rvonmises(100, circular(0), kappa=10)

rao.test(x, y)
## 
## Rao's Tests for Homogeneity 
## 
##        Test for Equality of Polar Vectors: 
##  
## Test Statistic = 3.1008 
## Degrees of Freedom = 1 
## P-value of test = 0.0783 
##  
##        Test for Equality of Dispersions: 
##  
## Test Statistic = 0.3045 
## Degrees of Freedom = 1 
## P-value of test = 0.5811 
## 
#Rayleigh's test
rayleigh.test(n_a)
## 
##        Rayleigh Test of Uniformity 
##        General Unimodal Alternative 
## 
## Test Statistic:  0.7168 
## P-value:  0

Session

sessionInfo()
## R version 4.4.1 (2024-06-14)
## Platform: x86_64-apple-darwin20
## Running under: macOS 15.1
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRblas.0.dylib 
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## time zone: America/Santiago
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] plotrix_3.8-4  circular_0.5-1
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.37     R6_2.5.1          fastmap_1.2.0     xfun_0.47        
##  [5] cachem_1.1.0      knitr_1.48        htmltools_0.5.8.1 rmarkdown_2.28   
##  [9] lifecycle_1.0.4   mvtnorm_1.3-1     cli_3.6.3         sass_0.4.9       
## [13] jquerylib_0.1.4   compiler_4.4.1    highr_0.11        boot_1.3-30      
## [17] rstudioapi_0.16.0 tools_4.4.1       evaluate_0.24.0   bslib_0.8.0      
## [21] yaml_2.3.10       rlang_1.1.4       jsonlite_1.8.8
footer.utf8.md


Licensing Creative Commons License
CC

Licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
2019 - Francisco E. Fontúrbel