# NOT RUN {
############# 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 <- ipf_step(dat$weight, dat$household, targets)
cbind(dat, new_weight)
## check solution
xtabs(new_weight ~ dat$household)
## calculate weights "by reference"
ipf_step_ref(dat$weight, dat$household, targets)
dat
############# 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),]
## 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)
# }
Run the code above in your browser using DataLab