#' 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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.