1

I'm trying to predict values with a linear mixed model and new data, but I keep getting errors that both functions I'm trying (predict.lme from nlme, and predict.merMod from lme4) do not exist. The packages are installed and loaded.

I first tried using predict.lme (nlme). I note that I've installed and loaded the package, since obviously this is a reason the function may not be found.

But I get this error:

Error in predict.lme(object = PlotModel.best, newdata = PlotInvData_predict,:
could not find function "predict.lme"

I've had random problems with nlme before, so I decide to try predict.merMod (lme4), but I get the same error that that function can't be found. I thought maybe those functions can't handle the interval argument that the regular predict function can. I get rid of that, and it still doesn't work. I tested other functions in those packages, and they work just fine. So something is wrong with my workflow, but I can't figure out what. It's the same workflow as with the regular predict function, which I've used just fine.

What am I doing wrong?

Here's the new data:

PlotInvData_predict <- read.csv(file="D:/ThesisPart2/Data/PlotInvData_predict.csv", header=TRUE, sep=",")

Heres the model:

 PlotModel.best <- lmer(d_InvCov ~ TimeSinceDist + UnitArea_ha + (1|MgmtSame)+ (1|LandMgmt.1), PlotInvData)

Then I first tried using predict.lme (nlme). I note that I've installed and loaded the package, since obviously this is a reason the function may not be found.

install.packages("nlme")
library(nlme)
p_bd <- predict.lme(object=PlotModel.best, newdata=PlotInvData_predict, interval="confidence")

Here's the error, again:

Error in predict.lme(object = PlotModel.best, newdata = PlotInvData_predict, :
could not find function "predict.lme"

When using the regular predict function with a different model (linear model), I would get something like this as a result:

   fit        lwr       upr
1 1.098959 0.5803632 1.617556
2 1.156005 0.6627035 1.649306
3 1.213050 0.7408797 1.685220
4 1.270095 0.8143122 1.725879
5 1.327141 0.8824762 1.771805
6 1.384186 0.9449715 1.823401
7 1.441231 1.0015871 1.880875
user20650
  • 24,654
  • 5
  • 56
  • 91
Melissa
  • 41
  • 4
  • 1
    `predict.lme` is not exported. To see this look at `methods(predict)` -- `predict.lme` will have an asterisk next to it. So use `nlme:::predict.lme` or just `predict` – user20650 Apr 09 '19 at 21:34
  • ... and you cant pass a model fit by `lme4` to the predict method of `nlme` – user20650 Apr 09 '19 at 21:35
  • 1
    @user20650’s comment correctly diagnoses the issue. But I need to protest their advice: Do *not* use `nlme:::predict.lme`! Doing so isn’t guaranteed to work in the future, and this isn’t how the package is supposed to be used. Just use `predict` instead. – Konrad Rudolph Apr 09 '19 at 21:54
  • @KonradRudolph, is there a difference between `nlme::predict.lme` and `nlme:::predict.lme` , not directly relevant to OP but I always used `::` and not `:::` for calling functions from a specific package? – Roasty247 Apr 09 '19 at 22:54
  • `:::` lets you call functions that are not exported by a package, but unexported functions are subject to change at any time and authors will not be careful about changing them – Calum You Apr 10 '19 at 00:27
  • 1
    @Roasty247 Using `::` is fine, and even encouraged. `:::` is a hack to access package internals. It simply shouldn’t exist, and its use should be banned from all code (if somebody really needs to glimpse inside the private namespace of another package then there are other ways, so `:::` is completely unnecessary). – Konrad Rudolph Apr 10 '19 at 09:11

0 Answers0