R/nerve/MixedModelling.R

# Title     : MixedModelling.R
# Objective : Mixed modelling via GLMM
# Created by: greyhypotheses
# Created on: 31/03/2022


source(file = 'R/functions/ValuationOfModelsGLMM.R')


#' Hierarchical, mixed, modelling via glmer(.)
#' 
#' @param nerve: The data frame of the nerve data set
#' 
MixedModelling <- function (nerve) {
  

  # model fitting: glmm -> patient level random intercept
  model <- glmer(formula = cbind(reactionCount, 15 - reactionCount) ~ painScoreStd + (1|patient),
                 family = binomial(link = 'logit'), data = nerve)
  modelglmm <- list('1' = model)


  # model fitting: glmm -> patient level random intercept & location level random intercept
  model <- glmer(formula = cbind(reactionCount, 15 - reactionCount) ~ painScoreStd + (1|patient) + (1|location),
                 family = binomial(link = 'logit'), data = nerve)
  modelglmm <- append(modelglmm, list('2' = model))


  # model fitting: glmm -> patient level random intercept & patient level random slope
  model <- glmer(formula = cbind(reactionCount, 15 - reactionCount) ~ painScoreStd + (painScoreStd|patient),
                 family = binomial(link = 'logit'), data = nerve)
  modelglmm <- append(modelglmm, list('3' = model))


  # model fitting: glmm -> patient level random intercept & patient level random slope of pain score
  #                        location level random intercept & location level random slope of pain score
  model <- glmer(formula = cbind(reactionCount, 15 - reactionCount) ~ painScoreStd +
    (painScoreStd|patient) + (painScoreStd|location),
                 family = binomial(link = 'logit'),
                 data = nerve)
  modelglmm <- append(modelglmm, list('4' = model))


  # clean-up
  rm(model)


  # valuations
  valuations <- ValuationOfModelsGLMM(models = modelglmm)


  return(list(models = modelglmm, valuations = valuations))

}
premodelling/mixed documentation built on April 25, 2022, 6:27 a.m.