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 ipf_step
functions. combine_factors
aggregates several vectors of
type factor into a single one to allow multidimensional ipu-steps. See examples.
Usage
ipf_step_ref(w, classes, targets)
ipf_step(w, classes, targets)
ipf_step_f(w, classes, targets)
combine_factors(dat, targets)
Arguments
- w
a numeric vector of weights. All entries should be positive.
- classes
a factor variable. Must have the same length as
w
.- targets
key figure to target with the ipu scheme. A numeric verctor of the same length as
levels(classes)
. This can also be atable
produced byxtabs
. See examples.- dat
a
data.frame
containing the factor variables to be combined.
Details
ipf_step
returns the adjusted weights. ipf_step_ref
does the same, but updates w
by reference rather than
returning. ipf_step_f
returns a multiplicator: adjusted weights divided by unadjusted weights. combine_factors
is
designed to make ipf_step
work with contingency tables produced by xtabs.
Examples
############# 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
#> weight household
#> 1 1.2602534 b
#> 2 1.1473186 a
#> 3 0.9549704 a
#> 4 1.9733603 a
#> 5 2.9270075 a
#> 6 0.3151157 c
#> 7 0.3069338 b
#> 8 1.3636635 c
#> 9 0.7147929 c
#> 10 0.4183751 b
## create targets (same lenght as classLabels!)
targets <- 3:5
## calculate weights
new_weight <- ipf_step(dat$weight, dat$household, targets)
cbind(dat, new_weight)
#> weight household new_weight
#> 1 1.2602534 b 2.5388342
#> 2 1.1473186 a 0.4915214
#> 3 0.9549704 a 0.4091178
#> 4 1.9733603 a 0.8454050
#> 5 2.9270075 a 1.2539559
#> 6 0.3151157 c 0.6582540
#> 7 0.3069338 b 0.6183313
#> 8 1.3636635 c 2.8485949
#> 9 0.7147929 c 1.4931510
#> 10 0.4183751 b 0.8428345
## check solution
xtabs(new_weight ~ dat$household)
#> dat$household
#> a b c
#> 3 4 5
## calculate weights "by reference"
ipf_step_ref(dat$weight, dat$household, targets)
dat
#> weight household
#> 1 2.5388342 b
#> 2 0.4915214 a
#> 3 0.4091178 a
#> 4 0.8454050 a
#> 5 1.2539559 a
#> 6 0.6582540 c
#> 7 0.6183313 b
#> 8 2.8485949 c
#> 9 1.4931510 c
#> 10 0.8428345 b
############# multidimensional ipu ##############
## load data
factors <- c("time", "sex", "smoker", "day")
tips <- data.frame(sex=c("Female","Male","Male"), day=c("Sun","Mon","Tue"),
time=c("Dinner","Lunch","Lunch"), smoker=c("No","Yes","No"))
tips <- tips[factors]
## combine factors
con <- xtabs(~., tips)
cf <- combine_factors(tips, con)
cbind(tips, cf)[sample(nrow(tips), 10, replace = TRUE),]
#> time sex smoker day cf
#> 2 Lunch Male Yes Mon 8
#> 2.1 Lunch Male Yes Mon 8
#> 3 Lunch Male No Tue 20
#> 1 Dinner Female No Sun 9
#> 2.2 Lunch Male Yes Mon 8
#> 2.3 Lunch Male Yes Mon 8
#> 2.4 Lunch Male Yes Mon 8
#> 1.1 Dinner Female No Sun 9
#> 1.2 Dinner Female No Sun 9
#> 3.1 Lunch Male No Tue 20
## adjust weights
weight <- rnorm(nrow(tips)) + 5
adjusted_weight <- ipf_step(weight, cf, con)
## check outputs
con2 <- xtabs(adjusted_weight ~ ., data = tips)
sum((con - con2)^2)
#> [1] 0