statistikat / simPop

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

Error "attempt to select less than one element in get1index" #18

Closed Kyoshido closed 2 years ago

Kyoshido commented 2 years ago

Hello, I have this error

Error in { : 
  task 1 failed - "attempt to select less than one element in get1index"

Problem occurred during function simCategorical() with model method = "distribution". The problem lies in other function generateValues_distribution.

Error "attempt to select less than one element in get1index" occurs when we use NULL as an index. There is following code:

pSplit <- lapply(splitS, function(i) {
    tmp <- tableWt(dataSample[i, 
                              additional, 
                              with=FALSE], 
                   dataSample[[w]][i])
    tmp <- as.data.frame(tmp)
    p <- ncol(tmp)
    tmp[, p]/sum(tmp[, p])
  })

And the problem is in dataSample[[w]][i]) Because I want to model whole population, my weights are NULL. And the w = NULL cause an error. This happened because in specifyInput.R the function is set with weight=NULL and from then the NULL travels.

 simPopObj <- function(data, hhid, hhsize=NULL, pid=NULL, weight=NULL,
                         strata=NULL, population=FALSE)
 dataS <- sampleObj(simPopObj)
 params$w <- dataS@weight
 w <- params$w
 dataSample[[w]][i])
alexkowa commented 2 years ago

Yes, the "distribution"-method gives this error, we will work on a fix. In the meantime, the other methods in simCategorical work fine.

Reproduced:

library(simPop)
data(eusilcS)
inp <- specifyInput(data=eusilcS, hhid="db030", hhsize="hsize", strata="db040", population = TRUE)
## in the following, nr_cpus are selected automatically
simPop <- simStructure(data=inp, method="direct", basicHHvars=c("age", "rb090"))
simPop <- simCategorical(simPop, additional=c("pl030", "pb220a"),
                         method="distribution", nr_cpus=1)