R/addnulldistdiff.R

Defines functions addnulldistdiff

Documented in addnulldistdiff

#' Makes and rbinds a null distribution created by sampling differences between point, then randomly adding them
#' to the mean glucose value of a cgms.df dataframe
#'
#' @param df input cgms.df object
#' @keywords null distributions
#' @export
#' @examples addnulldistdiff(cgms.df)

addnulldistdiff <- function(df) {

  #initlist
  difflist <- c()
  for(i in unique(df$run_id)){
    filtereddf <- df %>% dplyr::filter(df$run_id == i)
    diffs <- diff(filtereddf$historic_glucose)
    difflist <- append(difflist,diffs)
  }

  #init nulldistframe
  nulldistframe <- data.frame(matrix(ncol = 21,nrow = length(unique(df$time_past_start))))
  names(nulldistframe) <- names(df)
  nulldistframe <- nulldistframe %>%
    dplyr::mutate_at(.vars = c("projno","subjno","visit","id","Subject",
                               "Age","Gender","Group","subj_id","run_id"),
                     function(x){
                       return("nulldist")
                     }
    )
  nulldistframe$time_past_start <- unique(df$time_past_start)
  difflist.resampled <- sample(difflist,size = length(unique(df$time_past_start)))

  nulldisttrace = c(mean(df$historic_glucose))
  for (i in 2:length(difflist.resampled)) {
    nulldisttrace[i] <- nulldisttrace[i-1] + difflist.resampled[i]#add current diff to previous value in nulldifftrance
  }
  nulldistframe$historic_glucose <- nulldisttrace
  rdf <- rbind(df,nulldistframe)
  return(rdf)
}
hamsamilton/cgms.analysis documentation built on March 29, 2020, 12:57 a.m.