[R-sig-ME] predict() fails on new data for nlme model object
Dave Auty
daveauty at gmail.com
Sun Mar 6 23:16:02 CET 2016
Dear R-Mixed-Modellers,
I've come across some unusual behavior when using the predict function in
nlme_3.1-125. If I allow fixed effects parameters to vary by levels of a
categorical variable, the predict function fails on new data. I made a
reproducible example in the hope that someone might know what's going on:
#########
library(ggplot2)
library(dplyr)
library(nlme)
# Simulate density data with some random noise to fit Michaelis-Menten
equation as function of age. TreeIDs grouped by SP (spacing)
set.seed(1)
df <- data.frame(SP = rep(LETTERS[1:5], 250), expand.grid(TreeID =
factor(1:25), age = seq(1, 50, 1))) %>%
group_by(TreeID) %>%
mutate(dens = ((runif(1,10,20)*age)/(runif(1,9,10)+age)) +
rnorm(50, 0, 1))
# plot data by SP treatment:
ggplot(df) +
geom_line(aes(age, dens, group=TreeID)) +
facet_wrap(~SP)
# fit mixed-effects model:
fit1 <-nlme(dens ~ a*age/(b+age),
fixed=a+b~1,
start=c(a=15, b=5),
random=a~1|TreeID,
data=df)
summary(fit1)
# allow fixed effects parameters to vary by SP:
fit2 <-nlme(dens ~ a*age/(b+age),
fixed=list(a~SP, b~SP),
start=c(a=rep(14, 5), b=rep(4, 5)),
random=a~1|TreeID,
data=df)
summary(fit2)
# make new data for predictions:
newdat2 <- expand.grid(SP = LETTERS[1:5], age = seq(1, 50, 1))
df$newpred1 <- predict(fit1, newdat2, level=0) # works fine
df$newpred2 <- predict(fit2, newdat2, level=0) # throws the error:
Error in eval(expr, envir, enclos) : object 'SP' not found
This worked fine in previous versions of nlme. I'd appreciate any insight
into what might be happening here.
Many thanks.
Dave Auty, PhD.
Assistant Professor of Wood Science and Utilization
School of Forestry
Northern Arizona University
sessionInfo()
# R version 3.2.3 (2015-12-10)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows >= 8 x64 (build 9200)
#
# locale:
# [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United
States.1252 LC_MONETARY=English_United States.1252
# [4] LC_NUMERIC=C LC_TIME=English_United
States.1252
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] dplyr_0.4.3 ggplot2_2.0.0 nlme_3.1-124
#
# loaded via a namespace (and not attached):
# [1] Rcpp_0.12.3 lattice_0.20-33 assertthat_0.1
grid_3.2.3 plyr_1.8.3 R6_2.1.2 gtable_0.2.0
# [8] DBI_0.3.1 magrittr_1.5 scales_0.4.0 lazyeval_0.1.10
labeling_0.3 tools_3.2.3 munsell_0.4.3
# [15] parallel_3.2.3 colorspace_1.2-6
[[alternative HTML version deleted]]
More information about the R-sig-mixed-models
mailing list