# [R] gamm (mgvc) and time-varying coefficient model

Simon Wood s.wood at bath.ac.uk
Mon Mar 2 14:43:54 CET 2009

```Looks correct to me. You get a smooth of time for each level of "x", so the
smooths describe the way in which each coefficient of x varies in time.

Note that the time varying coefficient for some level of a factor is actually
given by the sum of the smooth for that factor level, and the estimate of the
coefficient for that factor level from the parametric part of the model. For
this reason it might actually be slightly better to use model formula:

y ~  factor(x) + s(time, by=factor(x)) - 1

which will ensure that the coefficients for factor(x) are directly
interpretable as the `average' values of the coefficents for each factor
level.

best,
Simon

On Sunday 01 March 2009 23:52, Marie-Pierre Sylvestre wrote:
> Dear R users,
>
> I have repeated measurements on individuals. I want to estimate the
> time-varying effect of a factor variable X (taking three levels), e.g. a
> model in the spirit of Hastie and Tibshirani (1993).
>
> I am considering using the package "mgvc" which implements generalized
> additive models, especially the function gamm, which estimates
> generalized additive mixed models, and thus, can deal with the
> correlated repeated measures within individuals.
>
> However, I am confused as to how to specify the time-varying coefficient
> part of the formula. According to the mgvc documentation (p. 35):
> "by variables are the means for constructing 'varying-coefficient
> models' (geographic regression models) and for letting smooths
> 'interact' with factors or parametric terms."
>
> Suppose that "y" is the response variable, "id" identifies individuals,
> "x" is the three-level factor variable and "time" indexes the chronology
> of responses.
>
> Is this model estimating the time-varying coefficient of x? If it is
> not, how should I specify the model?
>
> mod <- gamm( y ~, data=mydata,
> random=list(patient=~ 1), correlation = corAR1())
>
> Best,
> MP
>
>
>
>
> 	[[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 Sciences, University of Bath, Bath, BA2 7AY UK
> +44 1225 386603  www.maths.bath.ac.uk/~sw283

```