Learn R Programming

SSBtools (version 1.7.0)

FormulaSelection.default: Limit matrix or data frame to selected model terms

Description

For use with output from ModelMatrix or data frames derived from such output. It is a generic function which means that methods for other input objects can be added.

Usage

# S3 method for default
FormulaSelection(x, formula, intercept = NA, logical = FALSE)

FormulaSelection(x, formula, intercept = NA, logical = FALSE)

formula_selection(x, formula, intercept = NA, logical = FALSE)

Value

Limited model matrix or a data frame

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.

logical

When TRUE, the logical selection vector is returned.

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.

Examples

Run this code
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)
FormulaSelection(b, ~geo:age, logical = TRUE)

Run the code above in your browser using DataLab