build.x: build.x

View source: R/buildMatrix.r

build.xR Documentation

build.x

Description

Build the x matrix for a glmnet model

Usage

build.x(formula, data, contrasts = TRUE, sparse = FALSE)

Arguments

formula

A formula

data

A data.frame

contrasts

Logical indicating whether a factor's base level is removed. Can be either one single value applied to every factor or a value for each factor. Values will be recycled if necessary.

sparse

Logical indicating if result should be sparse.

Details

Given a formula and a data.frame build the predictor matrix

Value

A matrix of the predictor variables specified in the formula

Author(s)

Jared P. Lander

Examples

require(ggplot2)
head(mpg)
head(build.x(hwy ~ class + cyl + year, data=mpg))

testFrame <- data.frame(First=sample(1:10, 20, replace=TRUE), 
Second=sample(1:20, 20, replace=TRUE), 
Third=sample(1:10, 20, replace=TRUE), 
Fourth=factor(rep(c("Alice","Bob","Charlie","David"), 5)), 
Fifth=ordered(rep(c("Edward","Frank","Georgia","Hank","Isaac"), 4)), 
Sixth=factor(rep(c("a", "b"), 10)), stringsAsFactors=F)
head(build.x(First ~ Second + Fourth + Sixth, testFrame, 
contrasts=c("Fourth"=TRUE, "Fifth"=FALSE, "Sixth"=TRUE)))
head(build.x(First ~ Second + Fourth + Fifth + Sixth, testFrame, 
contrasts=c(Fourth=TRUE, Fifth=FALSE, Sixth=TRUE)))
head(build.x(First ~ Second + Fourth + Fifth + Sixth, testFrame, contrasts=TRUE))
head(build.x(First ~ Second + Fourth + Fifth + Sixth, testFrame, 
contrasts=FALSE))
head(build.x(First ~ Second + Fourth + Fifth + Sixth - 1, testFrame, 
contrasts=TRUE))
build.x(First ~ Second + Fourth + Fifth + Sixth - 1, testFrame, 
contrasts=TRUE, sparse=TRUE)
head(build.x(First ~ Second + Fourth + Fifth + Fourth*Sixth, testFrame, contrasts=TRUE))
head(build.x(First ~ Second + Fourth + Fifth + Third*Sixth, testFrame, contrasts=TRUE))
#' head(build.x(First ~ Second + Fourth + Fifth + Fourth*Sixth, testFrame, contrasts=FALSE))
head(build.x(First ~ Second + Fourth + Fifth + Third*Sixth, testFrame, contrasts=FALSE))
build.x(First ~ Second + Fourth + Fifth + Third*Sixth, testFrame, contrasts=FALSE, sparse=TRUE)

## if contrasts is a list then you can specify just certain factors

useful documentation built on Oct. 24, 2023, 9:07 a.m.