gam                   package:mgcv                   R Documentation

_G_e_n_e_r_a_l_i_z_e_d _a_d_d_i_t_i_v_e _m_o_d_e_l_s _w_i_t_h _i_n_t_e_g_r_a_t_e_d _s_m_o_o_t_h_n_e_s_s _e_s_t_i_m_a_t_i_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     Fits a generalized additive model (GAM) to data. The degree of
     smoothness of model terms is estimated as part of fitting;
     isotropic or scale invariant smooths of any number of variables
     are available as model terms; confidence/credible intervals are
     readily available for any quantity predicted using a fitted model;
     'gam' is extendable: i.e. users can add smooths. 

     Smooth terms are represented using penalized regression splines
     (or similar smoothers) with smoothing parameters selected by
     GCV/UBRE or by regression splines with fixed degrees of freedom
     (mixtures of the two are permitted). Multi-dimensional smooths are
     available using penalized thin plate regression splines
     (isotropic) or tensor product splines (when an isotropic smooth is
     inappropriate).  For more on specifying models see 'gam.models'.
     For more on model  selection see 'gam.selection'. For faster fits
     use the '"cr"' bases for smooth terms, 'te' smooths for smooths of
     several variables, and performance iteration for smoothing
     parameter estimation (see 'gam.method').  For large datasets see
     warnings.

     'gam()' is not a clone of what S-PLUS provides: the major
     differences are (i) that by default estimation of the degree of
     smoothness of model terms is part of model fitting, (ii) a
     Bayesian approach to variance estimation is employed that makes
     for easier confidence interval calculation (with good coverage
     probabilites) and (iii) the facilities for incorporating smooths
     of more than one variable are different: specifically there are no
     'lo' smooths, but instead (a) 's' terms can have more than one
     argument, implying an isotropic smooth and (b) 'te' smooths are
     provided as an effective means for modelling smooth interactions
     of any number of variables via scale invariant tensor product
     smooths. If you want a clone of what S-PLUS provides use gam from
     package 'gam'.

_U_s_a_g_e:

     gam(formula,family=gaussian(),data=list(),weights=NULL,subset=NULL,
         na.action,offset=NULL,control=gam.control(),method=gam.method(),
         scale=0,knots=NULL,sp=NULL,min.sp=NULL,H=NULL,gamma=1,
         fit=TRUE,G=NULL,in.out,...)

_A_r_g_u_m_e_n_t_s:

 formula: A GAM formula (see also 'gam.models'). This is exactly like
          the formula for a GLM except that smooth terms can be added
          to the right hand side of the formula (and a formula of the
          form 'y ~ .' is not allowed). Smooth terms are specified by
          expressions of the form: 
           's(var1,var2,...,k=12,fx=FALSE,bs="tp",by=a.var)' where
          'var1', 'var2', etc. are the covariates which the smooth is a
          function of and 'k' is the dimension of the basis used to
          represent the smooth term. If 'k' is not specified then
          'k=10*3^(d-1)' is used where 'd' is the number of covariates
          for this term. 'fx' is used to indicate whether or not this
          term has a fixed number of degrees of freedom ('fx=FALSE' to
          select d.f. by GCV/UBRE). 'bs' indicates the basis to use for
          the smooth: for a full list see 's', but note that the
          default '"tp"', while it possesses nice optimality properties
          is slow and memory hungry for very large datasets (but see
          examples for how to get around this). 'by' can be used to
          specify a variable by which the smooth should be multiplied.
          For example 'gam(y~z+s(x,by=z))' would specify a model
          E(y)=f(x)z where f(.) is a smooth function (the formula is
          'y~x+s(x,by=z)' rather than 'y~s(x,by=z)' because the smooths
          are always set up to sum to zero over the covariate values).
          The 'by' option is particularly useful for models in which
          different functions of the same variable are required for
          each level of a factor and for `variable parameter models':
          see 's'. 

          An alternative for specifying smooths of more than one
          covariate is e.g.: 
           'te(x,z,bs=c("tp","tp"),m=c(2,3),k=c(5,10))' which would
          specify a tensor product  smooth of the two covariates 'x'
          and 'z' constructed from marginal t.p.r.s. bases  of
          dimension 5 and 10 with marginal penalties of order 2 and 3.
          Any combination of basis types is  possible, as is any number
          of covariates.

          Formulae can involve nested or ``overlapping'' terms such as 
           'y~s(x)+s(z)+s(x,z)' or 'y~s(x,z)+s(z,v)': see 'gam.side'
          for further details and examples.

  family: This is a family object specifying the distribution and link
          to use in fitting etc. See 'glm' and 'family' for more
          details. The negative binomial families provided by the MASS
          library  can be used, with or without known theta parameter:
          see 'gam.neg.bin' for details. 

    data: A data frame containing the model response variable and 
          covariates required by the formula. By default the variables
          are taken  from 'environment(formula)': typically the
          environment from  which 'gam' is called.

 weights: prior weights on the data.

  subset: an optional vector specifying a subset of observations to be
          used in the fitting process.

na.action: a function which indicates what should happen when the data
          contain `NA's.  The default is set by the `na.action' setting
          of `options', and is `na.fail' if that is unset.  The
          ``factory-fresh'' default is `na.omit'.

  offset: Can be used to supply a model offset for use in fitting. Note
          that this offset will always be completely ignored when
          predicting, unlike an offset  included in 'formula': this
          conforms to the behaviour of 'lm' and 'glm'.

 control: A list of fit control parameters returned by  'gam.control'.

  method: A list controlling the fitting methods used. This can make a
          big difference to computational speed, and, in some cases,
          reliability of convergence: see 'gam.method' for details.

   scale: If this is zero then GCV is used for all distributions except
          Poisson and binomial where UBRE is used with scale parameter
          assumed to be 1. If this is greater than 1 it is assumed to
          be the scale parameter/variance and UBRE is used: to use the
          negative binomial in this case theta must be known. If
          'scale' is negative  GCV  is always used, which means that
          the scale parameter will be estimated by GCV and the Pearson 
          estimator, or in the case of the negative binomial theta will
          be estimated  in order to force the GCV/Pearson scale
          estimate to unity (if this is possible). For binomial models
          in  particular, it is probably worth  comparing UBRE and GCV
          results; for ``over-dispersed Poisson'' GCV is probably more
          appropriate than UBRE.

   knots: this is an optional list containing user specified knot
          values to be used for basis construction.  For the 'cr' and
          'cc' bases the user simply supplies the knots to be used, and
          there must be the same number as the basis dimension, 'k',
          for the smooth concerned. For the 'tp' basis 'knots' has two
          uses. Firstly, for large datasets  the calculation of the
          'tp' basis can be time-consuming. The user can retain most of
          the advantages of the t.p.r.s.  approach by supplying  a
          reduced set of covariate values from which to obtain the
          basis -  typically the number of covariate values used will
          be substantially  smaller than the number of data, and
          substantially larger than the basis dimension, 'k'. The
          second possibility  is to avoid the eigen-decomposition used
          to find the t.p.r.s. basis altogether and simply use  the
          basis implied by the chosen knots: this will happen if the
          number of knots supplied matches the  basis dimension, 'k'.
          For a given basis dimension the second option is  faster, but
          gives poorer results (and the user must be quite careful in
          choosing knot locations).  Different terms can use different 
          numbers of knots, unless they share a covariate. 

      sp: A vector of smoothing parameters for each term can be
          provided here. Smoothing parameters must  be supplied in the
          order that the smooth terms appear in the model  formula.
          With fit method '"magic"' (see 'gam.control'  and 'magic')
          then negative elements indicate that the  parameter should be
          estimated, and hence a mixture of fixed and estimated 
          parameters is possible. With fit method '"mgcv"', if 'sp' is 
          supplied then all its elements must be positive. Note that
          'fx=TRUE'  in a smooth term over-rides what is supplied here
          effectively setting the  smoothing parameter to zero.

  min.sp: for fit method '"magic"' only, lower bounds can be  supplied
          for the smoothing parameters. Note that if this option is
          used then the smoothing parameters 'sp', in the returned
          object, will need to be added to what is supplied here to get
          the actual smoothing parameters. Lower bounds on the
          smoothing  parameters can sometimes help stabilize otherwise
          divergent P-IRLS iterations.

       H: With fit method '"magic"' a user supplied fixed quadratic 
          penalty on the parameters of the  GAM can be supplied, with
          this as its coefficient matrix. A common use of this term is 
          to add a ridge penalty to the parameters of the GAM in
          circumstances in which the model is close to un-identifiable
          on the scale of the linear predictor, but perfectly well
          defined on the response scale.

   gamma: It is sometimes useful to inflate the model degrees of 
          freedom in the GCV or UBRE score by a constant multiplier.
          This allows  such a multiplier to be supplied if fit method
          is '"magic"'.

     fit: If this argument is 'TRUE' then 'gam' sets up the model and
          fits it, but if it is 'FALSE' then the model is set up and an
          object 'G' is returned which is the output from  'gam.setup'
          plus some extra items required to complete the GAM fitting
          process.

       G: Usually 'NULL', but may contain the object returned by a
          previous call to 'gam' with  'fit=FALSE', in which case all
          other arguments are ignored except for 'gamma', 'in.out',
          'control', 'method' and 'fit'.

  in.out: optional list for initializing outer iteration. If supplied
          then this must contain two elements: 'sp' should be an array
          of initialization values for all smoothing parameters (there
          must be a value for all smoothing parameters, whether fixed
          or to be estimated, but those for fixed s.p.s are not used);
          'scale' is the typical scale of the GCV/UBRE function, for
          passing to the outer optimizer.

     ...: further arguments for  passing on e.g. to 'gam.fit'

     (such as 'mustart').

_D_e_t_a_i_l_s:

     A generalized additive model (GAM) is a generalized linear model
     (GLM) in which the linear  predictor is given by a user specified
     sum of smooth functions of the covariates plus a  conventional
     parametric component of the linear predictor. A simple example is:

                   log(E(y_i))=f_1(x_1i)+f_2(x_2i)

     where the (independent) response variables y_i~Poi, and f_1 and
     f_2 are smooth functions of covariates x_1 and  x_2. The log is an
     example of a link function. 

     If absolutely any smooth functions were allowed in model fitting
     then maximum likelihood  estimation of such models would
     invariably result in complex overfitting estimates of  f_1  and
     f_2. For this reason the models are usually fit by  penalized
     likelihood  maximization, in which the model (negative log)
     likelihood is modified by the addition of  a penalty for each
     smooth function, penalizing its `wiggliness'. To control the
     tradeoff  between penalizing wiggliness and penalizing badness of
     fit each penalty is multiplied by  an associated smoothing
     parameter: how to estimate these parameters, and  how to
     practically represent the smooth functions are the main
     statistical questions  introduced by moving from GLMs to GAMs. 

     The 'mgcv' implementation of 'gam' represents the smooth functions
     using  penalized regression splines, and by default uses basis
     functions for these splines that  are designed to be optimal,
     given the number basis functions used. The smooth terms can be 
     functions of any number of covariates and the user has some
     control over how smoothness of  the functions is measured. 

     'gam' in 'mgcv' solves the smoothing parameter estimation problem
     by using the  Generalized Cross Validation (GCV) criterion

                           n D/(n - DoF)^2
/www.mata. By default the variables
          are taken  from 'environment(formula)': typically the
          environment from  which 'gam' is called.

 weights: prior weights on the data.

  subset: an optional vector specifying a subset of observations to be
          used in the fitting process.

na.action: a function which indicates what should happen when the data
          contain `NA's.  The default is set by the `na.action' setting
          of `options', and is `na.fail' if that is unset.  The
          ``factory-fresh'' default is `na.omit'.

  offset: Can be used to supply a model offset for use in fitting. Note
          that this offset will always be completely ignored when
          predicting, unlike an offset  included in 'formula': this
          conforms to the behaviour of 'lm' and 'glm'.

 control: A list of fit control parameters returned by  'gam.control'.

  method: A list controlling the fitting methods used. This can make a
          big difference to computational speed, and, in some cases,
          reliability of convergence: see 'gam.method' for details.

   scale: If this is zero then GCV is used for all distributions except
          Poisson and binomial where UBRE is used with scale parameter
          assumed to be 1. If this is greater than 1 it is assumed to
          be the scale parameter/variance and UBRE is used: to use the
          negative binomial in this case theta must be known. If
          'scale' is negative  GCV  is always used, which means that
          the scale parameter will be estimated by GCV and the Pearson 
          estimator, or in the case of the negative binomial theta will
          be estimated  in order to force the GCV/Pearson scale
          estimate to unity (if this is possible). For binomial models
          in  particular, it is probably worth  comparing UBRE and GCV
          results; for ``over-dispersed Poisson'' GCV is probably more
          appropriate than UBRE.

   knots: this is an optional list containing user specified knot
          values to be used for basis construction.  For the 'cr' and
          'cc' bases the user simply supplies the knots to be used, and
          there must be the same number as the basis dimension, 'k',
          for the smooth concerned. For the 'tp' basis 'knots' has two
          uses. Firstly, for large datasets  the calculation of the
          'tp' basis can be time-consuming. The user can retain most of
          the advantages of the t.p.r.s.  approach by supplying  a
          reduced set of covariate values from which to obtain the
          basis -  typically the number of covariate values used will
          be substantially  smaller than the number of data, and
          substantially larger than the basis dimension, 'k'. The
          second possibility  is to avoid the eigen-decomposition used
          to find the t.p.r.s. basis altogether and simply use  the
          basis implied by the chosen knots: this will happen if the
          number of knots supplied matches the  basis dimension, 'k'.
          For a given basis dimension the second option is  faster, but
          gives poorer results (and the user must be quite careful in
          choosing knot locations).  Different terms can use different 
          numbers of knots, unless they share a covariate. 

      sp: A vector of smoothing parameters for each term can be
          provided here. Smoothing parameters must  be supplied in the
          order that the smooth terms appear in the model  formula.
          With fit method '"magic"' (see 'gam.control'  and 'magic')
          then negative elements indicate that the  parameter should be
          estimated, and hence a mixture of fixed and estimated 
          parameters is possible. With fit method '"mgcv"', if 'sp' is 
          supplied then all its elements must be positive. Note that
          'fx=TRUE'  in a smooth term over-rides what is supplied here
          effectively setting the  smoothing parameter to zero.

  min.sp: for fit method '"magic"' only, lower bounds can be  supplied
          for the smoothing parameters. Note that if this option is
          used then the smoothing parameters 'sp', in the returned
          object, will need to be added to what is supplied here to get
          the actual smoothing parameters. Lower bounds on the
          smoothing  parameters can sometimes help stabilize otherwise
          divergent P-IRLS iterations.

       H: With fit method '"magic"' a user supplied fixed quadratic 
          penalty on the parameters of the  GAM can be supplied, with
          this as its coefficient matrix. A common use of this term is 
          to add a ridge penalty to the parameters of the GAM in
          circumstances in which the model is close to un-identifiable
          on the scale of the linear predictor, but perfectly well
          defined on the response scale.

   gamma: It is sometimes useful to inflate the model degrees of 
          freedom in the GCV or UBRE score by a constant multiplier.
          This allows  such a multiplier to be supplied if fit method
          is '"magic"'.

     fit: If this argument is 'TRUE' then 'gam' sets up the model and
          fits it, but if it is 'FALSE' then the model is set up and an
          object 'G' is returned which is the output from  'gam.setup'
          plus some extra items required to complete the GAM fitting
          process.

       G: Usually 'NULL', but may contain the object returned by a
          previous call to 'gam' with  'fit=FALSE', in which case all
          other arguments are ignored except for 'gamma', 'in.out',
          'control', 'method' and 'fit'.

  in.out: optional list for initializing outer iteration. If supplied
          then this must contain two elements: 'sp' should be an array
          of initialization values for all smoothing parameters (there
          must be a value for all smoothing parameters, whether fixed
          or to be estimated, but those for fixed s.p.s are not used);
          'scale' is the typical scale of the GCV/UBRE function, for
          passing to the outer optimizer.

     ...: further arguments for  passing on e.g. to 'gam.fit'

     (such as 'mustart').

_D_e_t_a_i_l_s:

     A generalized additive model (GAM) is a generalized linear model
     (GLM) in which the linear  predictor is given by a user specified
     sum of smooth functions of the covariates plus a  conventional
     parametric component of the linear predictor. A simple example is:

                   log(E(y_i))=f_1(x_1i)+f_2(x_2i)

     where the (independent) response variables y_i~Poi, and f_1 and
     f_2 are smooth functions of covariates x_1 and  x_2. The log is an
     example of a link function. 

     If absolutely any smooth functions were allowed in model fitting
     then maximum likelihood  estimation of such models would
     invariably result in complex overfitting estimates of  f_1  and
     f_2. For this reason the models are usually fit by  penalized
     likelihood  maximization, in which the model (negative log)
     likelihood is modified by the addition of  a penalty for each
     smooth function, penalizing its `wiggliness'. To control the
     tradeoff  between penalizing wiggliness and penalizing badness of
     fit each penalty is multiplied by  an associated smoothing
     parameter: how to estimate these parameters, and  how to
     practically represent the smooth functions are the main
     statistical questions  introduced by moving from GLMs to GAMs. 

     The 'mgcv' implementation of 'gam' represents the smooth functions
     using  penalized regression splines, and by default uses basis
     functions for these splines that  are designed to be optimal,
     given the number basis functions used. The smooth terms can be 
     functions of any number of covariates and the user has some
     control over how smoothness of  the functions is measured. 

     'gam' in 'mgcv' solves the smoothing parameter estimation problem
     by using the  Generalized Cross Validation (GCV) criterion

                           n D/(n - DoF)^2
/www.mata. By default the variables
          are taken  from 'environment(formula)': typically the
          environment from  which 'gam' is called.

 weights: prior weights on the data.

  subset: an optional vector specifying a subset of observations to be
          used in the fitting process.

na.action: a function which indicates what should happen when the data
          contain `NA's.  The default is set by the `na.action' setting
          of `options', and is `na.fail' if that is unset.  The
          ``factory-fresh'' default is `na.omit'.

  offset: Can be used to supply a model offset for use in fitting. Note
          that this offset will always be completely ignored when
          predicting, unlike an offset  included in 'formula': this
          conforms to the behaviour of 'lm' and 'glm'.

 control: A list of fit control parameters returned by  'gam.control'.

  method: A list controlling the fitting methods used. This can make a
          big difference to computational speed, and, in some cases,
          reliability of convergence: see 'gam.method' for details.

   scale: If this is zero then GCV is used for all distributions except
          Poisson and binomial where UBRE is used with scale parameter
          assumed to be 1. If this is greater than 1 it is assumed to
          be the scale parameter/variance and UBRE is used: to use the
          negative binomial in this case theta must be known. If
          'scale' is negative  GCV  is always used, which means that
          the scale parameter will be estimated by GCV and the Pearson 
          estimator, or in the case of the negative binomial theta will
          be estimated  in order to force the GCV/Pearson scale
          estimate to unity (if this is possible). For binomial models
          in  particular, it is probably worth  comparing UBRE and GCV
          results; for ``over-dispersed Poisson'' GCV is probably more
          appropriate than UBRE.

   knots: this is an optional list containing user specified knot
          values to be used for basis construction.  For the 'cr' and
          'cc' bases the user simply supplies the knots to be used, and
          there must be the same number as the basis dimension, 'k',
          for the smooth concerned. For the 'tp' basis 'knots' has two
          uses. Firstly, for large datasets  the calculation of the
          'tp' basis can be time-consuming. The user can retain most of
          the advantages of the t.p.r.s.  approach by supplying  a
          reduced set of covariate values from which to obtain the
          basis -  typically the number of covariate values used will
          be substantially  smaller than the number of data, and
          substantially larger than the basis dimension, 'k'. The
          second possibility  is to avoid the eigen-decomposition used
          to find the t.p.r.s. basis altogether and simply use  the
          basis implied by the chosen knots: this will happen if the
          number of knots supplied matches the  basis dimension, 'k'.
          For a given basis dimension the second option is  faster, but
          gives poorer results (and the user must be quite careful in
          choosing knot locations).  Different terms can use different 
          numbers of knots, unless they share a covariate. 

      sp: A vector of smoothing parameters for each term can be
          provided here. Smoothing parameters must  be supplied in the
          order that the smooth terms appear in the model  formula.
          With fit method '"magic"' (see 'gam.control'  and 'magic')
          then negative elements indicate that the  parameter should be
          estimated, and hence a mixture of fixed and estimated 
          parameters is possible. With fit method '"mgcv"', if 'sp' is 
          supplied then all its elements must be positive. Note that
          'fx=TRUE'  in a smooth term over-rides what is supplied here
          effectively setting the  smoothing parameter to zero.

  min.sp: for fit method '"magic"' only, lower bounds can be  supplied
          for the smoothing parameters. Note that if this option is
          used then the smoothing parameters 'sp', in the returned
          object, will need to be added to what is supplied here to get
          the actual smoothing parameters. Lower bounds on the
          smoothing  parameters can sometimes help stabilize otherwise
          divergent P-IRLS iterations.

       H: With fit method '"magic"' a user supplied fixed quadratic 
          penalty on the parameters of the  GAM can be supplied, with
          this as its coefficient matrix. A common use of this term is 
          to add a ridge penalty to the parameters of the GAM in
          circumstances in which the model is close to un-identifiable
          on the scale of the linear predictor, but perfectly well
          defined on the response scale.

   gamma: It is sometimes useful to inflate the model degrees of 
          freedom in the GCV or UBRE score by a constant multiplier.
          This allows  such a multiplier to be supplied if fit method
          is '"magic"'.

     fit: If this argument is 'TRUE' then 'gam' sets up the model and
          fits it, but if it is 'FALSE' then the model is set up and an
          object 'G' is returned which is the output from  'gam.setup'
          plus some extra items required to complete the GAM fitting
          process.

       G: Usually 'NULL', but may contain the object returned by a
          previous call to 'gam' with  'fit=FALSE', in which case all
          other arguments