Kernel density plots are effective way to visualize the distribution of variable (in this case, time). Thus, kernel plots can be considered as plots of smoothed histograms.
Here we’ll work with two R
packages: activity
and overlap
. We also will load two sample time datasets along with the packages to perform the activty test.
#Loading required packages
library("overlap")
library ("activity")
#Loading two activity datasets (time)
act_x<-read.table("./data/act_x.txt", header= T)
act_y<-read.table("./data/act_y.txt", header= T)
But before plotting the kernels, we must transform time data to radians using the following formula:
\[ rad=time*2*{\pi} \]
rad_x<-act_x$Time_2*2*pi
rad_y<-act_y$Time_2*2*pi
And one more step before the fun start… we must compute fitted data. In this example, we will use only one condition (A) of the two conditions available in the dataset (A and B).
fitact_a <-fitact(rad_x[act_x$Condition=="A"])
fitact_b <-fitact(rad_y[act_y$Condition=="A"])
Now we will plot kernel density functions for our two datasets separately.
#Density plot for dataset A
plot(fitact_a, yunit="dens", las=1, dline=list(col="lightblue"), tline=list(col="blue"), main="Data X", ylim=c(0,0.17))
#Density plot for dataset B
plot(fitact_b, yunit="dens", las=1, dline=list(col="pink"), tline=list(col="red"), main = "Data Y")
And we also are able to depict both kernels in one plot:
plot(fitact_a, yunit="dens", las=1, dline=list(col="lightblue"), tline=list(col="blue"), ylim=c(0,0.17))
plot(fitact_b, yunit="dens", add=TRUE, dline=list(col="pink"), tline=list(col="red"))
legend("topright", inset = c(0,0), title = "Data", c("X", "Y"), lty= c(1,1), col = c("lightblue", "pink"), bty = "n")
It is very nice, isn’t it?
The overlap coefficient of is the area under a density curve.This index is merely descriptive. On the other hand, the function CompareCkern
of the package activity
, is a probability test that assess if two datasets come from the same activity distribution. In addition, this test calculate overlap index dhat5 for two fitted distributions (with function fitact
).
Below we compare datasets X and Y, and test if they come or not from the same activity distribution:
compareCkern(fitact_a, fitact_b, reps = 100)
## obs null seNull pNull
## 0.94528569 0.96108573 0.01154511 0.10000000
Before plotting overlaps we must extracti data for condition = A…
act_a <- rad_x[act_x$Condition=="A"]
act_b <- rad_y[act_y$Condition=="A"]
And now we will graph the Density Plots!
#Density plot for A
densityPlot(act_a, xscale = 24, xcenter = c("noon", "midnight"), add = F, rug = T, extend = "lightgray", main = "Data X", xlab = "Time", ylab = "Density", bty = "L")
#Density plot for B
densityPlot(act_b, xscale = 24, xcenter = c("noon", "midnight"), add = F, rug = T, extend = "lightgray", main = "Data Y", xlab = "Time", ylab = "Density", bty = "L")
And of course, we can plot the two datasets in same plot with overlap
overlapPlot(act_a, act_b, main = " ", linecol = c("black", "darkgreen"), lty = c(1,3), bty = "L", xlab = "Time", ylab = "Density")
legend("topright", inset = c(0,0), title = "Data", c("X", "Y"), lty= c(1,3), col = c("gray25", "seagreen"), bty = "n")
sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS Mojave 10.14.5
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] activity_1.2 overlap_0.3.2
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.1 mvtnorm_1.0-11 digest_0.6.20 magrittr_1.5
## [5] evaluate_0.14 pbapply_1.4-1 stringi_1.4.3 boot_1.3-23
## [9] rmarkdown_1.14 tools_3.6.0 stringr_1.4.0 xfun_0.8
## [13] yaml_2.2.0 parallel_3.6.0 compiler_3.6.0 circular_0.4-93
## [17] htmltools_0.3.6 knitr_1.23
Licensing Creative Commons License
Licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
2019 - Francisco E. Fontúrbel