[R] Use generalised additive model to plot curve

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Dec 14 23:50:10 CET 2010


On Tue, 2010-12-14 at 22:36 +0000, e-letter wrote:
> Readers,
> 
> I have been reading 'the r book' by Crawley and think that the
> generalised additive model is appropriate for this problem. The
> package 'gam' was installed using the command (as root)
> 
> install.package("gam")
> ...
> library(gam)
> 
> > library(gam)
> Loading required package: splines
> Loading required package: akima
> > library(mgcv)
> This is mgcv 1.3-25

Why do you want to use both gam:::gam and mgcv:::gam at the same time?
Use one or the other package; not both.

> Attaching package: 'mgcv'
> 
> 
>         The following object(s) are masked from package:gam :
> 
>          gam,
>          gam.control,
>          gam.fit,
>          plot.gam,
>          predict.gam,
>          s,
>          summary.gam

These warnings/messages are because you loaded two packages that both
provide a gam() and other similarly named functions.

> > x<-c(30,50,80,90,100)
> > y<-c(160,180,250,450,300)
> > model<-gam(y~s(x))
> Error in smooth.construct.tp.smooth.spec(object, data, knots) :
>         A term has fewer unique covariate combinations than specified
> maximum degrees of freedom

You are trying to fit a thin-plate spline that is of greater complexity
than the available data. You need to reduce the complexity of the fitted
spline whilst fitting the model. For these data the maximum complexity
is 5:

> mod <- gam(y ~ s(x, k = 5))

HTH

G

> The objective is to plot y against x, finally to produce a graph with
> a smooth curve (and then remove the data points). What is my mistake
> please?
> 
> yours,
> 
> r251
> gnu/linux mandriva2008
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 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