makeind: Make Model Matrix from Data Frame

Description Usage Arguments Details Value Author(s) Examples

View source: R/bart.R

Description

Converts a data frame with numeric and factor contents into a matrix, suitable for use with bart. Unlike in linear regression, factors containing more than two levels result in dummy variables being created for each level.

Usage

1
2

Arguments

x

Data frame of explanatory variables.

drop

Logical or list controling whether or not columns that are constants or factor levels with no instances are omitted from the result. When a list, must be of length equal to x. Elements correspond to x according to:

  • vector - single logical

  • matrix - vector of logicals, one per column

  • factor - table of factor levels to be referenced; levels with counts of 0 are to be dropped

all

Not currently implemented.

Details

Note that if you have train and test data frames, it may be best to rbind the two together, apply makeModelMatrixFromDataFrame to the result, and then pull them back apart. Alternatively, save the drop attribute used in creating the train data and use it when creating a matrix from the test data. Example given below.

Value

A matrix with columns corresponding to the elements of the data frame. If drop = TRUE or is a list, the attribute drop on the result is set to the list used when creating the matrix.

Author(s)

Vincent Dorie: [email protected].

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
iv <- 1:10
rv <- runif(10)
f <- factor(rep(seq.int(3), c(4L, 4L, 2L)),
            labels = c("alice", "bob", "charlie"))
df <- data.frame(iv, rv, f)

mm <- makeModelMatrixFromDataFrame(df)

## create test and train matrices with disjoint factor levels
train.df <- df[1:8,]
test.df <- df[9:10,]
train.mm <- makeModelMatrixFromDataFrame(train.df)
test.mm <- makeModelMatrixFromDataFrame(test.df, attr(train.mm, "drop"))

vdorie/dbarts documentation built on Dec. 7, 2018, 7:53 a.m.