1

I have previously run mixed model analyses using glmer() in package lme4. I applied functions dredge() and get.models() in package MuMIn to quantify the top.models. I then used a model.avg() approach in package MuMIn to create a fitted object for function predict(). Finally, I created a newdata object called newdat, i.e. a new object for each predictor.

I then used newdatfinal <- predict(avModX, newdata = newdat, se.fit=TRUE, re.form=NA), where avModX presents the fitted model derived from subset.top.models <- c(top.models[[1]],top.models[[1]]) and avModX <- model.avg(subset.top.models). This all works fine.

I now need to use predict() on a segmented.lme() object. The code for function segmented.lme() can be found here: https://www.researchgate.net/publication/292986444_segmented_mixed_models_in_R_code_and_data. A reference working paper is available here: https://www.researchgate.net/publication/292629179_Segmented_mixed_models_with_random_changepoints_in_R. This function allows for detection of differences in slope and provides changepoint estimates, i.e. a test for breakpoint(s) in the data.

I first used the function

global.model.lme <- lme(response ~ predictor1*predictor2*predictor3*
                              predictor4 + covariate1 + covariate2 + covariate3,
                            data = mydat,
                            random = list(block = pdDiag(~ 1 + predictor1),
                                          transect = pdDiag(~ 1 + predictor1)),
                            na.action="na.fail") 

and followed by function

global.model.seg <- segmented.lme(global.model.lme, 
                                      Z = predictor1, 
                                      random = list(block = pdDiag(~ 1 + predictor1 + U + G0),
                                                    transect = pdDiag(~ 1 + predictor1 + U + G0)),
                                      psi.link = "identity")

Z = the 'segmented' covariate having a segmented relationship with the response, U = slope difference, G0 = the formula of random effects for changepoints (changepoint estimate)

I would now like to use the segmented.lme() object in function predict(), i.e. something like newdatfinal <- predict(global.model.seg, newdata = newdat, se.fit=TRUE, re.form=NA)

I currently get the error message: Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "segmented.lme"

This is a reproducible subset of the original data:

structure(list(block = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8"), class = "factor"), transect = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("B1L", "B1M", "B1S", "B2L", "B2M", "B2S", "B3L", "B3M", "B3S", "B4L", "B4M", "B4S", "B5L", "B5M", "B5S", "B6L", "B6M", "B6S", "B7L", "B7M", "B7S", "B8L", "B8M", "B8S"), class = "factor"), predictor1 = c(28.63734661, 31.70995133, 27.40407982, 25.48842992, 21.81094637, 24.02032756), predictor2 = c(5.002945364, 6.85567854, 0, 22.470422, 0, 0), predictor3 = c(3.72, 3.55, 3.66, 3.65, 3.53, 3.66), predictor4 = c(504.8, 547.6, 499.7, 497.8, 473.8, 467.5), covariate1 = c(391L, 394L, 351L, 336L, 304L, 335L), covariate2 = c(0.96671086, 2.81939707, 0.899512367, 1.024730094, 1.641161861, 1.419433714), covariate3 = c(0.787505444, 0.641693911, 0.115804751, -0.041146951, 1.983567486, -0.451039179), response = c(0.81257636, 0.622662116, 0.490330786, 0.709929461, -0.156398286, -1.185175095)), .Names = c("block", "transect", "predictor1", "predictor2", "predictor3", "predictor4", "covariate1", "covariate2", "covariate3", "response"), row.names = c(NA, 6L), class = "data.frame")

and a reproducible subset of the newdat data:

structure(list(predictor1 = c(-0.441935, -0.433467318435754,0.424999636871508, -0.416531955307263, -0.408064273743017, -0.399596592178771), covariate1 = c(0L, 0L, 0L, 0L, 0L, 0L), covariate2 = c(0L, 0L, 0L, 0L, 0L, 0L), covariate3 = c(0L, 0L, 0L, 0L, 0L, 0L), 
predictor2 = c(0L, 0L, 0L, 0L, 0L, 0L), predictor3 = c(0L, 
0L, 0L, 0L, 0L, 0L), predictor4 = c(0L, 0L, 0L, 0L, 0L, 0L
)), .Names = c("predictor1", "covariate1", "covariate2", "covariate3", "predictor2", "predictor3", "predictor4"), row.names = c(NA, 6L), class = "data.frame")

Many thanks in advance for any advice.

tabtimm
  • 411
  • 3
  • 6
  • 17

1 Answers1

2

segmented.lme is at preliminary stage, so currently there is no predict method function. However, since the algorithm relies on working linear model, you could use the last one (at convergence) to make predictions,

predict(global.model.seg[[2]], ..)

Results should be carefully checked.

user229044
  • 232,980
  • 40
  • 330
  • 338