ipu_step: Perform one step of iterative proportional updating

Description Usage Arguments Details Examples

View source: R/RcppExports.R

Description

C++ routines to invoke a single iteration of the Iterative proportional updating (IPU) scheme. Targets and classes are assumed to be one dimensional in the ipu_step functions. combine_factors aggregates several vectors of type factor into a single one to allow multidimensional ipu-steps. See examples.

Usage

1
2
3
4
5
6
7
combine_factors(dat, targets)

ipu_step_ref(w, classes, targets)

ipu_step(w, classes, targets)

ipu_step_f(w, classes, targets)

Arguments

dat

a data.frame containing the factor variables to be combined.

targets

key figure to target with the ipu scheme. A numeric verctor of the same length as levels(classes). This can also be a table produced by xtabs. See examples.

w

a numeric vector of weights. All entries should be positive.

classes

a factor variable. Must have the same length as w.

Details

ipu_step returns the adjusted weights. ipu_step_ref does the same, but updates w by reference rather than returning. ipu_step_f returns a multiplicator: adjusted weights divided by unadjusted weights. combine_factors is designed to make ipu_step work with contingency tables produced by xtabs.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
############# one-dimensional ipu ##############

## create random data
nobs <- 10
classLabels <- letters[1:3]
dat = data.frame(
  weight = exp(rnorm(nobs)),
  household = factor(sample(classLabels, nobs, replace = TRUE))
)
dat

## create targets (same lenght as classLabels!)
targets <- 3:5

## calculate weights
new_weight <- ipu_step(dat$weight, dat$household, targets)
cbind(dat, new_weight)

## check solution
xtabs(new_weight ~ dat$household)

## calculate weights "by reference"
ipu_step_ref(dat$weight, dat$household, targets)
dat

############# multidimensional ipu ##############

## load data
factors <- c("time", "sex", "smoker", "day")
data(tips, package = "reshape2")
tips <- tips[factors]

## combine factors
con <- xtabs(~., tips)
cf <- combine_factors(tips, con)
cbind(tips, cf)[sample(nrow(tips), 10),]

## adjust weights
weight <- rnorm(nrow(tips)) + 5
adjusted_weight <- ipu_step(weight, cf, con)

## check outputs
con2 <- xtabs(adjusted_weight ~ ., data = tips)
sum((con - con2)^2)

statistikat/simPop documentation built on Oct. 20, 2018, 8:06 a.m.