Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: multiple margins support #33

Closed
vkhodygo opened this issue Feb 8, 2024 · 3 comments
Closed

FR: multiple margins support #33

vkhodygo opened this issue Feb 8, 2024 · 3 comments
Assignees

Comments

@vkhodygo
Copy link

vkhodygo commented Feb 8, 2024

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
Copy link
Collaborator

@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
Copy link
Collaborator

Added more explanation to the help page: 1b40ecf

@vkhodygo
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants