ModelMatrixModel {ModelMatrixModel}R Documentation

ModelMatrixModel() function

Description

This function transforms a data.frame to matrix (or sparse matrix) based on a r formula. The mean different from model.matrix() function is that it outputs a class stored with the transformed matrix, as well as the transforming parameters which can be applied to new data.

Usage

ModelMatrixModel(
  rformula,
  data,
  sparse = TRUE,
  center = FALSE,
  scale = FALSE,
  remove_1st_dummy = FALSE,
  verbose = FALSE
)

Arguments

rformula

a formula, e.g. formula("~ 1+x1+x2"),"~ 1+x1+x2",or ~ 1+x1+x2 . Note the interpreting of the formula might be different slightly from model.matrix function. In model.matrix(),intercept column will be included in output matrix with or without "1" in the formula. But in ModelMatrixModel(),intercept column will be included in output matrix only when "1" is present. Moreover "0" or "." in the formula will be ignored.

data

a data.frame.

sparse

boolean, if TRUE return a sparse matrix, i.e. a "dgCMatrix" class.

center

boolean, if center the output.

scale

boolean, if scale the output.

remove_1st_dummy

boolean, if remove the first dummy variable in one hot key transformation.

verbose

boolean, if print out progress.

Details

see vignettes.

Value

A ModelMatrixModel class,which includes the transformed matrix and the transforming parameters.

Examples

library(ModelMatrixModel)
traindf= data.frame(x1 = sample(LETTERS[1:5], replace = TRUE, 20),
                    x2 = rnorm(20, 100, 5),
                    y = rnorm(20, 10, 2))
mm=ModelMatrixModel(~x1+x2,traindf,remove_1st_dummy = FALSE)
data.frame(as.matrix(head(mm$x,2)))

[Package ModelMatrixModel version 0.1.0 Index]