# Fit a Thin Plate Regression Spline

Thin plate regression splines (TPRS) are the default smooth basis in Whittaker. They minimise a penalty on integrated squared second derivatives, producing a smooth curve that adapts to the data without requiring you to place knots manually. Use `s(x)` or `s(x, bs='tp')` as both are identical. TPRS works well as a general-purpose smooth for any continuous predictor.


# Load data

Load the `mcycle` dataset, which records head acceleration during a simulated motorcycle crash. It is a classic benchmark for smoothing because the signal has strong curvature.


``` python
import whittaker as wk

data = wk.load_dataset("mcycle", as_frame=True)
data.head()
```


|     | times | accel      |
|-----|-------|------------|
| 0   | 0.0   | -11.075042 |
| 1   | 0.2   | 2.887586   |
| 2   | 0.4   | 3.909896   |
| 3   | 0.6   | 14.610615  |
| 4   | 2.2   | -0.172051  |


The two columns are `times` (milliseconds after impact) and `accel` (head acceleration in g).


# Fit

Fit a Gaussian GAM with a TPRS smooth on `times`. The `bs='tp'` argument is shown explicitly here but is the default (both formulas produce the same model).


``` python
# Fit TPRS smooth on times
model = wk.GAM("accel ~ s(times, bs='tp')").fit(data)

model.summary()
```


    GAM fit summary
    ============================================================
    Formula:    accel ~ s(times)
    Family:     Gaussian(link='identity')
    Observations: 133
    Coefficients: 10

    Parametric coefficients:
      Term                       Estimate    Std.Err    t value    p-value
      ------------------------ ---------- ---------- ---------- ----------
      (Intercept)                -45.6924     1.8364    -24.882    < 1e-16

    Approximate significance of smooth terms:
      Term                        EDF Ref.df     Chi.sq    p-value
      ------------------------ ------ ------ ---------- ----------
      s(times)                   7.92      8    663.396    < 1e-16

    Total EDF:  8.92
    Deviance:   55653.7152
    Null dev:   357878.4929
    Dev. expl:  84.4%
    GCV score:  480.746119
    Scale est:  448.517253
    AIC:        1198.44
    BIC:        1224.22


Check the estimated degrees of freedom (EDF) for the smooth. An EDF near 1 would indicate a nearly linear effect; a higher EDF reflects more curvature. The k-index should be above 1 and the p-value for the smooth should be small, confirming that the non-linear term is necessary.


# Partial effects

Plot the fitted smooth to see the shape of the relationship.


``` python
wk.partial_effects(model)
```


<style>
  #altair-viz-4dedc989a4444719b7b2c6f0de0e65e6.vega-embed {
    width: 100%;
    display: flex;
  }

  #altair-viz-4dedc989a4444719b7b2c6f0de0e65e6.vega-embed details,
  #altair-viz-4dedc989a4444719b7b2c6f0de0e65e6.vega-embed details summary {
    position: relative;
  }
</style>


The partial effects plot shows the TPRS smooth with a pointwise confidence band. Notice how the curve captures the sharp dip around 15-20 ms and the rebound afterward (a pattern that a straight line would completely miss).
