Contrast-coded dummy matrix (treatment coding) created from a factor
make.dummies(x, ...)
# Default S3 method
make.dummies(x, base = 1L, base.add = TRUE, ...)
# S3 method for class 'data.frame'
make.dummies(x, col, base = 1L, base.add = TRUE, ...)
# S3 method for class 'pdata.frame'
make.dummies(x, col, base = 1L, base.add = TRUE, ...)a factor from which the dummies are created (x is coerced to factor if not yet a factor) for the default method or a data data frame/pdata.frame for the respective method.
further arguments.
integer or character, specifies the reference level (base), if
integer it refers to position in levels(x), if character the name
of a level,
logical, if TRUE the reference level (base) is added
to the return value as first column, if FALSE the reference
level is not included.
character (only for the data frame and pdata.frame methods), to specify the column which is used to derive the dummies from,
For the default method, a matrix containing the contrast-coded
dummies (treatment coding),
dimensions are n x n where n = length(levels(x)) if argument
base.add = TRUE or n = length(levels(x)-1) if base.add = FALSE;
for the data frame and pdata.frame method, a data frame or pdata.frame,
respectively, with the dummies appropriately merged to the input as
last columns (column names are derived from the name of the column
used to create the dummies and its levels).
This function creates a matrix of dummies from the levels of a factor in treatment coding. In model estimations, it is usually preferable to not create the dummy matrix prior to estimation but to simply specify a factor in the formula and let the estimation function handle the creation of the dummies.
This function is merely a convenience wrapper around stats::contr.treatment
to ease the dummy matrix creation process shall the dummy matrix be explicitly
required. See Examples for a use case in LSDV (least squares dummy variable)
model estimation.
The default method uses a factor as main input (or something coercible to a
factor) to derive the dummy matrix from. Methods for data frame and pdata.frame
are available as well and have the additional argument col to specify the
the column from which the dummies are created; both methods merge the dummy
matrix to the data frame/pdata.frame yielding a ready-to-use data set.
See also Examples for use cases.
library(plm)
data("Grunfeld", package = "plm")
Grunfeld <- Grunfeld[1:100, ] # reduce data set (down to 5 firms)
## default method
make.dummies(Grunfeld$firm) # gives 5 x 5 matrix (5 firms, base level incl.)
#> 1 2 3 4 5
#> 1 1 0 0 0 0
#> 2 0 1 0 0 0
#> 3 0 0 1 0 0
#> 4 0 0 0 1 0
#> 5 0 0 0 0 1
make.dummies(Grunfeld$firm, base = 2L, base.add = FALSE) # gives 5 x 4 matrix
#> 1 3 4 5
#> 1 1 0 0 0
#> 2 0 0 0 0
#> 3 0 1 0 0
#> 4 0 0 1 0
#> 5 0 0 0 1
## data frame method
Grun.dummies <- make.dummies(Grunfeld, col = "firm")
## pdata.frame method
pGrun <- pdata.frame(Grunfeld)
pGrun.dummies <- make.dummies(pGrun, col = "firm")
## Model estimation:
## estimate within model (individual/firm effects) and LSDV models (firm dummies)
# within model:
plm(inv ~ value + capital, data = pGrun, model = "within")
#>
#> Model Formula: inv ~ value + capital
#> <environment: 0x5c8953b71c30>
#>
#> Coefficients:
#> value capital
#> 0.11492 0.32118
#>
## LSDV with user-created dummies by make.dummies:
form_dummies <- paste0("firm", c(1:5), collapse = "+")
form_dummies <- formula(paste0("inv ~ value + capital + ", form_dummies))
plm(form_dummies, data = pGrun.dummies, model = "pooling") # last dummy is dropped
#>
#> Model Formula: inv ~ value + capital + firm1 + firm2 + firm3 + firm4 + firm5
#> <environment: 0x5c8953b71c30>
#>
#> Coefficients:
#> (Intercept) value capital firm11 firm21 firm31
#> -121.13966 0.11492 0.32118 22.84207 210.30747 -128.19477
#> firm41
#> 88.65668
#>
# LSDV via factor(year) -> let estimation function generate dummies:
plm(inv ~ value + capital + factor(firm), data = pGrun, model = "pooling")
#>
#> Model Formula: inv ~ value + capital + factor(firm)
#> <environment: 0x5c8953b71c30>
#>
#> Coefficients:
#> (Intercept) value capital factor(firm)2 factor(firm)3
#> -98.29759 0.11492 0.32118 187.46540 -151.03684
#> factor(firm)4 factor(firm)5
#> 65.81461 -22.84207
#>