[R] I want to get smoothed splines by using the class gam

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Dec 8 19:04:00 CET 2010


On Wed, 2010-12-08 at 08:48 -0800, anna_m wrote:
> Hi all,
> I try to interpolate a data set in the form:
> 
> time	Erg
> 0.000000	48.650000
> 1.500000	56.080000
> 3.000000	38.330000
> 4.500000	49.650000
> 6.000000	61.390000
> 7.500000	51.250000
> 9.000000	50.450000
> 10.500000	55.110000
> 12.000000	61.120000
> 18.000000	61.260000
> 24.000000	62.670000
> 36.000000	63.670000
> 48.000000	74.880000
> 
> I want to get smoothed splines by using the class gam

It will be a cubic regression spline --- if that makes a difference?

con <- textConnection("time    Erg
0.000000        48.650000
1.500000        56.080000
3.000000        38.330000
4.500000        49.650000
6.000000        61.390000
7.500000        51.250000
9.000000        50.450000
10.500000       55.110000
12.000000       61.120000
18.000000       61.260000
24.000000       62.670000
36.000000       63.670000
48.000000       74.880000")

dat <- read.table(con, header = TRUE)
close(con)

require(mgcv)
g <- gam(Erg ~ s(time, k=4, fx = TRUE, bs = "cr"), data = dat)
plot(g)

> The first way I tried , was the following:
> 
> > library('mgcv')
> 
> > t=read.table('Valuedata', header=T, sep="\t")
> > g=gam(Erg~s(time,k=4,fx=TRUE,bs="cr"),data=t)
> > plot(g)
>  
> 
> The plot shows me 3 curves in the yrange of -20 and 30 and I don't
> know, which curve is the result.

The solid one. The dashed ones are the 95% credible intervals on the
fitted smooth, *minus* the uncertainty in the mean (Intercept) term.

> The second way I tried was:
> 
> > library("gam")
> > t=read.table('Valuedata', header=T, sep="\t")
> > g=gam(Erg~s(time,3), data=t)
> > plot(g)
> Error in eval(predvars, data, env) : invalid 'envir' argument
> 
> This way I can't see the result and I so I can't see the curve.
> 
> My first question:
> 
> How can I get the function to calculate the according splines, that are
> determined by gam.
> I think about a function like:
> f(x)=a0+a1+x+a2*x^2+a3*x^3 +smooth(x)
> a polynome of rank k, that I can use in an other context. I can find the 
> fitted.values, the coefficients and the smooth but I can't find the exact
> description to calculate the according values.
> I want to calculate the interpolations spline in the interval [0,48]
> and use maybe 100 - 1000 supporting points.
> Or exists a function in R, that gives me these values?

This will interpolate the points

foo <- with(dat, splinefun(x = time, y = Erg))

plot(Erg ~ time, data = dat)
pts <- seq(0, 48, length = 200)
lines(pts, foo(pts), col = "red", lwd = 2)

If you want the predicted values from the gam, we can do this

pdat <- data.frame(time = pts)
pred <- predict(g, newdata = pdat)
lines(pts, pred, col = "blue", lwd = 2)

We can fit a cubic smoothing spline using `smooth.spline()`:

g2 <- with(dat, smooth.spline(x = time, y = Erg, df = 43))
pg2 <- predict(g2, pts)
lines(pg2, col = "orange", lwd = 2)

> My second question:
> I can't find a reason for the error message above.

It might be because you called your data `t` which is the name of a
function. I don't know why and as I don;t have your data, it is too much
effort to debug.

> I have no experience with R, but I program in other languages.
> 
> 
> Thanks a lot to everybody who will help me - anna_m!

HTH

G

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