Generalized Mixed Model

gamlj_gmixed(
  formula = NULL,
  data = NULL,
  model_type = "logistic",
  dep = NULL,
  factors = NULL,
  covs = NULL,
  model_terms = NULL,
  fixed_intercept = TRUE,
  cluster = cluster,
  re = NULL,
  re_corr = "all",
  re_lrt = FALSE,
  re_ci = FALSE,
  nested_terms = NULL,
  nested_intercept = NULL,
  nested_re = NULL,
  es = list("expb"),
  expb_ci = TRUE,
  estimates_ci = FALSE,
  ci_method = "wald",
  boot_r = 1000,
  ci_width = 95,
  contrasts = NULL,
  contrast_custom_values = NULL,
  contrast_custom_focus = NULL,
  show_contrastnames = TRUE,
  show_contrastcodes = FALSE,
  plot_x = NULL,
  plot_z = NULL,
  plot_by = NULL,
  plot_raw = FALSE,
  plot_yscale = FALSE,
  plot_xoriginal = FALSE,
  plot_black = FALSE,
  plot_around = "none",
  plot_re = FALSE,
  plot_re_method = "average",
  plot_scale = "response",
  emmeans = NULL,
  posthoc = NULL,
  simple_x = NULL,
  simple_mods = NULL,
  simple_interactions = FALSE,
  covs_conditioning = "mean_sd",
  ccm_value = 1,
  ccp_value = 25,
  ccra_steps = 1,
  covs_scale_labels = "labels",
  adjust = list("bonf"),
  covs_scale = NULL,
  scale_missing = "complete",
  norm_test = FALSE
)

Arguments

formula

(optional) the formula to use, see the examples

data

the data as a data frame

model_type

Select the generalized linear model: logistic,probit,nb for negative binomial poisson,multinomial, ordinal for cumulative logit models.

dep

a string naming the dependent variable from data; the variable must be numeric. Not needed if formula is used.

factors

a vector of strings naming the fixed factors from data. Not needed if formula is used.

covs

a vector of strings naming the covariates from data. Not needed if formula is used.

model_terms

a list of character vectors describing fixed effects terms. Not needed if formula is used.

fixed_intercept

TRUE (default) or FALSE, estimates fixed intercept. Not needed if formula is used and "1" is passed explicitly to the formula.#'

cluster

a vector of strings naming the clustering variables from data, not needed if formula is defined.

re

a list of lists specifying the models random effects.

re_corr

'all', 'none' (default), or 'block'. When random effects are passed as list of length 1, it decides whether the effects should be correlated, non correlated. If 're' is a list of lists of length > 1, the option is automatially set to 'block'. The option is ignored if the model is passed using formula.

re_lrt

TRUE or FALSE (default), LRT for the random effects

re_ci

TRUE or FALSE (default), confidence intervals for the random effects

nested_terms

a list of character vectors describing effects terms for the nested model. It can be passed as right-hand formula.

nested_intercept

TRUE (default) or FALSE, estimates fixed intercept. Not needed if formula is used.

nested_re

a list of lists specifying the models random effects.

es

Effect size indices. expb (default) exponentiates the coefficients. For dichotomous dependent variables relative risk indices (RR) can be obtained. marginals computes the marginal effects.

expb_ci

TRUE (default) or FALSE , exp(B) CI in table

estimates_ci

TRUE or FALSE (default), parameters CI in table

ci_method

The method used to compute the confidence intervals. 'wald' uses the Wald method to compute standard errors and confidence intervals. `profile` computes Profile Likelihood Based Confidence Interval, in which the bounds are chosen based on the percentiles of the chi-square distribution around the maximum likelihood estimate. 'quantile' performs a non-parametric boostrap, with boot_r repetitions, and compute the CI based on the percentiles of the boostrap distribution. 'bcai' implements the bias-corrected bootstrap method.

boot_r

a number bootstrap repetitions.

ci_width

a number between 50 and 99.9 (default: 95) specifying the confidence interval width for the plots.

contrasts

a named vector of the form c(var1="type", var2="type2") specifying the type of contrast to use, one of 'deviation', 'simple', 'dummy', 'difference', 'helmert', 'repeated' or 'polynomial'. If NULL, simple is used. Can also be passed as a list of list of the form list(list(var="var1",type="type1")).

contrast_custom_values

a named list with the custom contrast weights, of the form list(factorname=numeric vector), for instance list(factorname=c(1,1,-2)). only one constrast per variable is allowed.

contrast_custom_focus

if any factor is coded with 'custom', when TRUE or NULL (default) the coefficients, simple effects and simple interactions are focused on the custom contrast. If FALSE , variables are coded accordingly to the passed contrast, but no special table or test is devoted to the contrast.

show_contrastnames

TRUE or FALSE (default), shows raw names of the contrasts variables in tables

show_contrastcodes

TRUE or FALSE (default), shows contrast coefficients tables

plot_x

a string naming the variable placed on the horizontal axis of the plot

plot_z

a string naming the variable represented as separate lines in the plot

plot_by

a list of string naming the variables defining the levels for multiple plots

plot_raw

TRUE or FALSE (default), plot raw data along the predicted values

plot_yscale

TRUE or FALSE (default), set the Y-axis range equal to the range of the observed values.

plot_xoriginal

TRUE or FALSE (default), use original scale for covariates.

plot_black

TRUE or FALSE (default), use different linetypes per levels.

plot_around

'none' (default), 'ci', or 'se'. Use no error bars, use confidence intervals, or use standard errors on the plots, respectively.

plot_re

TRUE or FALSE (default), add predicted values based on random effect in plot

plot_re_method

.

plot_scale

Plot ordinal model predicted values in as probabilities (response) or predicted class (mean.class)

emmeans

a rhs formula with the terms specifying the marginal means to estimate (of the form '~x+x:z')

posthoc

a rhs formula with the terms specifying the table to apply the comparisons (of the form '~x+x:z'). The formula is not expanded, so 'x*z' becomes 'x+z' and not 'x+z+x:z'. It can be passed also as a list of the form '`list("x","z",c("x","z")`'

simple_x

The variable for which the simple effects (slopes) are computed

simple_mods

the variable that provides the levels at which the simple effects are computed

simple_interactions

should simple Interactions be computed

covs_conditioning

'mean_sd' (default), 'custom' , or 'percent'. Use to condition the covariates (if any)

ccm_value

Covariates conditioning mean offset value: how many st.deviations around the means used to condition simple effects and plots. Used if covs_conditioning='mean_sd'

ccp_value

Covariates conditioning percentile offset value: number of percentiles around the median used to condition simple effects and plots. Used if covs_conditioning='percent'

ccra_steps

Covariate condition steps from min to max: At how many values between min and max should the covariate be conditioned simpleScale='range'

covs_scale_labels

how the levels of a continuous moderator should appear in tables and plots: labels, values and values_labels, ovalues, `ovalues_labels. The latter two refer to the variable orginal levels, before scaling.

adjust

one or more of 'none', 'bonf','tukey' 'holm','scheffe', 'tukey'; provide no, Bonferroni, Tukey and Holm Post Hoc corrections respectively.

covs_scale

a list of lists specifying the covariates scaling, one of 'centered to the mean', 'standardized', or 'none'. 'none' leaves the variable as it is

scale_missing

.

norm_test

TRUE or FALSE (default), provide a test for normality of residuals

Value

A results object containing:

results$modela property
results$infoa table
results$main$r2a table of R
results$main$fita table
results$main$anovaa table of omnibus
results$main$coefficientsa table
results$main$contrastCodeTablesan array of contrast coefficients tables
results$main$marginalsa table
results$main$relativeriska table
results$main$randoma table
results$main$randomcova table
results$main$multirandoman array
results$main$ranovaa table
results$posthocan array of post-hoc tables
results$simpleEffects$anovaa table of ANOVA for simple effects
results$simpleEffects$coefficientsa table
results$simpleInteractionsan array of simple interactions tables
results$emmeansan array of predicted means tables
results$mainPlotsan array of results plots
results$plotnotesa html
results$predictedan output
results$residualsan output

Tables can be converted to data frames with asDF or as.data.frame. For example:

results$info$asDF

as.data.frame(results$info)

Examples

data(clustermanymodels)
clustermanymodels$ybin<-factor(clustermanymodels$ybin)
GAMLj3::gamlj_gmixed(formula = ybin ~ 1 + x+( 1|cluster ),
                     data = clustermanymodels,
                     model_type="logistic"
                     )
#> 
#>  GENERALIZED MIXED MODEL
#> 
#>  Model Info                                                                 
#>  ────────────────────────────────────────────────────────────────────────── 
#>    Info                                                                     
#>  ────────────────────────────────────────────────────────────────────────── 
#>    Model Type       Logistic Model    Model for binary y                    
#>    Model            lme4::glmer       ybin ~ 1 + x + ( 1 | cluster )        
#>    Distribution     Binomial          Dichotomous event distribution of y   
#>    Link function    Logit             Log of the odd of y                   
#>    Direction        P(y=1)/P(y=0)     P( ybin = 1 ) / P( ybin = 0 )         
#>    Sample size                1800                                          
#>    Converged        yes                                                     
#>    C.I. method      Wald                                                    
#>  ────────────────────────────────────────────────────────────────────────── 
#>    Note. All covariates are centered to the mean
#> 
#> 
#>  MODEL RESULTS
#> 
#>  Model Fit                                                             
#>  ───────────────────────────────────────────────────────────────────── 
#>    Type           R²           df           LRT X²        p            
#>  ───────────────────────────────────────────────────────────────────── 
#>    Conditional    0.0436968    2.0000000    39.7507885    < .0000001   
#>    Marginal       0.0185754    1.0000000    26.8376082     0.0000002   
#>  ───────────────────────────────────────────────────────────────────── 
#> 
#> 
#>  Additional Indices                                              
#>  ─────────────────────────────────────────────────────────────── 
#>    Info              Model Value      Comment                    
#>  ─────────────────────────────────────────────────────────────── 
#>    LogLikelihood     -1210.6506201                               
#>    AIC                2427.3012403    Less is better             
#>    BIC                2443.7878661    Less is better             
#>    Deviance           2421.3012403    Less is better             
#>    Residual DF        1797.0000000                               
#>    Chi-squared/DF        0.9822442    Overdispersion indicator   
#>  ─────────────────────────────────────────────────────────────── 
#> 
#> 
#>  Fixed Effects Omnibus Tests                
#>  ────────────────────────────────────────── 
#>         X²          df          p           
#>  ────────────────────────────────────────── 
#>    x    26.24380    1.000000    0.0000003   
#>  ────────────────────────────────────────── 
#> 
#> 
#>  Parameter Estimates (Fixed Coefficients)                                                                                      
#>  ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#>    Names          Effect         Estimate       SE             Exp(B)       Lower        Upper        z            p           
#>  ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#>    (Intercept)    (Intercept)    -0.28754013    0.072358483    0.7501065    0.6509255    0.8643996    -3.973828    0.0000707   
#>    x              x               0.05075563    0.009907654    1.0520658    1.0318332    1.0726951     5.122871    0.0000003   
#>  ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
#> 
#> 
#>  Random Components                                                    
#>  ──────────────────────────────────────────────────────────────────── 
#>    Groups      Name           Variance      SD           ICC          
#>  ──────────────────────────────────────────────────────────────────── 
#>    cluster     (Intercept)    0.08642239    0.2939769    0.02559685   
#>    Residual                   3.28986813    1.8137994                 
#>  ──────────────────────────────────────────────────────────────────── 
#>    Note. Number of Obs: 1800 , Number of groups: cluster 30
#>