0

I recently updated R, and now ggpredict gives me error when calculating the predictions for my mixed effects model (using lme() function).

I want to analyse how anitoxidant activity varies between (plants at) different sites. In order to do that, I have 5 sites with 3 plots each, measured during 6 years.

I have established a linear mixed effects model (function lme()) where "anitox" is modeled against the fixed factor site, and the random factor plot.

Then I use ggpredict() (from package ggeffects) to obtain the model predictions and plot them.

It used to work fine, but since I updated R (version 3.6.0) I get an error message, which i do not know how to solve:

Error: Must use a vector in `[`, not an object of class matrix.

Do you have any idea about what the error means and how to solve the problem? I imagine it must be something with how the data are written/organized, but I do not know what.

Here is the data:

structure(list(year = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L), .Label = c("2011", "2012", "2013", "2014", "2015", "2016" ), class = "factor"), site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("Rebbenes", "Skogsfjord", "Snarby", "Skibotn", "Gukhesjavri"), class = "factor"), plot = c(11, 12, 13, 11, 12, 13, 11, 12, 13, 11, 12, 13, 11, 12, 13, 11, 12, 13, 21, 22, 23, 21, 22, 23, 21, 22, 23, 21, 22, 23, 21, 22, 23, 21, 22, 23, 31, 32, 31, 32, 33, 31, 32, 33, 31, 32, 33, 31, 32, 33, 31, 32, 33, 41, 42, 43, 41, 42, 43, 41, 42, 43, 41, 42, 43, 41, 42, 43, 41, 42, 43, 51, 52, 53, 51, 52, 53, 51, 52, 53, 51, 52, 53, 51, 52, 53, 51, 52, 53), antiox = c(2.46653362547122, 2.47281284862989, 3.08407675482263, 2.87831523045473, 3.034372394251, 3.21454300619676, 2.74011075995272, 2.33528554925283, 3.03876785046206, 2.44794278726812, 2.31854154139051, 2.53086546956933, 2.80912124091414, 2.44954337356346, 3.28600361541029, 3.44682099175027, 1.20643110534499, 3.35829625741535, 3.05272373173726, 2.99366209743898, 3.16879701865554, 3.22166561521105, 3.17619665222095, 3.27004641403846, 3.00891075995272, 2.69110819491058, 2.56533904870318, 2.72225865388593, 2.50975004267303, 2.71080830577306, 3.21333084150044, 3.42928071324833, 3.23869397818054, 3.50487302546225, 3.45334645895437, 3.32049779644063, 2.93581321506448, 3.09215971561413, 3.02336528541992, 3.05388107875081, 3.13256220736936, 2.4930048603558, 2.43519907068925, 2.50833355218431, 2.38878206812421, 2.12290006006265, 2.21622655328361, 3.04982479532968, 3.11766502984782, 3.1409350921417, 3.70615906430432, 3.35201703425668, 3.07677775246848, 3.09648082230486, 3.10361574352142, 3.16501054015022, 2.68618331400182, 3.15280346790435, 3.17590731546756, 2.85941599996738, 2.76602794573506, 2.8732056665119, 2.9199920351451, 2.79834747669878, 2.58608510953132, 2.83503842669648, 2.73056939041945, 2.74792959562282, 3.45827133986313, 3.50868980816654, 3.71756016360809, 3.1311150472957, 2.990842126812, 3.04599463688895, 2.62511479073322, 2.66155890945803, 2.76085682078086, 2.69787990616012, 3.01615649098973, 3.04644450857859, 2.49460544665114, 2.41654608424733, 2.6073852194617, 1.64795744465177, 2.71924216432931, 2.72552138748798, 3.09217877114499, 3.14044260405082, 2.87856206172762)), row.names = c(NA, -89L), class = c("tbl_df", "tbl", "data.frame"))

And this is the model:

m.antiox <- lme(antiox~site, random=~1|plot, data=antiox)

ggpredict(m.antiox)

Mikel
  • 1
  • 3
  • 2
    Welcome to Stack Overflow! Some suggestions to help others help you better: "I decided to make some dummy data to post here, but with the dummy data I actually do not get the error message and I get the predictions I want." Then it won't help us help you. "I would rather not write the whole dataframe by hand." Luckily, you don't have to! You can just edit your question you include the output of `dput(your_data_frame)` or `dput(head(your_data_frame))`; see [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – duckmayr Jun 10 '19 at 11:58

1 Answers1

1

Can't reproduce your error, for me, everything works fine:

library(nlme)
library(ggeffects)

snowmelt <- c(rep("early", 20), rep("mid",20), rep("late",20))
transect <- c(rep("T1",10), rep("T2",10), rep("T3",10), rep("T4",10), rep("T5",10), rep("T6",10))
plot <- c("T1_1","T1_1","T1_2","T1_2","T1_3","T1_3","T1_4","T1_4","T1_5","T1_5","T2_1","T2_1","T2_2","T2_2","T2_3","T2_3","T2_4","T2_4","T2_5","T2_5","T3_1","T3_1","T3_2","T3_2","T3_3","T3_3","T3_4","T3_4","T3_5","T3_5","T4_1","T4_1","T4_2","T4_2","T4_3","T4_3","T4_4","T4_4","T4_5","T4_5","T5_1","T5_1","T5_2","T5_2","T5_3","T5_3","T5_4","T5_4","T5_5","T5_5","T6_1","T6_1","T6_2","T6_2","T6_3","T6_3","T6_4","T6_4","T6_5","T6_5")
soillayer <- c(rep(c("shallow","deep"),30))
success <- runif(60)

plantsuccess <- data.frame(snowmelt, plot, soillayer, success)
m.success <- lme(success ~ snowmelt+soillayer, random=~1|transect/plot, data=plantsuccess)

ggpredict(m.success, c("snowmelt","soillayer"))
#> 
#> # Predicted values of success
#> # x = snowmelt
#> 
#> # soillayer = deep
#>  x predicted std.error conf.low conf.high
#>  1     0.479     0.088    0.307     0.651
#>  2     0.599     0.088    0.427     0.771
#>  3     0.465     0.088    0.293     0.637
#> 
#> # soillayer = shallow
#>  x predicted std.error conf.low conf.high
#>  1     0.521     0.088    0.349     0.693
#>  2     0.642     0.088    0.470     0.814
#>  3     0.508     0.088    0.336     0.680

Created on 2019-06-10 by the reprex package (v0.3.0)

Daniel
  • 7,252
  • 6
  • 26
  • 38
  • I could not share the original data, but I have the same problem with a different analysis. So I have rephrased the question and added the data that give me problems. Could you check this one as well? Thanks in advance! – Mikel Jun 17 '19 at 09:48
  • Ok, I see. Please use data frames, not tibbles. – Daniel Jun 17 '19 at 20:55
  • Done, thanks for your help! I needed to use as.data.frame. But I don't know the difference between a data fram and tibbles. Actually, when using is.data.frama I got the answer TRUE – Mikel Jun 26 '19 at 11:57
  • `Actually, when using is.data.frama I got the answer TRUE` - Yes, and that's actually a dangerous and annoying thing, because tibble do not behave 100% in the same way like data frames, so you run into problems every now and then, where those issues are hard to detect. – Daniel Jun 27 '19 at 13:41