strengejacke / sjPlot

sjPlot - Data Visualization for Statistics in Social Science

Home Page:https://strengejacke.github.io/sjPlot

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error "could not access model information"

kaijagahm opened this issue · comments

Hi! My colleague and I are trying to plot effects from a glm model. I'm working on getting more information about how the model was run, but I know it's a resource selection function (animal movement data, with spatial rasters as predictors). However, it looks like the resulting model is just a glm, so we figured it would be fine to plot with sjPlot.

However, we are getting an error when we try to plot it. Here's a small example model (sorry for the gross data structure, but I wanted to make sure it was reproducible). When we run plot_model() on the model object, we get an error that sjPlot "Could not access model information."

library(sjPlot)
mod <- structure(list(call = stats::glm(formula = formula, family = stats::binomial(link = "logit"), 
    data = data), terms = case_ ~ scale(canopy) + scale(grass) + 
    scale(herbs) + scale(shrubs) + scale(litter) + scale(canopy) + 
    scale(twigs) + scale(distance) + scale(dbh), family = structure(list(
    family = "binomial", link = "logit", linkfun = function (mu) 
    .Call(C_logit_link, mu), linkinv = function (eta) 
    .Call(C_logit_linkinv, eta), variance = function (mu) 
    mu * (1 - mu), dev.resids = function (y, mu, wt) 
    .Call(C_binomial_dev_resids, y, mu, wt), aic = function (y, 
        n, mu, wt, dev) 
    {
        m <- if (any(n > 1)) 
            n
        else wt
        -2 * sum(ifelse(m > 0, (wt/m), 0) * dbinom(round(m * 
            y), round(m), mu, log = TRUE))
    }, mu.eta = function (eta) 
    .Call(C_logit_mu_eta, eta), initialize = {
        if (NCOL(y) == 1) {
            if (is.factor(y)) 
                y <- y != levels(y)[1L]
            n <- rep.int(1, nobs)
            y[weights == 0] <- 0
            if (any(y < 0 | y > 1)) 
                stop("y values must be 0 <= y <= 1")
            mustart <- (weights * y + 0.5)/(weights + 1)
            m <- weights * y
            if ("binomial" == "binomial" && any(abs(m - round(m)) > 
                0.001)) 
                warning(gettextf("non-integer #successes in a %s glm!", 
                  "binomial"), domain = NA)
        }
        else if (NCOL(y) == 2) {
            if ("binomial" == "binomial" && any(abs(y - round(y)) > 
                0.001)) 
                warning(gettextf("non-integer counts in a %s glm!", 
                  "binomial"), domain = NA)
            n <- (y1 <- y[, 1L]) + y[, 2L]
            y <- y1/n
            if (any(n0 <- n == 0)) 
                y[n0] <- 0
            weights <- weights * n
            mustart <- (n * y + 0.5)/(n + 1)
        }
        else stop(gettextf("for the '%s' family, y must be a vector of 0 and 1's\nor a 2 column matrix where col 1 is no. successes and col 2 is no. failures", 
            "binomial"), domain = NA)
    }, validmu = function (mu) 
    all(is.finite(mu)) && all(mu > 0 & mu < 1), valideta = function (eta) 
    TRUE, simulate = function (object, nsim) 
    {
        ftd <- fitted(object)
        n <- length(ftd)
        ntot <- n * nsim
        wts <- object$prior.weights
        if (any(wts%%1 != 0)) 
            stop("cannot simulate from non-integer prior.weights")
        if (!is.null(m <- object$model)) {
            y <- model.response(m)
            if (is.factor(y)) {
                yy <- factor(1 + rbinom(ntot, size = 1, prob = ftd), 
                  labels = levels(y))
                split(yy, rep(seq_len(nsim), each = n))
            }
            else if (is.matrix(y) && ncol(y) == 2) {
                yy <- vector("list", nsim)
                for (i in seq_len(nsim)) {
                  Y <- rbinom(n, size = wts, prob = ftd)
                  YY <- cbind(Y, wts - Y)
                  colnames(YY) <- colnames(y)
                  yy[[i]] <- YY
                }
                yy
            }
            else rbinom(ntot, size = wts, prob = ftd)/wts
        }
        else rbinom(ntot, size = wts, prob = ftd)/wts
    }), class = "family"), deviance = 69.521975919166, aic = 85.521975919166, 
    contrasts = NULL, df.residual = 57L, null.deviance = 86.6161975139079, 
    df.null = 64L, iter = 15L, deviance.resid = c(`1` = -0.798064823330203, 
    `2` = -2.03933398033762, `3` = -1.25575265109358, `4` = -0.798064823330203, 
    `5` = -1.01076765259479, `6` = -0.798064823330203, `7` = -0.798064823330203, 
    `8` = -1.01076765259479, `9` = -0.798064823330203, `10` = -0.798064823330203, 
    `11` = -1.01076765259479, `12` = -1.25575265109358, `13` = -1.89301847282484, 
    `14` = -1.35372872605567, `15` = -1.01076765259479, `16` = -1.25575265109358, 
    `17` = -1.01076765259479, `18` = -2.03933398033762, `19` = -1.25575265109358, 
    `20` = -1.48230380736751, `21` = -0.798064823330203, `22` = -1.25575265109358, 
    `23` = -1.01076765259479, `24` = -1.35372872605567, `25` = -0.798064823330203, 
    `26` = 0.516781177336254, `27` = 1.35372872605568, `28` = 1.35372872605568, 
    `29` = 1.01076765259479, `30` = 1.01076765259479, `31` = 1.61200681396219, 
    `32` = 1.10103206453791, `33` = 1.10103206453791, `34` = 1.10103206453791, 
    `35` = 1.10103206453791, `36` = 1.61200681396219, `37` = 1.01076765259479, 
    `38` = 1.10103206453791, `39` = 0.516781177336254, `40` = 1.10103206453791, 
    `41` = 0.900516638500554, `42` = 0.000357469456456634, `43` = 0.000357469456456634, 
    `44` = 0.000357469456456634, `45` = 1.61200681396219, `46` = 1.35372872605568, 
    `47` = 0.516781177336254, `48` = 0.516781177336254, `49` = 0.516781177336254, 
    `50` = 0.516781177336254, `51` = 0.516781177336254, `52` = 0.516781177336254, 
    `53` = 0.516781177336254, `54` = 0.516781177336254, `55` = 0.516781177336254, 
    `56` = 1.35372872605568, `57` = 0.603856865149281, `58` = 0.603856865149281, 
    `59` = 0.603856865149281, `60` = 0.603856865149281, `61` = 0.603856865149281, 
    `62` = 0.516781177336254, `63` = 0.516781177336254, `64` = 0.516781177336254, 
    `65` = 0.900516638500554), coefficients = structure(c(1.25781425322686, 
    -3.16168108403283, 4.51389759154932, -15.2939928924097, 14.7536808820167, 
    -11.0186868226195, -7.91987702471343, -1.59714620936048, 
    63.9411894148139, 399.097435423433, 454.161346178966, 1064.03725546773, 
    1032.36107888708, 608.663777744486, 546.476053248414, 126.596574866518, 
    0.0196714240810704, -0.00792207817792255, 0.00993897351574825, 
    -0.0143735501871001, 0.0142912021614778, -0.0181030763214647, 
    -0.0144926332592899, -0.0126160301812627, 0.9843054866476, 
    0.993679162247681, 0.992069977040604, 0.988531961103937, 
    0.98859765857414, 0.985556623804887, 0.988436956455161, 0.989934131321475
    ), dim = c(8L, 4L), dimnames = list(c("(Intercept)", "scale(canopy)", 
    "scale(grass)", "scale(herbs)", "scale(shrubs)", "scale(litter)", 
    "scale(twigs)", "scale(distance)"), c("Estimate", "Std. Error", 
    "z value", "Pr(>|z|)"))), aliased = c(`(Intercept)` = FALSE, 
    `scale(canopy)` = FALSE, `scale(grass)` = FALSE, `scale(herbs)` = FALSE, 
    `scale(shrubs)` = FALSE, `scale(litter)` = FALSE, `scale(twigs)` = FALSE, 
    `scale(distance)` = FALSE, `scale(dbh)` = TRUE), dispersion = 1, 
    df = c(8L, 57L, 9L), cov.unscaled = structure(c(4088.47570378111, 
    -25518.1854978551, 29039.1439114251, -68034.2929020814, 66009.0523248342, 
    -38917.284126303, -34941.4142728239, -8094.53221481939, -25518.1854978551, 
    159278.762961561, -181252.855048157, 424638.868135471, -411999.09037812, 
    242900.542714043, 218087.455225278, 50523.7813119167, 29039.1439114251, 
    -181252.855048157, 206262.528363091, -483239.736060487, 468854.671001479, 
    -276424.189758775, -248184.691833798, -57494.7479757872, 
    -68034.2929020814, 424638.868135471, -483239.736060487, 1132175.2810233, 
    -1098470.4619899, 647638.256020859, 581470.527493075, 134700.251268852, 
    66009.0523248342, -411999.09037812, 468854.671001479, -1098470.4619899, 
    1065769.39720091, -628357.314085163, -564159.934340875, -130690.604784356, 
    -38917.284126303, 242900.542714043, -276424.189758775, 647638.256020859, 
    -628357.314085163, 370471.594338189, 332619.487701676, 77051.2792597018, 
    -34941.4142728239, 218087.455225278, -248184.691833798, 581470.527493075, 
    -564159.934340875, 332619.487701676, 298636.076773964, 69179.9159344326, 
    -8094.53221481939, 50523.7813119167, -57494.7479757872, 134700.251268852, 
    -130690.604784356, 77051.2792597018, 69179.9159344326, 16026.692767934
    ), dim = c(8L, 8L), dimnames = list(c("(Intercept)", "scale(canopy)", 
    "scale(grass)", "scale(herbs)", "scale(shrubs)", "scale(litter)", 
    "scale(twigs)", "scale(distance)"), c("(Intercept)", "scale(canopy)", 
    "scale(grass)", "scale(herbs)", "scale(shrubs)", "scale(litter)", 
    "scale(twigs)", "scale(distance)"))), cov.scaled = structure(c(4088.47570378111, 
    -25518.1854978551, 29039.1439114251, -68034.2929020814, 66009.0523248342, 
    -38917.284126303, -34941.4142728239, -8094.53221481939, -25518.1854978551, 
    159278.762961561, -181252.855048157, 424638.868135471, -411999.09037812, 
    242900.542714043, 218087.455225278, 50523.7813119167, 29039.1439114251, 
    -181252.855048157, 206262.528363091, -483239.736060487, 468854.671001479, 
    -276424.189758775, -248184.691833798, -57494.7479757872, 
    -68034.2929020814, 424638.868135471, -483239.736060487, 1132175.2810233, 
    -1098470.4619899, 647638.256020859, 581470.527493075, 134700.251268852, 
    66009.0523248342, -411999.09037812, 468854.671001479, -1098470.4619899, 
    1065769.39720091, -628357.314085163, -564159.934340875, -130690.604784356, 
    -38917.284126303, 242900.542714043, -276424.189758775, 647638.256020859, 
    -628357.314085163, 370471.594338189, 332619.487701676, 77051.2792597018, 
    -34941.4142728239, 218087.455225278, -248184.691833798, 581470.527493075, 
    -564159.934340875, 332619.487701676, 298636.076773964, 69179.9159344326, 
    -8094.53221481939, 50523.7813119167, -57494.7479757872, 134700.251268852, 
    -130690.604784356, 77051.2792597018, 69179.9159344326, 16026.692767934
    ), dim = c(8L, 8L), dimnames = list(c("(Intercept)", "scale(canopy)", 
    "scale(grass)", "scale(herbs)", "scale(shrubs)", "scale(litter)", 
    "scale(twigs)", "scale(distance)"), c("(Intercept)", "scale(canopy)", 
    "scale(grass)", "scale(herbs)", "scale(shrubs)", "scale(litter)", 
    "scale(twigs)", "scale(distance)")))), class = "summary.glm")

plot_model(sjPlot)

Error in if (fam.info$is_linear) transform <- NULL else transform <- "exp" : 
  argument is of length zero
In addition: Warning message:
Could not access model information. 

I dug a bit into the plot_model code and found the part that's generating this error. We tried explicitly specifying transform = NULL, but that threw a different error... so it seems like there's just something about this model structure that sjPlot doesn't like.

Is there anything we can/should do to be able to plot this model with sjPlot, or is this just a model format that's not implemented yet?

Thank you so much!