Skip to content

Plot with residuals in mgcv

4 messages · silje skår, Peter Ehlers, William Dunlap +1 more

#
On 2012-11-28 05:02, silje sk?r wrote:
Well, the error means that the matrix X[, first:last] and
the vector object$coefficients[first:last] don't have
'matching' dimensions. I don't see why, but since the
'residuals' argument to plot.gam() can be an array
_of the correct length_ (see ?plot.gam), it just might be
that you have an object called 'T' hanging around, in which
case using 'T' in place of 'TRUE' is a bad idea.
Actually, it's _always_ a bad idea.

Peter Ehlers
#
Those errors often come from omitting the drop=FALSE argument to [ when subscripting
a matrix.  It looks like that is the problem here (I am still using R 2-2.15.1 and mgcv 1.7.18):

  > fit <- gam(mpg ~ s(wt, bs="re") + am, data=mtcars)
  > plot(fit, residuals=TRUE)
  Error in X[, first:last] %*% object$coefficients[first:last] : 
    non-conformable arguments

  Enter a frame number, or 0 to exit   

  1: plot(fit, residuals = TRUE)
  2: plot.gam(fit, residuals = TRUE)
  3: predict(x, type = "terms")
  4: predict.gam(x, type = "terms")

  Selection: 4
  Called from: predict.gam(x, type = "terms")
  Browse[1]> first:last # length 1 result means first==last
  [1] 3
  Browse[1]> try(X[, first:last] %*% object$coefficients[first:last])
  Error in X[, first:last] %*% object$coefficients[first:last] : 
    non-conformable arguments
  Browse[1]> try(X[, first:last, drop=FALSE] %*% object$coefficients[first:last])
              [,1]
   [1,] -13.720247
   [2,] -15.055614
   [3,] -12.149226
   [4,] -16.836104
   [5,] -18.014370
   ...
   [32,] -14.558125

If this is still the problem in the latest version of mgcv you should write
to mgcv's maintainer about the problem.
  > maintainer("mgcv")
  [1] "Simon Wood <simon.wood at r-project.org>"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
Hi Silje,

Thanks for this. I guess RUTE is a numeric variable in this model and 
hence only has one associated random coefficient? This then causes a 
problem when calling predict.gam as part of processing 'residuals=T'. 
I've fixed the problem for the next release, but did you really want a 
single random slope for RUTE, or should it really have been declared as 
a factor variable? btw gamma doesn't do anything with ML and REML 
smoothness selection (it's only used in prediction error criteria 
smoothness selection to increase the penalization per effective degrees 
of freedom).

best,
Simon
On 28/11/12 13:02, silje sk?r wrote: