FormulaSums: Sums (aggregates) and/or sparse model matrix with possible...

View source: R/FormulaSums.R

FormulaSumsR Documentation

Sums (aggregates) and/or sparse model matrix with possible cross table

Description

By default this function return sums if the formula contains a response part and a model matrix otherwise

Usage

FormulaSums(
  data,
  formula,
  makeNames = TRUE,
  crossTable = FALSE,
  total = "Total",
  printInc = FALSE,
  dropResponse = FALSE,
  makeModelMatrix = NULL,
  sep = "-",
  sepCross = ":",
  avoidHierarchical = FALSE,
  includeEmpty = FALSE,
  NAomit = TRUE,
  rowGroupsPackage = "base",
  viaSparseMatrix = TRUE,
  ...
)

Formula2ModelMatrix(data, formula, dropResponse = TRUE, ...)

Arguments

data

data frame

formula

A model formula

makeNames

Column/row names made when TRUE

crossTable

Cross table in output when TRUE

total

String used to name totals

printInc

Printing "..." to console when TRUE

dropResponse

When TRUE response part of formula ignored.

makeModelMatrix

Make model matrix when TRUE. NULL means automatic.

sep

String to separate when creating column names

sepCross

String to separate when creating column names involving crossing

avoidHierarchical

Whether to avoid treating of hierarchical variables. Instead of logical, variables can be specified.

includeEmpty

When TRUE, empty columns of the model matrix (only zeros) are included. This is not implemented when a response term is included in the formula and dropResponse = FALSE (error will be produced).

NAomit

When TRUE, NAs in the grouping variables are omitted in output and not included as a separate category. Technically, this parameter is utilized through the function RowGroups.

rowGroupsPackage

Parameter pkg to the function RowGroups. Default is "base". Setting this parameter to "data.table" can improve speed.

viaSparseMatrix

When TRUE, the model matrix is constructed by a single call to sparseMatrix. Setting it to FALSE reverts to the previous behavior. This parameter is included for testing purposes and will likely be removed in future versions.

...

Further arguments to be passed to FormulaSums

Details

In the original version of the function the model matrix was constructed by calling fac2sparse repeatedly. Now this is replaced by a single call to sparseMatrix. The sums are computed by calling aggregate repeatedly. Hierarchical variables handled when constructing cross table. Column names constructed from the cross table. The returned model matrix includes the attribute startCol (see last example line).

Value

A matrix of sums, a sparse model matrix or a list of two or three elements (model matrix and cross table and sums when relevant).

Author(s)

Øyvind Langsrud

See Also

ModelMatrix

Examples

x <- SSBtoolsData("sprt_emp_withEU")

FormulaSums(x, ths_per ~ year*geo + year*eu)
FormulaSums(x, ~ year*age*eu)
FormulaSums(x, ths_per ~ year*age*geo + year*age*eu, crossTable = TRUE, makeModelMatrix = TRUE)
FormulaSums(x, ths_per ~ year:age:geo -1)
m <- Formula2ModelMatrix(x, ~ year*geo + year*eu)
print(m[1:3, ], col.names = TRUE)
attr(m, "startCol")

SSBtools documentation built on Oct. 30, 2024, 5:09 p.m.