I wanted to make a small example for the explanation of my raking model, but the code below does not seem to work.
I don't understand why it doesn't, since I just did (for all I know) exactly the same a few days ago.
Maybe I overlooked something?
The code I made:
require(pacman)
p_load(tidyverse, anesrake, weights, purrr)
leeftijd <- c(0.35, 0.40, 0.25)
school <- c(0.20, 0.30, 0.35, 0.15)
dat <- data.frame(leeftijd = c('jong', 'middel', 'oud',
'jong', 'middel', 'oud',
'jong', 'middel', 'oud',
'jong', 'middel', 'oud'),
school = c('mbo1-2','mbo1-2','mbo1-2',
'mbo3-4','mbo3-4','mbo3-4',
'hbo','hbo','hbo',
'wo','wo','wo'),
perc = c(0.06,0.06,0.03,
0.08,0.10,0.10,
0.9,0.10,0.9,
0.03,0.14,0.08))
dat$leeftijd <- as.factor(dat$leeftijd)
dat$school <- as.factor(dat$school)
target <- list(leeftijd, school)
names(target) <- c("leeftijd", "school")
levels(target$leeftijd) <- c('jong', 'middel', 'oud')
levels(target$school) <- c('mbo1-2', 'mbo3-4', 'hbo', 'wo')
raking <- anesrake(target,
dat,
dat$caseid,
cap = 10,
choosemethod = "total",
type = "pctlim",
pctlim = 0.05
)
I checked all the classes, structures, you name it. Maybe I forgot something?
If I make R calculate the weights like this, it works:
target <- with(dat, list(
leeftijd = wpct(leeftijd, perc),
school = wpct(school, perc)
))
but these are not the numbers I want. Why doesn't it work with leeftijd
and school
?
Thanks in advance!