Search code examples
rgamrandom-effects

Extracting random effects from a gamlss object


I have run a beta regression with random intercepts model using the gamlss package in R. The call looks like:

uik.max.model <- gamlss(formula = max.opp.vote ~ n_commute + vote_commute + uik_dummy +
                      opp.gd + opp.m + pay + govt_dep + higher_ed + max.opp.n +
                      opp.m*vote_commute + opp.m*n_commute +
                      re(random = ~1|mf),
                    family = BE(),
                    data = poll_df)

I want to extract the random effect for each grouping factor (mf) from the model object in order to create a predicted probability plot, but I am unable to find where/if they are stored in the object.

Are the random intercepts for each value of the grouping factor available? If so, where?


Solution

  • To analyze the structure of the fitted object we can use str(uik.max.model). Thus the random effects can be found in uik.max.model$mu.coefSmo[[1]]$coefficients$random. Consider this example:

    library(gamlss)
    # creating some data
    variable <- as.factor(rep(1:26, 1e3))
    levels(variable) <- LETTERS
    value <- rbinom(2.6e4, 10, .5)
    df1 <- data.frame(variable, value)
    
    # fitting a minimal model
    fit <- gamlss(formula = value ~ 1 +
                  re(random = ~1|variable),
                data = na.omit(df1))
    
    # analyzing structure of fitted object
    str(fit)
    # ...
    
    # random effects for each value of "variable"
    fit$mu.coefSmo[[1]]$coefficients$random
    # $variable
    # (Intercept)
    # A -1.029350e-07
    # B -2.465111e-09
    # C  1.326496e-07
    # D -1.632303e-08
    # E  2.731609e-09
    # F -4.403887e-08
    # G -2.465111e-09
    # H -7.695143e-08
    # I  2.698297e-08
    # J  4.950209e-08
    # K -3.191319e-08
    # L  9.627257e-08
    # M -4.057439e-08
    # N  3.737641e-08
    # O -1.285855e-08
    # P  1.551687e-07
    # Q  1.312505e-08
    # R -8.907712e-08
    # S -9.394071e-09
    # T  2.178625e-08
    # U -7.661831e-09
    # V -1.978751e-08
    # W -8.734488e-08
    # X  3.737641e-08
    # Y -1.285855e-08
    # Z -1.632303e-08