FormulaSelection: Limit matrix or data frame to selected model terms

View source: R/FormulaSelection.R

FormulaSelectionR Documentation

Limit matrix or data frame to selected model terms

Description

For use with output from ModelMatrix or data frames derived from such output.

Usage

FormulaSelection(x, formula, intercept = NA)

formula_selection(x, formula, intercept = NA)

Arguments

x

Model matrix or a data frame

formula

Formula representing the limitation or character string(s) to be converted to a formula (see details)

intercept

Parameter that specifies whether a possible intercept term (overall total) should be included in the output. Default is TRUE when a formula is input. Otherwise, see details.

Details

The selection is based on startCol or startRow attribute in input x.

With formula as character:

  • ~ is included: Input is converted by as.formula and default intercept is TRUE.

  • ~ is not included: Internally, input data is converted to a formula by adding ~ and possibly +'s when the length is ⁠>1⁠. Default intercept is FALSE unless "1" or "(Intercept)" (is changed internally to "1") is included.

Value

Limited model matrix or a data frame

Note

formula_selection and FormulaSelection are identical

Examples

z <- SSBtoolsData("sprt_emp_withEU")
z$age[z$age == "Y15-29"] <- "young"
z$age[z$age == "Y30-64"] <- "old"

x <- ModelMatrix(z, formula = ~age * year)

FormulaSelection(x, "age")
FormulaSelection(x, ~year)
FormulaSelection(x, ~year:age)

# x1, x2, x3, x4 and x4 are identical
x1 <- FormulaSelection(x, ~age)
x2 <- FormulaSelection(x, "~age")
x3 <- FormulaSelection(x, "age", intercept = TRUE)
x4 <- FormulaSelection(x, c("1", "age"))
x5 <- FormulaSelection(x, c("(Intercept)", "age"))


a <- ModelMatrix(z, formula = ~age * geo + year, crossTable = TRUE)
b <- cbind(as.data.frame(a$crossTable), 
           sum = (t(a$modelMatrix) %*% z$ths_per)[, 1], 
           max = DummyApply(a$modelMatrix, 
           z$ths_per, max))
rownames(b) <- NULL
attr(b, "startRow") <- attr(a$modelMatrix, "startCol", exact = TRUE)

FormulaSelection(b, ~geo * age)
FormulaSelection(b, "age:geo")
FormulaSelection(b, ~year - 1)

statisticsnorway/SSBtools documentation built on Jan. 17, 2024, 3:40 p.m.