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

Simon Wood s.wood at bath.ac.uk
Mon Dec 3 11:59:22 CET 2012


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:
> 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:
>> 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.
>>
>>
>


-- 
Simon Wood, Mathematical Science, University of Bath BA2 7AY UK
+44 (0)1225 386603               http://people.bath.ac.uk/sw283




More information about the R-help mailing list