The Problem
I have a detailed dataset of 6 variabes, but for all but one year, I only have marginal distributions of 5 variables, the rest is missing. From that, I would like to obtain a full dataset.
To illustrate my problem imagine the case if I had a dataset of 3 variables (Age, Level, Year) and had only 2 margins for all but one year.
2011 (the “full year")
Lvl1 Lvl2 Lvl3 Lvl4 Lvl5 Lvl6 Lvl7 Lvl1-7
1-20 2758 3531 2759 1472 963 1025 790 13298
21-40 2421 3530 2371 1583 1119 1376 720 13120
41-60 3497 3322 1818 1276 710 562 240 11425
61-80 4122 5554 2754 1786 1180 496 282 16174
80+ 2740 5116 3050 2333 1889 656 334 16118
Sum 15538 21053 12752 8450 5861 4115 2366 70135
2010 (only 2 marginals)
Lvl1 Lvl2 Lvl3 Lvl4 Lvl5 Lvl6 Lvl7 Lvl1-7
1-20 13197
21-40 12922
41-60 11369
61-80 16353
80+ 15774
Sum 15151 21643 12611 8273 5586 4026 2325 69615
2009 (only 2 marginals)
Lvl1 Lvl2 Lvl3 Lvl4 Lvl5 Lvl6 Lvl7 Lvl1-7
1-20 12684
21-40 12477
41-60 10844
61-80 16022
80+ 14911
Sum 14367 20999 12201 8032 5235 3841 2263 66938
In this example, the challenge would be to fill in the missing values of the 2010 and 2009 data, using the 2011 distribution to make all necessary assumptions on multivariate marginal distributions.
Note that my problem is somewhat more complex. I have 5 dimensions (Age, Level, Region, Gender, Year) and three 4-dimensional marginal distributions tables (Gender, Level, Region, Year), (Age, Gender, Region, Year) and (Gender, Age, Level, Year) – but the problem is basically the same (I think … ?).
How can that be done?
Here’s my theory. Survey statistics has done a lot of reaserach on how to make any given (sample) data agree with population margins, I could use their methods. They do so by reweighting observations. Raking can be used to obtain a set of such weights. The rake
function in the survey
package allows raking with multidimensional joint distributions.
My idea was to use the full data as “sample”, and let the rake function compute weights that make the data agree with the marginal distributions.
Question 1: Is that a good idea? Do you have a better idea?
As you can imagine, there are several other problems to be solved. First one is that survey methods always assume that the “sample” data is individual data, I have counts, and no other characteristics of the individuals other than the stratification variables.
What I tried to do is specifying the counts as pre-raking survey weights. I tried that but the raking results in unplausible weights, that is, most weights are 0 and some are Inf.
Another possibility would be to duplicate every entry of the “population” data.frame depending on the number of observations, but that would bloat the data.frame significantly (it would have approximately 18 000 000 rows instead of currently 24 480 (the product of all dimension sizes) and cause memory problems.
If I specify no weights (all probabilities are 1), I get the following distribution of weights after raking:
> mean(lpg.rake$prob)
[1] 0.1289419
> median(lpg.rake$prob)
[1] 0.01700375
> quantile(lpg.rake$prob,c(.075,.1,.5,.9,.95,.99,.999))
7.5% 10% 50% 90% 95% 99% 99.9%
0.001274434 0.001697273 0.017003749 0.155742479 0.322445467 1.597854446 11.711632910
Given that the mean of frequencies in the one year for which I have a full dataset is around 700, I would expect that to be the average of the weights.
Question 2: How should I deal with the fact that survey methods assume that observations are individuals, whereas I have one row for every combination of stratification variables plus a “count” variable?
A working example
Since my problem is very complex, and I am not familiar with the survey
package, it is difficult for me to produce a minimal example that reproduces my problem. In some way, the complexity is the main problem. There is the example data in the survey
package, you could take a look at that (link).
However, since my data is not secret, I don’t see why I could not give you the full dataset and what I have tried so far. Here it is.
The password for the zip file is: xB2KzbuK
Source: Ministry for Social Affairs (1995-2012): Pflegevorsorgebericht / Bericht des Arbeitskreises für Pflegevorsorge. Vienna, Austria AND own calculations.
Consequently, question 3 is: Is there an error in my code? What could I do in order to get meaningful results?