dummyVars
creates a full set of dummy variables (i.e. less than full
rank parameterization)
dummyVars(formula, ...) # S3 method for default dummyVars(formula, data, sep = ".", levelsOnly = FALSE, fullRank = FALSE, ...) # S3 method for dummyVars print(x, ...) # S3 method for dummyVars predict(object, newdata, na.action = na.pass, ...) contr.ltfr(n, contrasts = TRUE, sparse = FALSE) class2ind(x, drop2nd = FALSE)
formula | An appropriate R model formula, see References |
---|---|
... | additional arguments to be passed to other methods |
data | A data frame with the predictors of interest |
sep | An optional separator between factor variable names and their
levels. Use |
levelsOnly | A logical; |
fullRank | A logical; should a full rank or less than full rank
parameterization be used? If |
x | A factor vector. |
object | An object of class |
newdata | A data frame with the required columns |
na.action | A function determining what should be done with missing
values in |
n | A vector of levels for a factor, or the number of levels. |
contrasts | A logical indicating whether contrasts should be computed. |
sparse | A logical indicating if the result should be sparse. |
drop2nd | A logical: if the factor has two levels, should a single binary vector be returned? |
The output of dummyVars
is a list of class 'dummyVars' with
elements
the function call
the model formula
names of all the variables in the model
names of all the factor variables in the model
levels of any factor variables
NULL
or a character separator
the terms.formula
object
a logical
Most of the contrasts
functions in R produce full rank
parameterizations of the predictor data. For example,
contr.treatment
creates a reference cell in the data
and defines dummy variables for all factor levels except those in the
reference cell. For example, if a factor with 5 levels is used in a model
formula alone, contr.treatment
creates columns for the
intercept and all the factor levels except the first level of the factor.
For the data in the Example section below, this would produce:
(Intercept) dayTue dayWed dayThu dayFri daySat daySun 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 1 0 0
In some situations, there may be a need for dummy variables for all the levels of the factor. For the same example:
dayMon dayTue dayWed dayThu dayFri daySat daySun 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0
Given a formula and initial data set, the class dummyVars
gathers all
the information needed to produce a full set of dummy variables for any data
set. It uses contr.ltfr
as the base function to do this.
class2ind
is most useful for converting a factor outcome vector to a
matrix (or vector) of dummy variables.
https://cran.r-project.org/doc/manuals/R-intro.html#Formulae-for-statistical-models
when <- data.frame(time = c("afternoon", "night", "afternoon", "morning", "morning", "morning", "morning", "afternoon", "afternoon"), day = c("Mon", "Mon", "Mon", "Wed", "Wed", "Fri", "Sat", "Sat", "Fri")) levels(when$time) <- list(morning="morning", afternoon="afternoon", night="night") levels(when$day) <- list(Mon="Mon", Tue="Tue", Wed="Wed", Thu="Thu", Fri="Fri", Sat="Sat", Sun="Sun") ## Default behavior: model.matrix(~day, when)#> (Intercept) dayTue dayWed dayThu dayFri daySat daySun #> 1 1 0 0 0 0 0 0 #> 2 1 0 0 0 0 0 0 #> 3 1 0 0 0 0 0 0 #> 4 1 0 1 0 0 0 0 #> 5 1 0 1 0 0 0 0 #> 6 1 0 0 0 1 0 0 #> 7 1 0 0 0 0 1 0 #> 8 1 0 0 0 0 1 0 #> 9 1 0 0 0 1 0 0 #> attr(,"assign") #> [1] 0 1 1 1 1 1 1 #> attr(,"contrasts") #> attr(,"contrasts")$day #> [1] "contr.treatment" #>mainEffects <- dummyVars(~ day + time, data = when) mainEffects#> Dummy Variable Object #> #> Formula: ~day + time #> <environment: 0xd5f5238> #> 2 variables, 2 factors #> Variables and levels will be separated by '.' #> A less than full rank encoding is used#> day.Mon day.Tue day.Wed day.Thu day.Fri day.Sat day.Sun time.morning #> 1 1 0 0 0 0 0 0 0 #> 2 1 0 0 0 0 0 0 0 #> 3 1 0 0 0 0 0 0 0 #> time.afternoon time.night #> 1 1 0 #> 2 0 1 #> 3 1 0#> day.Mon day.Tue day.Wed day.Thu day.Fri day.Sat day.Sun time.morning #> 1 1 0 0 0 0 0 0 NA #> 2 1 0 0 0 0 0 0 0 #> 3 1 0 0 0 0 0 0 0 #> time.afternoon time.night #> 1 NA NA #> 2 0 1 #> 3 1 0#> day.Mon day.Tue day.Wed day.Thu day.Fri day.Sat day.Sun time.morning #> 2 1 0 0 0 0 0 0 0 #> 3 1 0 0 0 0 0 0 0 #> time.afternoon time.night #> 2 0 1 #> 3 1 0interactionModel <- dummyVars(~ day + time + day:time, data = when, sep = ".") predict(interactionModel, when[1:3,])#> day.Mon day.Tue day.Wed day.Thu day.Fri day.Sat day.Sun time.morning #> 1 1 0 0 0 0 0 0 0 #> 2 1 0 0 0 0 0 0 0 #> 3 1 0 0 0 0 0 0 0 #> time.afternoon time.night dayMon:timemorning dayTue:timemorning #> 1 1 0 0 0 #> 2 0 1 0 0 #> 3 1 0 0 0 #> dayWed:timemorning dayThu:timemorning dayFri:timemorning daySat:timemorning #> 1 0 0 0 0 #> 2 0 0 0 0 #> 3 0 0 0 0 #> daySun:timemorning dayMon:timeafternoon dayTue:timeafternoon #> 1 0 1 0 #> 2 0 0 0 #> 3 0 1 0 #> dayWed:timeafternoon dayThu:timeafternoon dayFri:timeafternoon #> 1 0 0 0 #> 2 0 0 0 #> 3 0 0 0 #> daySat:timeafternoon daySun:timeafternoon dayMon:timenight dayTue:timenight #> 1 0 0 0 0 #> 2 0 0 1 0 #> 3 0 0 0 0 #> dayWed:timenight dayThu:timenight dayFri:timenight daySat:timenight #> 1 0 0 0 0 #> 2 0 0 0 0 #> 3 0 0 0 0 #> daySun:timenight #> 1 0 #> 2 0 #> 3 0noNames <- dummyVars(~ day + time + day:time, data = when, levelsOnly = TRUE) predict(noNames, when)#> Mon Tue Wed Thu Fri Sat Sun morning afternoon night dayMon:timemorning #> 1 1 0 0 0 0 0 0 0 1 0 0 #> 2 1 0 0 0 0 0 0 0 0 1 0 #> 3 1 0 0 0 0 0 0 0 1 0 0 #> 4 0 0 1 0 0 0 0 1 0 0 0 #> 5 0 0 1 0 0 0 0 1 0 0 0 #> 6 0 0 0 0 1 0 0 1 0 0 0 #> 7 0 0 0 0 0 1 0 1 0 0 0 #> 8 0 0 0 0 0 1 0 0 1 0 0 #> 9 0 0 0 0 1 0 0 0 1 0 0 #> dayTue:timemorning dayWed:timemorning dayThu:timemorning dayFri:timemorning #> 1 0 0 0 0 #> 2 0 0 0 0 #> 3 0 0 0 0 #> 4 0 1 0 0 #> 5 0 1 0 0 #> 6 0 0 0 1 #> 7 0 0 0 0 #> 8 0 0 0 0 #> 9 0 0 0 0 #> daySat:timemorning daySun:timemorning dayMon:timeafternoon #> 1 0 0 1 #> 2 0 0 0 #> 3 0 0 1 #> 4 0 0 0 #> 5 0 0 0 #> 6 0 0 0 #> 7 1 0 0 #> 8 0 0 0 #> 9 0 0 0 #> dayTue:timeafternoon dayWed:timeafternoon dayThu:timeafternoon #> 1 0 0 0 #> 2 0 0 0 #> 3 0 0 0 #> 4 0 0 0 #> 5 0 0 0 #> 6 0 0 0 #> 7 0 0 0 #> 8 0 0 0 #> 9 0 0 0 #> dayFri:timeafternoon daySat:timeafternoon daySun:timeafternoon #> 1 0 0 0 #> 2 0 0 0 #> 3 0 0 0 #> 4 0 0 0 #> 5 0 0 0 #> 6 0 0 0 #> 7 0 0 0 #> 8 0 1 0 #> 9 1 0 0 #> dayMon:timenight dayTue:timenight dayWed:timenight dayThu:timenight #> 1 0 0 0 0 #> 2 1 0 0 0 #> 3 0 0 0 0 #> 4 0 0 0 0 #> 5 0 0 0 0 #> 6 0 0 0 0 #> 7 0 0 0 0 #> 8 0 0 0 0 #> 9 0 0 0 0 #> dayFri:timenight daySat:timenight daySun:timenight #> 1 0 0 0 #> 2 0 0 0 #> 3 0 0 0 #> 4 0 0 0 #> 5 0 0 0 #> 6 0 0 0 #> 7 0 0 0 #> 8 0 0 0 #> 9 0 0 0#> setosa versicolor virginica #> 1 1 0 0 #> 2 1 0 0 #> 3 1 0 0 #> 4 1 0 0 #> 5 1 0 0 #> 6 1 0 0#> a b #> 1 1 0 #> 2 1 0 #> 3 1 0 #> 4 1 0 #> 5 1 0 #> 6 0 1 #> 7 0 1 #> 8 0 1 #> 9 0 1 #> 10 0 1class2ind(two_levels, drop2nd = TRUE)#> 1 2 3 4 5 6 7 8 9 10 #> 1 1 1 1 1 0 0 0 0 0