[R] varIdent error using gam function in mgcv

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Jun 21 11:07:18 CEST 2010


On Fri, 2010-06-18 at 10:35 -0700, niallllll wrote:
> Hello,
> 
> As I am relatively new to the R environment this question may be either
> a) Really simple to answer

Use gamm() not gam()

> b) Or I am overlooking something relatively simple.

Use gamm() not gam() ;-)

> I am trying to add a VarIdent structure to my gam model which is fitting
> smoothing functions to the time variables year and month for a particular
> species. When I try to add the varIdent weights to variable Month I get this
> error returned. 
> 
> Error in model.frame.default(formula = Chel ~ 1 + M + YM + M + YM, weights =
> Vf1,  : 
>   variable lengths differ (found for '(weights)')

In gam(), weights is an argument where you need to specify the numeric
prior weights for the observations. If you have n observations then
length(weights) == n.

varIdent is a variance function from the nlme package. It specifies a
variance function, the parameters of which will be estimated as part of
the model fitting, but only within a call to lme (and/or nlme I
presume).

To use it, you'll need to use the gamm() function which will eventually
use lme (via glmmPQL in package MASS). Whether this is something you
want to do is another matter.

I'm not even sure that 'weights' in glm/gam should be used in the way
you are trying to; I recall some discussion on this list that weights
are for sampling weights, but I may be mis-remembering.

Regardless, you can't use varIdent like that in a gam() model.

HTH

G

> Here is the simple code I am using.
> library(mgcv); library(lattice)
> library(nlme)
> Vf1=varIdent(form=~M)
> Chel2=gam(Chel~s(M, fx=F, bs="tp")+ s(YM, fx=F, bs="tp")+s(M,YM, fx=F,
> bs="tp"), family=poisson(link="log"), weights=Vf1) # second GAM  with
> interaction
> 
> M=Month
> YM=Year
> 
> Regards,
> Niall

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list