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,
  ...
)

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).

...

Further arguments to be passed to FormulaSums

Details

The model matrix is constructed by calling fac2sparse() repeatedly. 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 July 9, 2023, 6:16 p.m.