knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

weightedcontrasts

CRAN Version

Provides the function contr.poly.weighted to apply orthogonal polynomial contrasts to unbalanced data. The function is general, but the examples are specific to age-period-cohort models. Currently, the package contains the following:

Installation

You can install the development version:

remotes::install_github("elbersb/weightedcontrasts")

Example

This example shows how contr.poly.weighted provides a contrast matrix that will lead to an orthogonal design matrix, even if the data are unbalanced. We assume a situation in which an evenly-spaced predictor, such as age groups (e.g., 20-24, 25-29, 30-34, etc.).

library("weightedcontrasts")

# first, the balanced case
group <- factor(c(20, 20, 25, 25, 30, 30))
contrasts(group) <- contr.poly
X <- model.matrix(~ group)
zapsmall(crossprod(X)) # correct result

# but in the unbalanced case, contr.poly fails
group <- factor(c(20, 20, 25, 25, 30))
contrasts(group) <- contr.poly
X <- model.matrix(~ group)
zapsmall(crossprod(X)) # wrong result!

# this is where contr.poly.weighted comes in
# (width specifies the width of the group intervals)
group <- factor(c(20, 20, 25, 25, 30))
contrasts(group) <- contr.poly.weighted(group, width = 5)
X <- model.matrix(~ group)
zapsmall(crossprod(X)) # correct result

References

Elbers, Benjamin. 2020. Orthogonal Polynomial Contrasts and Applications to Age-Period-Cohort Models, Working Paper.

Fosse, Ethan and Christopher Winship. 2019. Analyzing Age-Period-Cohort Data: A Review and Critique, Annual Review of Sociology 45:467–92.

Holford, Theodore R.. 1983. The Estimation of Age, Period and Cohort Effects for Vital Rates, Biometrics 39(2): 311-324.



elbersb/weightedcontrasts documentation built on Dec. 20, 2021, 4:15 a.m.