statistikat / simPop

Simulation of Synthetic Populations for Survey Data Considering Auxiliary Information
30 stars 7 forks source link

FR: multiple margins support #33

Closed vkhodygo closed 5 months ago

vkhodygo commented 5 months ago

It is my understanding that at the moment the simulated annealing part supports at most two tables: one for individuals, and the other one is for households.

Whereas it is possible to create and pass a single contingency table, its size in certain cases is absolutely off the charts. It is also impossible sometimes to have one distribution only: census targets are provided as anonymised aggregated distributions.

The question is what's needed to support multiple marginal tables at this stage.

JohannesGuss commented 5 months ago

@vkhodygo: It is already possible to supply multiple margins both for tables regarding number of households and tables regarding number of individuals. The current documentation of calibPop() is however not sufficient in explaining this. Thanks for pointing this out, we will improve it as soon as possible.

Supplying a list of tables to either (or both) hhTables or persTables is supported.

Here is some example code for using two tables for margins on individual level - it might take some minutes to run:

library(simPop)
library(data.table)

data(eusilcS) # load sample data
data(eusilcP) # population data

setDT(eusilcP)
setDT(eusilcS)

# init simPop object
inp <- specifyInput(data=eusilcS, hhid="db030", hhsize="hsize", strata="db040", weight="db090")
simPop <- simStructure(data=inp, method="direct", basicHHvars=c("age", "rb090"))
simPop <- simCategorical(simPop, additional=c("pl030", "pb220a"), method="multinom", nr_cpus=1)

# multiple person margins
perstab1 <- eusilcP[,.(Freq=.N), by=.(db040 = region, rb090 = gender)]
perstab2 <- eusilcP[,.(Freq=.N), by=.(db040 = region, pb220a = citizenship)]
persTables <- list(perstab1,perstab2)

simPop_adj <- calibPop(simPop, split="db040", 
                         temp=1, epsP.factor=0.1,
                         persTables = persTables,
                         nr_cpus = 1)

# compare with using joint table for person margins
persTables2 <- eusilcP[,.(Freq=.N), by=.(db040 = region, rb090 = gender,
                                         pb220a = citizenship)]
simPop_adj_2 <- calibPop(simPop, split="db040", 
                       temp=1, epsP.factor=0.1,
                       persTables = persTables2,
                       nr_cpus = 1)

# compare margins
calibtab1 <- simPop_adj@pop@data[,.(N1=.N),by=.(db040,rb090)]
calibtab2 <- simPop_adj_2@pop@data[,.(N2=.N),by=.(db040,rb090)]
perstab1[calibtab1,N1:=N1, on=.(db040, rb090)]
perstab1[calibtab2,N2:=N2, on=.(db040, rb090)]

print(perstab1)

The same will work for supplying multiple table for margins on households. The number of tables you want to supply is not restricted. Each table however needs have variables which are coded in the exact the way as is in the synthetic population. There cannot be any missing values or values of variable which only exist in the synthetic population or the supplied margin and vice versa. This is checked before the simulated annealing starts and will throw an error.

JohannesGuss commented 5 months ago

Added more explanation to the help page: 1b40ecf

vkhodygo commented 5 months ago

Much appreciated, I'd like to keep the issue open for a bit in the case I get more questions.