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

Random effect 'not found in model terms' for lme mixed model with nested random effects

pauljerem opened this issue · comments

Hi,

I'm trying to create a marginal effects plot for a single predictor at each level of a random effect for an lme mixed model with an exponential correlation structure and nested random effects. But, it seems for some reason plot_model can't find the random effects as I get the error:

`id` was not found in model terms. Maybe misspelled?
Error in names(x) <- value : 
  'names' attribute [5] must be the same length as the vector [4]
In addition: Warning message:
In names(focal_terms) != colnames(datlist) :
  longer object length is not a multiple of shorter object length

I've had a dig around in the issues, but can't find anything with the same error. Consequently, I'm struggling to work out what the issue is.

Here's a very minimal reproducible example (my data is more like 1500 lines):

library(nlme)
library(sjPlot)

mydata <- structure(list(id = c("F530", "F530", "F530", "F530", "F530", "M391", "M391", "M391", "M391", "M391", "M391", "M391"),testforid = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("1", "2"), class = c("ordered", "factor")), time = c(12.043, 60.308, 156.439, 900.427, 1844.542, 42.095, 61.028, 130.627, 194.893, 238.893, 905.282, 1859.534), a = c(35.5786398928957, 35.4973671656257, 36.7414694383557, 37.4316029157078, 36.0805603474457, 38.892219234833, 37.081136308003, 37.339272893363, 36.744902161663, 36.741897283613, 38.158072893363, 38.946697283613), b = c(0.0079975108148372, 0.0151689857479705, 0.0275942757878888, 0.0125676102827941, 0.0352227834243443, 0.0195902976534779, 0.0118588484445401, 0.0069799148425349, 0.00723445099500534, 0.00787758751826021, 0.0162518412492866, 0.0127526068249484), c = c(1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L), class = "data.frame")

mod <- lme(a ~ b + c,
                    random = list(id = ~1, testforid = ~1),
                    correlation = corExp(metric = "maximum", nugget = TRUE),
                    method = "ML",
                    data = mydata)

plot <- plot_model(model, type = "pred", terms = c("b", "id"), pred.type="re")

Any assistance would be most gratefully received! :)