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

Andrew Crane-Droesch andrewcd at gmail.com
Mon Dec 3 06:01:23 CET 2012

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.

#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))

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))


correc = matrix(5,3,3)#5 is totally arbitrary, standing in for a proper 
MI correction

par(mfrow = c(1,2))
dumb.example2$coeff = averaged.models
dumb.example2$Vp = changed.vcv

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:
> 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:
>> Dear List,
>> I'm using GAMs in a multiple imputation project, and I want to be able
>> to combine the parameter estimates and covariance matrices from each
>> completed dataset's fitted model in the end.  In order to do this, I
>> need the knots to be uniform for each model with partially-imputed
>> data.  I want to specify these knots based on the quantiles of the
>> unique values of the non-missing original data, ignoring the NA's.  When
>> I fit the GAM with the imputed data included, I don't want mgcv to use
>> the data that it is supplied to figure out the knots, because this will
>> lead to un-comparable results when the many fitted models are combined.
>> Here is a caricatured example of what I want to do:
>> #Random data
>> x = runif(1000,0,1)
>> y = (log(x^2)+x^3)/sin(x)
>> example = gam(y~s(x))
>> plot(example)
>> #But I want to define my own knots
>> dumb.knots = c(.7,.8,.9)
>> dumb.example = gam(y~s(x,k=3),knots=list(dumb.knots))
>> plot(dumb.example)
>> dumb.example2 = gam(y~s(x,k=3))
>> plot(dumb.example2)
>> Dumb example 1 is the same as dumb example 2, but it shouldn't be.
>> Once I figure out how to do this, I'll take the fitted coefficients from
>> each model and average them, then take the vcv's from each model and
>> average them, and add a correction to account for within and between
>> imputation variability, then plug them into a gamObject$coeffient and
>> gamObject$Vp matrix, plot/summarize, and have my result. Comments
>> welcome on whether or not this would be somehow incorrect would be
>> welcome as well.  Still have a lot to learn!
>> Thanks,
>> Andrew
>>     [[alternative HTML version deleted]]
>> ______________________________________________
>> 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.

More information about the R-help mailing list