# package names
packages<-c("readr","ggplot2", "here", "mcp", "lubridate", "knitr", "ezknitr", "loo")
# install any packages not previously installed
installed_packages<-packages %in% rownames(installed.packages())
if(any(installed_packages == FALSE)){
install.packages(packages[!installed_packages])
}
# load packages
invisible(lapply(packages, library, character.only = TRUE))
Load bear data.
df<-read_csv(here("DRUM_materials/data_files_for_DRUM/black_bear.csv"))
head(df)
## # A tibble: 6 x 2
## HeartRate DateTime
## <dbl> <chr>
## 1 43 10/11/2014 14:55
## 2 47 10/11/2014 14:57
## 3 42 10/11/2014 14:59
## 4 42 10/11/2014 15:01
## 5 44 10/11/2014 15:03
## 6 43 10/11/2014 15:05
Convert the datetime column to an index from the starting point.
df$dt<-mdy_hm(df$DateTime)
df$time<-as.numeric(difftime(df$dt,df$dt[1], units = 'mins'))
Fit piecewise model.
model<-list(
HeartRate~1,
~1+time)
fit<-mcp(model, df[,c("HeartRate", "time")])
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
## Graph information:
## Observed stochastic nodes: 60
## Unobserved stochastic nodes: 5
## Total graph size: 805
##
## Initializing model
summary(fit)
## Family: gaussian(link = 'identity')
## Iterations: 9000 from 3 chains.
## Segments:
## 1: HeartRate ~ 1
## 2: HeartRate ~ 1 ~ 1 + time
##
## Population-level parameters:
## name mean lower upper Rhat n.eff
## cp_1 64.75 62.8 66.00 1 1459
## int_1 43.88 39.8 47.66 1 4662
## int_2 159.60 149.7 168.36 1 574
## sigma_1 10.94 9.0 13.09 1 3277
## time_2 -0.76 -1.1 -0.45 1 575
Plot fitted model.
plot(fit, q_fit=T)+
xlab("Time (in minutes)")+ylab("Heart rate (beats per minute)\n")+
theme_bw(base_size = 14)+
geom_rect(aes(xmin=60, xmax=65, ymin=0, ymax=Inf), fill="green",alpha=0.005, color="grey", size=0.1)+
# geom_rect(aes(xmin=75, xmax=80, ymin=0, ymax=Inf), fill="blue",alpha=0.005, color="grey", size=0.1)+
theme(plot.title=element_text(hjust=0.5))
sessionInfo()
## R version 4.0.2 (2020-06-22)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19043)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=English_United States.1252
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] loo_2.4.1 ezknitr_0.6 knitr_1.30 lubridate_1.7.9
## [5] mcp_0.3.0.9000 here_0.1 ggplot2_3.3.2 readr_1.3.1
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.8 pillar_1.4.6 bslib_0.3.1 compiler_4.0.2
## [5] jquerylib_0.1.4 tools_4.0.2 digest_0.6.28 checkmate_2.0.0
## [9] jsonlite_1.7.1 evaluate_0.14 lifecycle_0.2.0 tibble_3.0.3
## [13] gtable_0.3.0 pkgconfig_2.0.3 rlang_0.4.10 rstudioapi_0.11
## [17] patchwork_1.1.1 parallel_4.0.2 yaml_2.2.1 xfun_0.26
## [21] fastmap_1.1.0 withr_2.3.0 stringr_1.4.0 dplyr_1.0.2
## [25] generics_0.0.2 vctrs_0.3.4 sass_0.4.0 hms_0.5.3
## [29] rprojroot_1.3-2 grid_4.0.2 tidyselect_1.1.0 glue_1.4.2
## [33] R6_2.4.1 rmarkdown_2.11 purrr_0.3.4 magrittr_2.0.1
## [37] matrixStats_0.57.0 backports_1.1.10 scales_1.1.1 ellipsis_0.3.1
## [41] htmltools_0.5.2 colorspace_1.4-1 stringi_1.5.3 munsell_0.5.0
## [45] crayon_1.3.4