# Correcting multiple marginals in a subsample of a survey

My question in short: How to assign weights to a subsample of a survey in order to fit multiple features simultaneously to their original marginals?

...and now the details: I have some data set X of n=10000 sample points with 100 features (age, gender, citysize, online-hours, number of siblings, ...). The sample is representative with respect to age, gender, and citysize (that is, the marginals of age, gender, and citysize give the real one). Now I partition the sample X into two parts X1 and X2, depending on some criterion. It turns out that, restricted to only X1 or X2 alone, the marginals age, gender, and citysize do no longer give the original distribution. I want to correct for this by assigning appropriate weights to the sample points, such that including the weights, all three representative factors (age, gender, and citysize) give again the original marginal distribution inside X1 and X2, respectively.

This is fairly easy if you only correct for a single factor, say for the age=1,2,...,10:

1. create the age-histograms for X1 and X2, respectively (which deviate from the "true" histograms of X)
2. assign the weight X.agehist[i] / X1.agehist[i] to each sample in X1 of age i
3. assign the weight X.agehist[i] / X2.agehist[i] to each sample in X2 of age i

Then, including the weights, the marginals of X1 and X2 give the same original distribution as the marginals of X.

The problem now is, that I want to correct for multiple factors, so finding proper weights seems to be more tricky. Are there any standard techniques for doing so ?

The procedure is called "calibration", and one special algorithm to perform it is called "raking". Essentially, you cycle over the margins adjusting each one until the weights converge. In R, it is implemented by rake() function in survey package. In Stata, I implemented it in ipfraking package.