R/CalcEnSlide.R

Defines functions CalcEnSlide

Documented in CalcEnSlide

#' Calculates Sliding Entropy for groups of interest in dataframes
#' @author Samuel Hamilton
#' @param df input cgms.df dataframe
#' @param entmeasure chosen entropy measure. Either FastSampEn or FastApEn. String
#' @param ofint variable of interest to calculate entropy on from the datagrame. String
#' @param varlist Which groups to separate dataframe by
#' @param r_thrsh what is the tolerance value r. This is multiplied by the sd of the whole ofint column.
#' @param size the window size
#' @param step step length
#' @import TSEntropies dplyr
#' @keywords null distributions
#' @return dataframe with the entropy value for each group in varslist
#' @export
#' @examples CalcEnSlide(df = cgms.df,varlist = c("Group"))
CalcEnSlide <- function(df,entmeasure = "FastSampEn",ofint = "historic_glucose",varlist,r_thrsh = .2,size = 200, step = 1) {

  #calculate  sd for all ts
  allsd <-  allsd <- sd(dplyr::pull(df,
                                    ofint),
                        na.rm = T)

  #Uses above to calc mean sliding window entropy function for each var in varslist.
  sumdf <- plyr::ddply(df,
             varlist,
             summarise,
             entropy = mean(calc_En_slidingwindow(entmeasure = entmeasure,
                                                  ofint = get(ofint),
                                                  size = size,
                                                  step = step,
                                                  r_thresh = r_thrsh*allsd)
             )
  )
  return(sumdf)
}
hamsamilton/cgms.analysis documentation built on March 29, 2020, 12:57 a.m.