Skip to content
Prev 336148 / 398502 Next

package mgcv - predict with bam: Error in X[ind, ] : subscript out of bounds

Hi Katharina,

Thanks for sending this.

The problem is that the prediction data for site contain levels not
available in the (useable non-NA) fit data...

 > levels(m$model$site)
[1] "KRB"     "NP.FOR"  "WKS.FRE" "WKS.KRE" "WKS.RIE" "WKS.WUE"
 > levels(gapData$site)
[1] "KRB"     "NP.FOR"  "RIE.2"   "WKS.BBR" "WKS.FRE" "WKS.HOE" "WKS.KRE"
[8] "WKS.RIE" "WKS.WUE"

predict.lm has a check for this, and so fails with a rather more 
informative error message. e.g.

m0 <- lm(sensor1 ~ sensor2 + site + site:NthSampling, 
data=xylemRohWeekXnn2011,na.action=na.omit)
predict(m0,gapData)
... factor site has new levels RIE.2, WKS.BBR

I'll add a better check to predict.gam.

best,
Simon

ps. if you want predictions with the random effects for site set to zero 
then one trick is to use terms like s(site,bs="re",by=dum) in fitting 
with dum set to 1. Then in prediction you can set 'site' to any existing 
level, and dum to zero, in order to get a prediction for the missing 
level, with the 'site' effect set to zero.
On 02/02/14 17:52, Katharina May wrote: