nested_sum_contrasts: Sum contrasts for a nested factor

Description Usage Arguments Details Value Note Author(s) References See Also Examples

Description

The function creates sum contrasts for a factor whose levels are nested within the levels of another factor.

Usage

1
nested_sum_contrasts(outer.factor, inner.factor)

Arguments

outer.factor

A factor.

inner.factor

A factor whose levels are nested in the levels of the outer.factor.

Details

The outer and inner factors need to have the same length. It can be data that is used in a regression model, so it is allowed to have multiple instances of the inner factor in the inner.factor, but it can also be shorter factors that just describe the nesting.

The contrast matrix can be used directly on the inner factor using the funtion contrasts, but it is usually necessary to specify the argument how.many=nlevels(inner.factor) - nlevels(outer.factor).

The function superMatrix is used to make a block diagonal matrix.

Value

An object of class matrix with nlevels(inner.factor) rows and nlevels(inner.factor) - nlevels(outer.factor) columns.

Note

The nesting of the input vectors is not checked.

Author(s)

Johanna Bertl

References

Bertl, J.; Guo, Q.; Rasmussen, M. J.; Besenbacher, S; Nielsen, M. M.; Hornshøj, H.; Pedersen, J. S. & Hobolth, A. A Site Specific Model And Analysis Of The Neutral Somatic Mutation Rate In Whole-Genome Cancer Data. bioRxiv, 2017. doi: https://doi.org/10.1101/122879 http://www.biorxiv.org/content/early/2017/06/21/122879

See Also

contr.sum, contrasts, nested_treatment_contrasts

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Sum contrasts for the cancer type and the sample ID, which is nested in the cancer type.

data("cancermutations")

contrasts(cancermutations$cancer_type) = contr.sum(nlevels(cancermutations$cancer_type))
nesting = nested_sum_contrasts(outer.factor=cancermutations$cancer_type, inner.factor=cancermutations$sample_id)
how.many = nlevels(cancermutations$sample_id) - nlevels(cancermutations$cancer_type)
contrasts(cancermutations$sample_id, how.many=how.many) = nesting

# Visualize the contrast matrix: 
image(t(nesting[273:1,]))

MultinomialMutations/MultinomialMutations documentation built on May 22, 2019, 4:39 p.m.