Learn R Programming

dlnm (version 1.6.2)

onebasis: Generate a Basis Matrix for Different Functions

Description

Generate the basis matrix for a predictor vector, choosing among a set of possible basis functions. Specifically, different types of splines, polynomials, strata and linear threshold functions.

Usage

onebasis(x, type="ns", df=1, degree=1, knots=NULL, bound, int=FALSE, cen)

## S3 method for class 'onebasis':
summary(object, ...)

Arguments

x
the predictor variable. Missing values are allowed.
type
type of basis. See Details below for the list of possible choices.
df
dimension of the basis, equivalent to number of degrees of freedom. They depend on knots if provided, or on degree for type="poly".
degree
degree of polynomial. Used only for type equal to "bs" (degree of the piecewise polynomial for the B-spline) or "poly" (degree of the polynomial).
knots
knots location for the basis function. They specify the position of the internal knots for "ns" and "bs", the cut-off points for "strata" (defining right-open intervals) and the threshold(s)/cut-off points for
bound
boundary knots (sometimes called external knots). Used only for type equal to "ns" and "bs". Default to the range of the variable.
int
logical. If TRUE, an 'intercept' is included in the basis matrix, with different meanings depending on type above: see Details below.
cen
logical or a numeric scalar. It specifies the value the basis functions for the space of predictor are centered at, then used as a reference for predictions. See Note below.
object
a object of class "onebasis".
...
additional arguments to be passed to summary.

Value

  • A matrix object of class "onebasis" which can be included in a model formula in order to estimate the association. It contains the attribute range (range of the original vector of observations) and additional attributes corresponding to the arguments above. The function summary.onebasis returns a summary of the basis matrix and the related attributes, and can be used to check the options for the chosen basis function.

Warnings

Meaningless combinations of arguments (for example the inclusion of knots lying outside the range for type equal to "strata" or thr-type) could lead to collinear variables, with identifiability problems in the model and the exclusion of some of them.

Details

The value in type defines the basis function. It must be one of: "ns": natural cubic B-splines (constrained to be linear beyond the boundary knots). Specified by knots (internal knots) and bound (boundary or external knots). If knots is provided, the dimension df is set to length(knots)+1+int. An intercept is included if int=T (corresponding to a vector of 1's if df=1, involving a more complex parameterization for df>1). See the functions ns for additional information. The transformed variables can be centered by cen. "bs": B-splines characterized by degree (degree of the piecewise polynomial). Specified by knots (internal knots) and bound (boundary or external knots). If knots is provided, the dimension df is set to length(knots)+degree+int; if not, df must be higher than degree+int. An intercept is included if int=T (corresponding to a vector of 1's if df=1, involving a more complex parameterization for df>1). See the functions bs for additional information. The transformed variables can be centered by cen. "strata": strata variables (dummy parameterization) determined by internal cut-off values specified in knots, which represent the lower boundaries for the right-open intervals. Intervals containing no observation are automatically discarded. If knots is provided, the dimension df is set to length(knots)+int. A dummy variable for the reference stratum (the first one by default) is included if int=T, generating a full rank basis. Never centered. "poly": polynomial with power specified by degree. The dimension df is set to to degree+int. An intercept, corresponding to a vector of 1's (the power 0 of the polynomial) is included if int=T. The transformed variables can be centered by cen. "integer": strata variables (dummy parameterization) for each integer values, expressly created to specify an unconstrained function in the space of lags. df is set automatically to the number of integer values minus 1 plus int. A dummy variable for the reference stratum (the first one by default) is included if int=T, generating a full rank basis. Never centered. "hthr", "lthr": high and low threshold parameterization, with a linear relationship above or below the threshold, respectively, and flat otherwise. The threshold is chosen by knots: if more than one is provided, a piecewise linear relationship is applied above the first knot or below the last one, respectively, with the slope changing at each further knot. df is automatically set to length(knots)+int. An intercept (corresponding to a vector of 1's) is included if int=T. Never centered. "dthr": double threshold parameterization (2 independent linear relationships above the second and below the first threshold, flat between them). The thresholds are chosen by knots. If only one is provided, the threshold is unique (V-model). If more than 2 are provided, the first and the last ones are chosen. df is automatically set to 2+int. An intercept (corresponding to a vector of 1's) is included if int=T. Never centered. "lin": linear relationship (untransformed apart from optional centering). df is automatically set to 1+int. An intercept (corresponding to a vector of 1's) is included if int=T. It can be centered by cen. Results from models including basis functions are interpreted here relatively to a reference value of the predictor, determined automatically or through a centering point (see also Note below). In the latter case, the centering value is chosen by cen (if a numeric scalar), or fixed at the mean if cen=TRUE. The basis is uncentered for cen=FALSE. Some other arguments can be automatically changed for not sensible combinations, or set to NULL if not required. Use summary.onebasis to check the result. For a detailed illustration of the use of the function, see: vignette("dlnmOverview")

See Also

crossbasis to generate cross-basis matrices. crosspred to obtain predictions after model fitting. plot.crosspred to plot several type of graphs. See dlnm-package for an overview of the package and type 'vignette(dlnmOverview)' for a detailed description.

Examples

Run this code
### relationship between PM10 and mortality: strata
b <- onebasis(chicagoNMMAPS$pm10, "strata", knots=c(20,40))
summary(b)
model <- glm(death ~ b, family=quasipoisson(), chicagoNMMAPS)
pred <- crosspred(b, model, at=0:60)
plot(pred, xlab="PM10", ylab="RR", main="RR for PM10")

### relationship between temperature and mortality: double threshold
b <- onebasis(chicagoNMMAPS$temp, "dthr", knots=c(10,25))
summary(b)
model <- glm(death ~ b, family=quasipoisson(), chicagoNMMAPS)
pred <- crosspred(b, model, by=1)
plot(pred, xlab="Temperature (C)", ylab="RR", main="RR for temperature")

### extending the example for the 'ns' function in package splines
b <- onebasis(women$height, df=5)
summary(b)
model <- lm(weight ~ b, data=women)
pred <- crosspred(b, model)
plot(pred, xlab="Height (in)", ylab="Weight (lb) difference",
  main="Association between weight and height")

Run the code above in your browser using DataLab