With distinct knots , a natural cubic spline is a function on the real line, cubic on each interval between consecutive knots and affine outside the two outer knots. The latter condition gives .
There are coefficients for the interior cubic pieces. Matching values and the first two derivatives at the interior knots imposes independent constraints, and the two natural endpoint conditions impose two more. The affine continuations are then determined by endpoint values and slopes. The dimension of a vector space is thereforeOne explicit basis uses and for , whereThe cubic and quadratic terms cancel in each difference above , making it affine there; below the truncated powers vanish. The changes in third derivative at individual knots show independence of these basis functions. This count uses the linear independence of the spline constraints and counts the listed outer knots as boundary knots. If a convention instead lists interior knots and two additional boundaries, the dimension is . With only one listed knot and linearity on both sides, the condition forces a globally affine function, so the exceptional dimension is two.
Choose knots independently of the eventual validation responses, and let be a natural cubic spline basis as in part (a). A regression spline writes . Define the design matrix . Under the stated normal linear model, maximum likelihood estimation for is the same as ordinary least squares:Differentiation gives the normal equations . If the design has full column rank,If it does not, a Moore-Penrose inverse gives the fitted values, but not every coefficient or off-sample prediction is identifiable. Merely choosing distinct knots does not guarantee full rank for an arbitrary set of observed predictor values.
More knots allow greater curvature and reduce approximation bias at the cost of larger estimation variance. Too many or poorly located knots can cause unstable fits. Equally spaced knots are simple; empirical predictor quantiles put more flexibility where observations are dense; subject knowledge can concentrate knots near likely changes. Both the number and positions can be compared using cross-validation or an appropriate penalized model-selection criterion. Adaptive knot selection must be included within validation, since treating a data-selected basis as prespecified understates its flexibility. Boundary knots should cover the region where inference is intended, and natural linear tails do not by themselves justify far-out extrapolation.
For , minimize the smoothing spline objective over the Sobolev space of functions with square-integrable second derivative on the observed interval:The roughness penalty measures curvature; its null space consists of affine functions. To see why the minimizer is a natural cubic spline, let be the natural spline interpolating the values of any competitor at its distinct observation locations, and put . Integration by parts twice on each knot interval gives : on each interval, vanishes at the knots, is continuous, and vanishes at the outer endpoints. Hencewhile both functions have the same residual sum of squares. This proves the reduction to a finite-dimensional spline space.
For distinct and , use its -dimensional basis, write , and define the positive semidefinite matrixThe penalized least squares problem is , soUse linear continuation outside the observation interval. The fitted-value matrix is ; its trace gives the effective degrees of freedom. As decreases to zero, the natural spline approaches interpolation, with trace ; as it grows without bound, the fit approaches ordinary least squares on , with trace two. For the criterion alone does not determine values between observations; the natural interpolant is the limiting convention. Repeated observation locations are combined by using distinct knots and their multiplicities as weights, rather than pretending the spline design has independent interpolation values.
Because no family is specified, the command fits a Gaussian identity-link generalized additive model:The are centered penalized regression splines with cubic regression bases; centering separates them from the intercept. The age curve in the original figure falls to a minimum near age 25, then rises markedly, particularly from about 40 onwards. Its effective degrees of freedom are , reflecting curvature. The white-cell curve has effective degrees of freedom one and is nearly flat with a slight positive slope; the pointwise uncertainty bands are consistent with no substantial effect. These plots display centered partial mean contributions, not numbers of prescriptions by themselves.
A simpler mean model keeps the age regression spline and replaces the white-cell smooth by a linear term. A separate issue is that the response is a nonnegative count: a Poisson regression with logarithmic link function respects that support and positivity of the mean. A reasonable candidate isIf diagnostics show overdispersion, one can fitusing a negative binomial regression. Alternatively, if a Gaussian approximation is adequate, the minimal simplification is
model1p <- gam(npres ~ s(age, bs="cr") + wbc, family=poisson(link="log"))
gam.check(model1p)model1nb <- gam(npres ~ s(age, bs="cr") + wbc, family=nb(link="log"))gam(npres ~ s(age, bs="cr") + wbc). These are candidates to compare by diagnostics and validation, rather than guaranteed improvements from the partial plots alone. Retain the curved age effect; consider a linear or removable white-cell effect, and assess an appropriate count family. Refit before interpreting the old curves on a new link scale.There is a genuine syntax error in the printed command: With the default Gaussian family, this generalized additive mixed model isHere across doctors, is an estimated covariance matrix, and the Gaussian errors have variance and are independent of the random effects. The formula
random=list(...) must be a separate argument, following a comma rather than a plus. The intended call ismodel2 <- gamm(npres ~ s(age, bs="cr") + s(wbc, bs="cr"), random=list(doctor=~age))~age includes a random intercept as well as a random slope; it does not force these two effects to be uncorrelated.The random intercept allows different prescribing baselines, and the random slope allows doctor-specific linear modifications to the population age curve. Conditional on these effects the observations are independent under the model, whereas two patients of the same doctor have additional covariance . This accounts for clustered data and avoids treating doctor variation as independent patient-level noise. The population smooth and doctor-specific predictions are different targets. With only four doctors, estimates of the random-effect covariance can be imprecise; the Gaussian support issue for counts also remains. The new model accounts for doctor-specific baselines and age slopes.
Articles by others on the same topic
There are currently no matching articles.