[R] [mgcv][gam] Odd error: Error in PredictMat(object$smooth[[k]], data) : , `by' variable must be same dimension as smooth arguments

Andrew Crane-Droesch andrewcd at gmail.com
Thu Mar 21 22:14:31 CET 2013


Dear List,

I'm getting an error in mgcv, and I can't figure out where it comes 
from.  The setup is the following:  I've got a fitted GAM object called 
"MI", and a vector of "prediction data" (with default values for 
predictors).  I feed this into predict.gam(object, newdata = whatever) 
via the following function:

makepred = function(varstochange,val){
     for (i in 1:length(varstochange)){
         if (varstochange[i] == "pot.trial"){j=1}
         if (varstochange[i] == "year"){j=2}
         if (varstochange[i] == "crop.legume"){j=3}
         if (varstochange[i] == "crop.fruit"){j=4}
         if (varstochange[i] == "feedstock"){j=5}
         if (varstochange[i] == "BCAR.imp"){j=8}
         if (varstochange[i] == "INAR.imp"){j=9}
         if (varstochange[i] == "bcph.imp"){j=10}
         if (varstochange[i] == "phi.imp"){j=11}
         if (varstochange[i] == "htt.imp"){j=12}
         if (varstochange[i] == "bc.prc.C.imp"){j=13}
         if (varstochange[i] == "CEC.imp"){j=14}
         if (varstochange[i] == "soc.imp"){j=15}
         if (varstochange[i] == "sand.imp"){j=16}
         if (varstochange[i] == "clay.imp"){j=17}
         if (varstochange[i] == "abslat.imp"){j=18}
         preddat[j] = val[i]
         }
     predict.gam(MI,newdata=preddat,se.fit=TRUE)
     }

I then make predictions that look like this:

a = makepred(c("phi.imp","bcph.imp","year"),c(4.5,7.25,1))
b = makepred(c("phi.imp","bcph.imp","year"),c(5.5,7.25,1))
c = makepred(c("phi.imp","bcph.imp","year"),c(6.5,7.25,1))
d = makepred(c("phi.imp","bcph.imp","year"),c(7.5,7.25,1))
makepHplot(a,b,c,d,title="1st harvest, BC pH = 7.25")

where "makepHplot" is a different function that I made.

This worked for quite some time.  Then I added some data to the model 
and changed the specification slightly.  Now I'm getting this error message:

1> a = makepred(c("bcph.imp","year"),c(7.5,1))
Error in PredictMat(object$smooth[[k]], data) :
   `by' variable must be same dimension as smooth arguments

I never got this message with the old fitted model (and still don't).  
What is happening?  I can't figure out what about the new fitted model 
is causing this problem.  Typing "PredictMat" isn't helping me, nor is 
google.  The problem isn't that all of the variables aren't in the 
prediction data.

Would appreciate any help here.

Thanks,
Andrew



More information about the R-help mailing list