Skip to content

[mgcv][gam] Manually defining my own knots?

3 messages · Andrew Crane-Droesch, Simon Wood

#
Hi Andrew,

mgcv matches the knots to the smooth arguments by name. If an element of 'knots' has
no name it will be ignored. The following will do what you want...

dumb.example = gam(y~s(x,k=3),knots=list(x=dumb.knots))

best,
Simon
On 29/11/12 23:44, Andrew Crane-Droesch wrote:
#
Hi Simon,

Thanks for your help.  I've got another question if you don't mind -- is 
it possible to "swap out" a set of coefficients of a gamObject in order 
to change the results when that gamObject is plotted?  The (silly) 
example below illustrates that this is possible with the Vp matrix.  But 
it is not working for me as I'd like it to for the coefficients.

library(mgcv)
#Random data
x = runif(1000,0,1)
y = (log(x^2)+x^3)/sin(x)
dumb.knots = c(.1,.2,.3)
dumb.example1 = gam(y~s(x,k=3),knots=list(x=dumb.knots))
plot(dumb.example1)

x = runif(1000,0,1)
y = (log(x^2)+x^3)/sin(x)
dumb.knots = c(.1,.2,.3)
dumb.example2 = gam(y~s(x,k=3),knots=list(x=dumb.knots))
plot(dumb.example2)

cbind(dumb.example1$coeff,dumb.example2$coeff)

averaged.models=(dumb.example1$coeff+dumb.example2$coeff)/2
correc = matrix(5,3,3)#5 is totally arbitrary, standing in for a proper 
MI correction
changed.vcv=correc+(dumb.example1$Vp+dumb.example2$Vp)/2

par(mfrow = c(1,2))
plot(dumb.example2,ylim=c(-500,200))
dumb.example2$coeff = averaged.models
dumb.example2$Vp = changed.vcv
plot(dumb.example2,ylim=c(-500,200))

The confidence bands expand but the location of the fit doesn't change!  
What part of the gamObject controls the plot of the smooth?
On 12/02/2012 02:15 AM, Simon Wood wrote:
#
Andrew,

I think you mean....

dumb.example2$coefficients = averaged.models
                    ~~~~~~~

currently your code adds a new vector 'coeff' to the gam object, rather 
than modifying 'coefficients'. (A good example of why forgiving 
languages like R are dangerous, and you should really write everything 
in something utterly unforgiving like C - only kidding).

Simon
On 03/12/12 05:01, Andrew Crane-Droesch wrote: