1

I am getting an error message when using predict.gls when the package "MuMIn" is installed.

The following (Ex. 1) works:

### EX. 1

library(nlme)

# example code from https://stat.ethz.ch/R-manual/R-devel/library/nlme/html/predict.gls.html

fm1 <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), Ovary,
           correlation = corAR1(form = ~ 1 | Mare))
newOvary <- data.frame(Time = c(-0.75, -0.5, 0, 0.5, 0.75))
predict(fm1, newOvary)

# [1]  9.441686 13.116003 11.316793 13.116003 14.991110
# attr(,"label")
# [1] "Predicted values"

However, the following (Ex. 2) yields an error message, even though the library(MuMIn) line is the only difference from Ex. 1:

### EX. 2

library(nlme)
library(MuMIn) # (This is the only thing different from Ex. 1)

# example code from https://stat.ethz.ch/R-manual/R-devel/library/nlme/html/predict.gls.html

fm1 <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), Ovary,
           correlation = corAR1(form = ~ 1 | Mare))
newOvary <- data.frame(Time = c(-0.75, -0.5, 0, 0.5, 0.75))
predict(fm1, newOvary)

# Error in eval(predvars, data, env) : object 'follicles' not found

Does anyone know why this is the case? There seems to be an incompatibility when using 'predict' when MuMIn is installed

Interestingly, the following (Ex. 3), which calls predict.gls directly, restores it to working:

### EX. 3

library(nlme)
library(MuMIn) 

# example code from https://stat.ethz.ch/R-manual/R-devel/library/nlme/html/predict.gls.html

fm1 <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), Ovary,
       correlation = corAR1(form = ~ 1 | Mare))
newOvary <- data.frame(Time = c(-0.75, -0.5, 0, 0.5, 0.75))
nlme:::predict.gls(fm1, newOvary) # (This is the only thing different from Ex. 2)

# [1]  9.441686 13.116003 11.316793 13.116003 14.991110
# attr(,"label")
# [1] "Predicted values"

However, I have read that it is inadvisable to use 'nlme:::predict.gls' as the ':::' can be 'risky' because it accesses internal functions that aren't meant to be directly available.

Here is my current R.version output:

platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 4.0
year 2017
month 04
day 21
svn rev 72570
language R
version.string R version 3.4.0 (2017-04-21) nickname You Stupid Darkness

Incidentally, I didn't have this problem on my old computer, which used an older version of R. I had a friend try Ex. 2 on his computer, and it produced an error message too.

Any insight into the reason for the error message in Ex. 2, and how I can fix it without resorting to the workaround in Ex. 3, would be much appreciated!

www
  • 38,575
  • 12
  • 48
  • 84
Asteraceae
  • 13
  • 2

2 Answers2

2

"MuMIn" implements its own predict.gls method which allows for se.fit, but as it turns out it may have a bug. If you load "nlme" after "MuMIn", the original method from "nlme" will be used.

Edit: This is fixed now. Please update MuMIn to version 1.16.5, from R-Forge: install.packages("MuMIn", repos="http://R-Forge.R-project.org")

Kamil Bartoń
  • 1,482
  • 9
  • 10
  • Thank you, @KamilBartoń, updating MuMIn using your method (in the "Edit") did the trick and it appears to be working now. – Asteraceae May 26 '17 at 16:53
0

A way to bypass the problem is to use the function MuMIn:::predict.gls adding to the newdata dataset the response variable follicles. You can input random values to the response variables, they will be not used in the calculation.

library(nlme)
library(MuMIn)

fm1 <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), Ovary,
                   correlation = corAR1(form = ~ 1 | Mare))
newOvary <- data.frame(Time = c(-0.75, -0.5, 0, 0.5, 0.75), follicles=rep(1,5))
predict(fm1, newOvary)

############
[1]  9.441686 13.116003 11.316793 13.116003 14.991110
attr(,"label")
[1] "Predicted values"

These estimates are exactly the same given by nlme:::predict.gls

nlme:::predict.gls(fm1)
[1]  9.441686 13.116003 11.316793 13.116003 14.991110
attr(,"label")
[1] "Predicted values"
Marco Sandri
  • 23,289
  • 7
  • 54
  • 58