Skip to content

Commit

Permalink
adopt test files
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Gussenbauer - QM committed Nov 28, 2023
1 parent 701228c commit a1ce58e
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 270 deletions.
147 changes: 0 additions & 147 deletions inst/tinytest/simInitSpatialTest.R

This file was deleted.

113 changes: 51 additions & 62 deletions inst/tinytest/test_calibPop.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,55 @@
#
if(Sys.info()["user"]%in%c("kowarik","gussenbauer")){


message("calibPop")
library(simPop)

data(eusilcS) # load sample data
data(eusilcP) # population data
#Reduce data to 2 states for computation times
eusilcP <- eusilcP[eusilcP$region%in%c("Vorarlberg","Tyrol"),]
eusilcS <- eusilcS[eusilcS$db040%in%c("Vorarlberg","Tyrol"),]
eusilcP$region <- as.factor(as.character(eusilcP$region))
eusilcS$db040 <- as.factor(as.character(eusilcS$db040))


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)

# add margins
margins <- as.data.frame(
xtabs(rep(1, nrow(eusilcP)) ~ eusilcP$region + eusilcP$gender + eusilcP$citizenship))
colnames(margins) <- c("db040", "rb090", "pb220a", "freq")
simPop <- addKnownMargins(simPop, margins)

# Test CalibPop - Comparison memory intense vs. memory saving method",{
simPop_adj <- calibPop(simPop, split="db040", temp=1, eps.factor=0.01)
simPop_adj2 <- calibPop(simPop, split="db040", temp=1, nr_cpus = 1,eps.factor=0.01,memory=TRUE)
expect_equal(simPop_adj2@table[,sum(N)],simPop_adj@table[,sum(N)])
#

# Test CalibPop - check temp.factor",{
#simPop_adj <- calibPop(simPop, split="db040", temp=1, eps.factor=0.001,memory=TRUE,choose.temp.factor = .9)
simPop_adj <- calibPop(simPop, split="db040", temp=1, eps.factor=0.1,memory=TRUE,choose.temp.factor = .5)
expect_true(abs(simPop_adj@table[,sum(N)]-sum(margins$freq))<1)
#

# Test CalibPop - check sizefactor",{
simPop_adj <- calibPop(simPop, split="db040", temp=1, eps.factor=0.1,memory=TRUE,sizefactor = 5)
expect_true(abs(simPop_adj@table[,sum(N)]-sum(margins$freq))<1)
#

# Test CalibPop - check scale.redraw",{
simPop_adj <- calibPop(simPop, split="db040", temp=1, eps.factor=0.1,memory=TRUE,sizefactor = 5,scale.redraw = .2)
#simPop_adj <- calibPop(simPop, split="db040", temp=1, eps.factor=0.1,memory=TRUE,sizefactor = 5,scale.redraw = .8)
expect_true(abs(simPop_adj@table[,sum(N)]-sum(margins$freq))<1)
#

# Test CalibPop - check observe.break",{
simPop_adj <- calibPop(simPop, split="db040", temp=1, eps.factor=0.1,memory=TRUE,sizefactor = 5,observe.break = 0)
expect_true(abs(simPop_adj@table[,sum(N)]-sum(margins$freq))<1)
#simPop_adj <- calibPop(simPop, split="db040", temp=1, eps.factor=0.1,memory=TRUE,sizefactor = 5,observe.break = .3)
#

# Test CalibPop - check observe.times",{
simPop_adj <- calibPop(simPop, split="db040", temp=1, eps.factor=0.1,memory=TRUE,sizefactor = 5,observe.times=10)
#simPop_adj <- calibPop(simPop, split="db040", temp=1, eps.factor=0.1,memory=TRUE,sizefactor = 5,observe.times=0)
#simPop_adj <- calibPop(simPop, split="db040", temp=1, eps.factor=0.1,memory=TRUE,sizefactor = 5,observe.times=10,observe.break = 0.01)
#simPop_adj <- calibPop(simPop, split="db040", temp=1, eps.factor=0.1,memory=TRUE,sizefactor = 5,observe.times=10,observe.break = .5)
expect_true(abs(simPop_adj@table[,sum(N)]-sum(margins$freq))<1)
simPop_adj <- calibPop(simPop, split="db040", temp=1, eps.factor=0.1,memory=TRUE,sizefactor = 5,observe.times=10,observe.break = .5)
expect_true(abs(simPop_adj@table[,sum(N)]-sum(margins$freq))<1)
#


message("calibPop")
library(simPop)

data(eusilcS) # load sample data
data(eusilcP) # population data
#Reduce data to 2 states for computation times
eusilcP <- eusilcP[eusilcP$region%in%c("Vorarlberg","Tyrol"),]
eusilcS <- eusilcS[eusilcS$db040%in%c("Vorarlberg","Tyrol"),]
eusilcP$region <- as.factor(as.character(eusilcP$region))
eusilcS$db040 <- as.factor(as.character(eusilcS$db040))


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)

# add margins
margins <- as.data.frame(
xtabs(rep(1, nrow(eusilcP)) ~ eusilcP$region + eusilcP$gender + eusilcP$citizenship))
colnames(margins) <- c("db040", "rb090", "pb220a", "Freq")
simPop <- addKnownMargins(simPop, margins)


# Test CalibPop - check temp.factor",{
simPop_adj <- calibPop(simPop, split="db040", temp=1, epsP.factor=0.1,choose.temp.factor = .5)
expect_true(abs(simPop_adj@table[,sum(Freq)]-sum(margins$Freq))<1)
#

# Test CalibPop - check sizefactor",{
simPop_adj <- calibPop(simPop, split="db040", temp=1, epsP.factor=0.1,sizefactor = 5)
expect_true(abs(simPop_adj@table[,sum(Freq)]-sum(margins$Freq))<1)
#

# Test CalibPop - check scale.redraw",{
simPop_adj <- calibPop(simPop, split="db040", temp=1, epsP.factor=0.1,sizefactor = 5,scale.redraw = .2)
expect_true(abs(simPop_adj@table[,sum(Freq)]-sum(margins$Freq))<1)
#

# Test CalibPop - check observe.break",{
simPop_adj <- calibPop(simPop, split="db040", temp=1, epsP.factor=0.1,sizefactor = 5,observe.break = 0)
expect_true(abs(simPop_adj@table[,sum(Freq)]-sum(margins$Freq))<1)
#

# Test CalibPop - check observe.times",{
simPop_adj <- calibPop(simPop, split="db040", temp=1, epsP.factor=0.1,sizefactor = 5,observe.times=10)
expect_true(abs(simPop_adj@table[,sum(Freq)]-sum(margins$Freq))<1)
simPop_adj <- calibPop(simPop, split="db040", temp=1, epsP.factor=0.1,sizefactor = 5,observe.times=10,observe.break = .5)
expect_true(abs(simPop_adj@table[,sum(Freq)]-sum(margins$Freq))<1)
#

}
14 changes: 7 additions & 7 deletions inst/tinytest/test_cforest.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
run_tests <- length(strsplit(as.character(packageVersion("simPop")), "[.]")[[1]]) > 3
if(run_tests){
#Test for cforest implementation
library(simPop)

# cforest integration tests",{
#Test for cforest implementation
library(simPop)
# cforest integration tests",{

data("eusilc13puf")
data("totalsRGtab")
Expand All @@ -29,8 +29,8 @@ library(simPop)
additional=c("pl031", "pb220a"),
method="cforest",
nr_cpus = 1)

expect_true(nrow(simPop@pop@data)>0,
"Expected generated synthetic population to have some rows")
#
"Expected generated synthetic population to have some rows")
#
}
14 changes: 7 additions & 7 deletions inst/tinytest/test_correctHeaps.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
run_tests <- length(strsplit(as.character(packageVersion("simPop")), "[.]")[[1]]) > 3
if(run_tests){
message("heap")
library(simPop)
# correctHeaps",{
message("heap")
library(simPop)
# correctHeaps",{
## create some artificial data
age <- rlnorm(10000, meanlog=2.466869, sdlog=1.652772)
age <- round(age[age < 93])
Expand Down Expand Up @@ -36,9 +36,9 @@ library(simPop)

expect_identical(cs10f[i1],age10[i1])
expect_identical(cs5f[i2],age5[i2])
#

# correctSingleHeap",{
#
# correctSingleHeap",{
## create some artificial data
age <- rlnorm(10000, meanlog=2.466869, sdlog=1.652772)
age <- round(age[age < 93])
Expand All @@ -59,5 +59,5 @@ library(simPop)
i <- sample(1:length(age23),5)
csf <- correctSingleHeap(age23, heap=23, before=5, after=5, method="lnorm", fixed=i)
expect_identical(csf[i],age23[i])
#
#
}
12 changes: 6 additions & 6 deletions inst/tinytest/test_crossValidation.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
run_tests <- length(strsplit(as.character(packageVersion("simPop")), "[.]")[[1]]) > 3
if(run_tests){
#Test for xgboost implementation
library(simPop)

# xgboost integration tests",{
#Test for xgboost implementation
library(simPop)
# xgboost integration tests",{

data(eusilcS) # load sample data

Expand All @@ -23,6 +23,6 @@ library(simPop)
verbose = TRUE, hyper_param_grid = grid)

expect_true(nrow(simPop@pop@data)> 0,
"Expected generated synthetic population to have some rows")
#
"Expected generated synthetic population to have some rows")
#
}
File renamed without changes.
Loading

0 comments on commit a1ce58e

Please sign in to comment.