I have a survey data set and some quotes:
The population quotes are:
(1 = up to 29 years 0,00%)
2 = 30 to 39 years 18,10%
3 = 40 to 49 years 28,77%
4 = 50 to 59 years 33,11%
5 = 60 and more years 20,01%
In the data set, I have to weight category 5 is missing. Here are the stats of the variable in the data set:
2 = 32,33%
3 = 36,56%
4 = 31,12%
If I perform the raking I get the following error:
library(anesrake)
r = anesrake(list_weights,
d,
verbose = FALSE,
caseid = d$RESPID,
maxit = 1500,
cap = 5,
choosemethod = "max",
type = "nolim")
Error in rakeonvar.default(mat[, i], inputter[[i]], weightvec) : variables must be coded continuously from 1 to n with no missing values
Any Idea how to deal with missing levels in the data?
Here is a dput of the quotes
list(Rec_Age = c(`2` = 0.181, `3` = 0.2877, `4` = 0.3311))
and a small dput of the data
structure(list(RESPID = structure(c(459, 311, 223, 60, 613, 495,
300, 273, 78, 170, 217, 61, 175, 619, 270, 218, 453, 492, 23,
65, 33, 113, 532, 26, 119, 49, 208, 102, 200, 165, 435, 298,
593, 220, 111, 53, 494, 271, 305, 420, 323, 607, 105, 19, 426,
171, 330, 201, 332, 277), label = "RESPID - Respondent ID", format.spss = "F10.0", display_width = 0L),
Rec_Age = structure(c(4, 2, 4, 3, 4, 4, 4, 3, 2, 2, 3, 2,
3, 4, 4, 2, 4, 4, 2, 3, 2, 2, 2, 3, 3, 2, 2, 2, 2, 3, 2,
3, 2, 3, 4, 3, 4, 3, 2, 3, 3, 3, 4, 4, 4, 2, 2, 3, 4, 3), label = "Rec_Age - Recode Age")), row.names = c(NA,
-50L), class = "data.frame")
@Yuriy Saraykin
You are right there ist no error right now but all weights are at 1 after raking if I use your code. So something did must go wrong.
I don´t understand the reason for this. If I use the list with all levels like you I get this error (I tried it before).
Error in rakeonvar.default(mat[, i], inputter[[i]], weightvec) : you cannot rake any variable category to 0 or a negative number
What is the difference between your list and mine (even if your code don´t provide the desired result)?
Your list:
your_list
[[1]]
1 2 3 4 5
0.0000000 0.1810181 0.2877288 0.3311331 0.2001200
dput(your_list)
list(Rec_Age = c(`1` = 0, `2` = 0.181, `3` = 0.2877, `4` = 0.3311,
`5` = 0.2001))
My list:
My_list
my_list:
$Rec_Age
1 2 3 4 5
0.0000 0.1810 0.2877 0.3311 0.2001
dput(my_list)
list(Rec_Age = c(`1` = 0, `2` = 0.181, `3` = 0.2877, `4` = 0.3311, `5` =
0.2001))
My list was generated like:
REC_age = c(0, 0.181, 0.2877, 0.3311, 0.2001)
names(REC_age) = c(1, 2, 3, 4, 5)
Try it like this. It seems to me that you can include information about the population in the sample. Here is a good article on the topic. https://www.r-bloggers.com/survey-raking-an-illustration/
library(anesrake)
library(weights)
library(tidiverse)
d <- d %>% mutate(Rec_Age = as.factor(Rec_Age))
population <- data.frame(Rec_Age = c("2", "3", "4"),
fraction = c(0.181, 0.2877, 0.3311))
list_weights <- with(population,
list(Rec_Age = wpct(Rec_Age, fraction)))
r <- anesrake(list_weights,
d,
caseid = d$RESPID,
maxit = 1500,
cap = 5,
choosemethod = "max",
type = "nolim")