<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Specify a Smoothing Spline Fit in a GAM Formula</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="https://rpkg.net/assets/examples.css" />
</head><body>

<table width="100%" summary="page for gam.s"><tr><td>gam.s</td><td style="text-align: right;">R Documentation</td></tr></table>

<h2>Specify a Smoothing Spline Fit in a GAM Formula</h2>

<h3>Description</h3>

<p>A symbolic wrapper to indicate a smooth term in a formala argument to gam
</p>


<h3>Usage</h3>

<pre>
gam.s(x, y, w = rep(1, length(x)), df = 4, spar = 1, xeval)

s(x, df = 4, spar = 1)
</pre>


<h3>Arguments</h3>

<table summary="R argblock">
<tr valign="top"><td><code>x</code></td>
<td>
<p>the univariate predictor, or expression, that evaluates to a
numeric vector.</p>
</td></tr>
<tr valign="top"><td><code>y</code></td>
<td>
<p>a response variable passed to <code>gam.s</code> during backfitting</p>
</td></tr>
<tr valign="top"><td><code>w</code></td>
<td>
<p>weights</p>
</td></tr>
<tr valign="top"><td><code>df</code></td>
<td>
<p>the target equivalent degrees of freedom, used as a smoothing
parameter. The real smoothing parameter (<code>spar</code> below) is found such
that <code>df=tr(S)-1</code>, where <code>S</code> is the implicit smoother matrix.
Values for <code>df</code> should be greater than <code>1</code>, with <code>df=1</code>
implying a linear fit. If both <code>df</code> and <code>spar</code> are supplied, the
former takes precedence. Note that <code>df</code> is not necessarily an integer.</p>
</td></tr>
<tr valign="top"><td><code>spar</code></td>
<td>
<p>can be used as smoothing parameter, with values typically in
<code>(0,1]</code>. See <code>smooth.spline</code> for more details.</p>
</td></tr>
<tr valign="top"><td><code>xeval</code></td>
<td>
<p>If this argument is present, then <code>gam.s</code> produces a
prediction at <code>xeval</code>.</p>
</td></tr>
</table>


<h3>Value</h3>

<p><code>s</code> returns the vector <code>x</code>, endowed with a number of attributes.
The vector itself is used in the construction of the model matrix, while the
attributes are needed for the backfitting algorithms <code>general.wam</code>
(weighted additive model) or <code>s.wam</code>. Since smoothing splines
reproduces linear fits, the linear part will be efficiently computed with
the other parametric linear parts of the model.
</p>
<p>Note that <code>s</code> itself does no smoothing; it simply sets things up for
<code>gam</code>.
</p>
<p>One important attribute is named <code>call</code>. For example, <code>s(x)</code> has a
call component <code>gam.s(data[["s(x)"]], z, w, spar = 1, df = 4)</code>. This is
an expression that gets evaluated repeatedly in <code>general.wam</code> (the
backfitting algorithm).
</p>
<p><code>gam.s</code> returns an object with components </p>
<table summary="R valueblock">
<tr valign="top"><td><code>residuals</code></td>
<td>
<p>The
residuals from the smooth fit. Note that the smoother removes the parametric
part of the fit (using a linear fit in <code>x</code>), so these residual
represent the nonlinear part of the fit.</p>
</td></tr> <tr valign="top"><td><code>nl.df</code></td>
<td>
<p>the nonlinear degrees
of freedom</p>
</td></tr> <tr valign="top"><td><code>var</code></td>
<td>
<p>the pointwise variance for the nonlinear fit</p>
</td></tr>
</table>
<p>When <code>gam.s</code> is evaluated with an <code>xeval</code> argument, it returns a
vector of predictions.
</p>


<h3>Author(s)</h3>

<p>Written by Trevor Hastie, following closely the design in the
&quot;Generalized Additive Models&quot; chapter (Hastie, 1992) in Chambers and Hastie
(1992).
</p>


<h3>References</h3>

<p>Hastie, T. J. (1992) <em>Generalized additive models.</em> Chapter
7 of <em>Statistical Models in S</em> eds J. M. Chambers and T. J. Hastie,
Wadsworth &amp; Brooks/Cole.
</p>
<p>Hastie, T. and Tibshirani, R. (1990) <em>Generalized Additive Models.</em>
London: Chapman and Hall.
</p>
<p>Cantoni, E. and hastie, T. (2002) Degrees-of-freedom tests for smoothing
splines, <em>Biometrika</em> 89(2), 251-263
</p>


<h3>See Also</h3>

<p><code>lo</code>, <code>smooth.spline</code>, <code>bs</code>,
<code>ns</code>, <code>poly</code>
</p>


<h3>Examples</h3>

<pre>

     # fit Start using a smoothing spline with 4 df.
     y ~ Age + s(Start, 4)
     # fit log(Start) using a smoothing spline with 5 df.
     y ~ Age + s(log(Start), df=5)
 
</pre>


</body></html>
