Learn R Programming

Epi (version 1.1.7)

Ns: Natural splines - (cubic splines linear beyond outermost knots) with convenient specification of knots and possibility of centering and detrending.

Description

This function is partly for convenient specification of natural splines in practical modelling. The convention used is to take the smallest and the largest of the supplied knots as boundary knots. It also has the option of centering the effects provided at a chosen reference point as well as projecting the columns on the orthogonal space to that spanned by the intercept and the linear effect of the variable.

Usage

Ns( x, ref = NULL, df = NULL,
                knots = NULL,
            intercept = FALSE,
       Boundary.knots = NULL,
              detrend = FALSE )

Arguments

x
A variable.
ref
Scalar. Reference point on the x-scale, where the resuting effect will be 0.
df
degrees of freedom.
knots
knots to be used both as boundary and internal knots. If Boundary.knots are given, this will be taken as the set of internal knots.
intercept
Should the intercept be included in the resulting basis? Ignored if any of ref or detrend is given.
Boundary.knots
The boundary knots beyond which the spline is linear.
detrend
If TRUE, the columns of the spline basis will be projected to the orthogonal of cbind(1,x). Optionally detrend can be given as a vector of non-negative numbers used to define an inner product as

Value

  • A matrix of dimension c(length(x),df) where either df was supplied or if knots were supplied, df = length(knots) - intercept. Ns returns a spline basis which is centered at ref, without the ref= argument Ns behaves as Ns, and so the two will likely be merged. Ns returns a spline basis which is orthogonal to cbind(1,x) with respect to the inner product defined by the positive definite matrix diag(weight) (an assumption which is checked).

Examples

Run this code
require(splines)
require(stats)
require(graphics)

ns( women$height, df = 3)
Ns( women$height, knots=c(63,59,71,67) )

# Gives the same results as ns:
summary( lm(weight ~ ns(height, df = 3), data = women) )
summary( lm(weight ~ Ns(height, df = 3), data = women) )

# Get the diabetes data and set up as Lexis object
data(DMlate)
DMlate <- DMlate[sample(1:nrow(DMlate),500),]
dml <- Lexis( entry = list(Per=dodm, Age=dodm-dobth, DMdur=0 ),
               exit = list(Per=dox),
        exit.status = factor(!is.na(dodth),labels=c("DM","Dead")),
               data = DMlate )

# Split in 1-year age intervals
dms <- splitLexis( dml, time.scale="Age", breaks=0:100 )
summary( dms )

# Model age-specific rates using Ns with 6 knots
n.kn <- 6
( a.kn <- with( subset(dms,lex.Xst=="Dead"),
                quantile( Age+lex.dur, probs=(1:n.kn-0.5)/n.kn ) ) )
m1 <- glm( lex.Xst=="Dead" ~ Ns( Age, kn=a.kn ),
           offset = log( lex.dur ), family=poisson, data=dms )

# Plot estimated curve and knots chosen:
nd <- data.frame(Age=40:90,lex.dur=1000)
matplot( nd$Age, ci.pred( m1, newdata=nd ),
         type="l", lwd=c(3,1,1), lty=1, col="black", log="y",
         ylab="Mortality rates per 1000 PY", xlab="Age (years)", las=1 )
rug( a.kn, lwd=2 )

Run the code above in your browser using DataLab