#' Makes and rbinds a null distribution created by sampling differences between points, then randomly adding them
#' to the mean glucose value of a cgms.df dataframe. This null distribution has "guard rails", meaning it can't got above or below the
#' max and min value of all glucose in the cgms.df object
#' @param df input cgms.df object
#' @keywords null distributions
#' @export
#' @import TSEntropies dplyr
#' @examples addnulldistdiffguard(cgms.df)
addnulldistdiffguard <- 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)
maxbp <- max(df$historic_glucose)
minbp <- min(df$historic_glucose)
# init difflist
difflist.resampled <- c(mean(df$historic_glucose))
while(length(difflist.resampled) < nrow(nulldistframe)){
sample.1 <- sample(difflist,size = 1)
if(last(difflist.resampled) + sample.1 < maxbp & last(difflist.resampled) + sample.1 > minbp){
difflist.resampled <- append(difflist.resampled,sample.1 + last(difflist.resampled))
}
}
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 <- difflist.resampled
rdf <- rbind(df,nulldistframe)
return(rdf)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.