ModelMatrix {SSBtools} | R Documentation |
Model matrix from hierarchies and/or a formula
Description
A common interface to Hierarchies2ModelMatrix
, Formula2ModelMatrix
and HierarchiesAndFormula2ModelMatrix
Usage
ModelMatrix(
data,
hierarchies = NULL,
formula = NULL,
inputInOutput = TRUE,
crossTable = FALSE,
sparse = TRUE,
viaOrdinary = FALSE,
total = "Total",
removeEmpty = !is.null(formula) & is.null(hierarchies),
modelMatrix = NULL,
dimVar = NULL,
select = NULL,
...
)
NamesFromModelMatrixInput(
data = NULL,
hierarchies = NULL,
formula = NULL,
dimVar = NULL,
...
)
Arguments
data |
Matrix or data frame with data containing codes of relevant variables |
hierarchies |
List of hierarchies, which can be converted by |
formula |
A model formula |
inputInOutput |
Logical vector (possibly recycled) for each element of hierarchies.
TRUE means that codes from input are included in output. Values corresponding to |
crossTable |
Cross table in output when TRUE |
sparse |
Sparse matrix in output when TRUE (default) |
viaOrdinary |
When TRUE, output is generated by |
total |
String(s) used to name totals |
removeEmpty |
When |
modelMatrix |
The model matrix as input (same as output) |
dimVar |
The main dimensional variables and additional aggregating variables. This parameter can be useful when hierarchies and formula are unspecified. |
select |
Data frame specifying variable combinations for output or a named list specifying code selections for each variable (see details). |
... |
Further arguments to |
Details
The default value of removeEmpty
corresponds to the default settings of the underlying functions.
The functions Hierarchies2ModelMatrix
and HierarchiesAndFormula2ModelMatrix
have removeEmpty
as an explicit parameter with FALSE
as default.
The function Formula2ModelMatrix
is a wrapper for FormulaSums
,
which has a parameter includeEmpty
with FALSE
as default.
Thus, ModelMatrix
makes a call to Formula2ModelMatrix
with includeEmpty = !removeEmpty
.
NamesFromModelMatrixInput
returns the names of the data columns involved in creating the model matrix.
Note that data
must be non-NULL to convert dimVar as indices to names.
The select
parameter is forwarded to Hierarchies2ModelMatrix
unless removeEmpty = TRUE
is combined with select
as a data frame.
In all other cases, select
is handled outside the underlying functions by making selections in the result.
Empty columns can be added to the model matrix when removeEmpty = FALSE
(with warning).
Value
A (sparse) model matrix or a list of two elements (model matrix and cross table)
Author(s)
Øyvind Langsrud
See Also
Examples
# Create some input
z <- SSBtoolsData("sp_emp_withEU")
ageHier <- data.frame(mapsFrom = c("young", "old"), mapsTo = "Total", sign = 1)
geoDimList <- FindDimLists(z[, c("geo", "eu")], total = "Europe")[[1]]
# Small dataset example. Two dimensions.
s <- z[z$geo == "Spain" & z$year != 2016, ]
rownames(s) <- NULL
s
# via Hierarchies2ModelMatrix() and converted to ordinary matrix (not sparse)
ModelMatrix(s, list(age = ageHier, year = ""), sparse = FALSE)
# Hierarchies generated automatically. Then via Hierarchies2ModelMatrix()
ModelMatrix(s[, c(1, 4)])
# via Formula2ModelMatrix()
ModelMatrix(s, formula = ~age + year)
# via model.matrix() after adding empty factor levels
ModelMatrix(s, formula = ~age + year, sparse = FALSE, viaOrdinary = TRUE)
# via sparse.model.matrix() after adding empty factor levels
ModelMatrix(s, formula = ~age + year, viaOrdinary = TRUE)
# via HierarchiesAndFormula2ModelMatrix() and using different data and parameter settings
ModelMatrix(s, list(age = ageHier, geo = geoDimList, year = ""), formula = ~age * geo + year,
inputInOutput = FALSE, removeEmpty = TRUE, crossTable = TRUE)
ModelMatrix(s, list(age = ageHier, geo = geoDimList, year = ""), formula = ~age * geo + year,
inputInOutput = c(TRUE, FALSE), removeEmpty = FALSE, crossTable = TRUE)
ModelMatrix(z, list(age = ageHier, geo = geoDimList, year = ""), formula = ~age * year + geo,
inputInOutput = c(FALSE, TRUE), crossTable = TRUE)
# via Hierarchies2ModelMatrix() using unnamed list element. See AutoHierarchies.
colnames(ModelMatrix(z, list(age = ageHier, c(Europe = "geo", Allyears = "year", "eu"))))
colnames(ModelMatrix(z, list(age = ageHier, c("geo", "year", "eu")), total = c("t1", "t2")))
# Example using the select parameter as a data frame
select <- data.frame(age = c("Total", "young", "old"), geo = c("EU", "nonEU", "Spain"))
ModelMatrix(z, list(age = ageHier, geo = geoDimList),
select = select, crossTable = TRUE)$crossTable
# Examples using the select parameter as a list
ModelMatrix(z, list(age = ageHier, geo = geoDimList), inputInOutput = FALSE,
select = list(geo = c("nonEU", "Portugal")), crossTable = TRUE)$crossTable
ModelMatrix(z, list(age = ageHier, geo = geoDimList),
select = list(geo = c("nonEU", "Portugal"), age = c("Total", "young")),
crossTable = TRUE)$crossTable