R/class.plot.R

Defines functions class.plot

Documented in class.plot

#' A LCGA Model PLotting Function
#'
#' This function helps you plot predicted latent classes generated by an lcmm model.
#' @param model enter lcmm model.
#' @param data enter data set (complete with multiple ID and predicted class rows).
#' @param smooth would you like ggplot to smooth (using loess) time-varying outcome for each class?
#' @keywords lcmm
#' @export
#' @examples
#' class.plot()

class.plot <- function(model, data, smooth='none') {
  # aggregate data
  mean_df <- data %>%
    group_by(months, p_ids) %>%
    summarise(outcomes = mean(outcomes, na.rm=T))
  # plot data
  p <- ggplot(mean_df, aes(x = months, y = outcomes, colour = as.factor(p_ids))) +
    xlab('Time') +
    ylab('Outcome') +
    ggtitle(paste('Outcome Changes Over Time by Latent Class,', 'BIC =', round(model$BIC, 2))) +
    theme_minimal() 
  # smooth option 1 (no smoothing)
  if(smooth == 'none') {
    p <- p + geom_path(aes(group = as.factor(p_ids)))
  }
  # smooth option 2 (just smooth, no path)
  if(smooth == 'some') {
    for(i in 1:model$ng) {
      p <- p + stat_smooth(method = 'loess', se = F)
    }
    p <- p + stat_smooth(method = 'loess', se = F)
    # smooth option 3 (smoothing + path)
  } else if(smooth == 'all') {
    for(i in 1:model$ng) {
      p <- p + geom_path(aes(group=as.factor(p_ids))) + stat_smooth(method = 'loess', se = F)
    }
  }
  # end
  return(p)
}
noahlorinczcomi/sgmm documentation built on Aug. 23, 2023, 1:28 a.m.