Learn R Programming

basefun (version 1.1-4)

as.basis: Convert Formula or Factor to Basis Function

Description

Convert a formula or factor to basis functions

Usage

as.basis(object, ...)
# S3 method for formula
as.basis(object, data = NULL, remove_intercept = FALSE, 
         ui = NULL, ci = NULL, negative = FALSE, scale = FALSE, 
         Matrix = FALSE, prefix = "", ...)
# S3 method for factor_var
as.basis(object, ...)
# S3 method for ordered_var
as.basis(object, ...)
# S3 method for factor
as.basis(object, ...)
# S3 method for ordered
as.basis(object, ...)

Arguments

object

a formula or an object of class factor, factor_var, ordered or ordered_var

data

either a vars object or a data.frame

remove_intercept

a logical indicating if any intercept term shall be removed

ui

a matrix defining constraints

ci

a vector defining constraints

negative

a logical indicating negative basis functions

scale

a logical indicating a scaling of each column of the model matrix to the unit interval (based on observations in data)

Matrix

a logical requesting a sparse model matrix, that is, a Matrix object.

prefix

character prefix for model matrix column names (allows disambiguation of parameter names).

...

additional arguments to model.matrix, for example contrasts

Details

as.basis returns a function for the evaluation of the basis functions with corresponding model.matrix and predict methods.

Unordered factors (classes factor and factor_var) use a dummy coding and ordered factor (classes ordered or ordered_var) lead to a treatment contrast to the last level and removal of the intercept term with monotonicity constraint. Additional arguments (...) are ignored for ordered factors.

Linear constraints on parameters parm are defined by ui %*% parm >= ci.

Examples

Run this code

  ## define variables and basis functions
  v <- c(numeric_var("x"), factor_var("y", levels = LETTERS[1:3]))
  fb <- as.basis(~ x + y, data = v, remove_intercept = TRUE, negative = TRUE,
                 contrasts.arg = list(y = "contr.sum"))

  ## evaluate basis functions
  model.matrix(fb, data = as.data.frame(v, n = 10))
  ## basically the same as (but wo intercept and times -1)
  model.matrix(~ x + y, data = as.data.frame(v, n = 10))

  ### factor
  xf <- gl(3, 1)
  model.matrix(as.basis(xf), data = data.frame(xf = xf))

  ### ordered
  xf <- gl(3, 1, ordered = TRUE)
  model.matrix(as.basis(xf), data = data.frame(xf = unique(xf)))

Run the code above in your browser using DataLab