Skip to content
Permalink
Browse files

weights and all constraints are checked in ipf

  • Loading branch information
Johannes Gussenbauer - QM
Johannes Gussenbauer - QM committed Jan 24, 2020
1 parent 25a4595 commit 33fba8dda758160e733c753ca31fdc23c11ac3be
Showing with 75 additions and 5 deletions.
  1. +42 −5 R/ipf.r
  2. +33 −0 tests/testthat/test_ipf.R
47 R/ipf.r
@@ -78,14 +78,51 @@ boundsFakHH <- function(g1, g0, eps, orig, p, bound = 4) {
return(g1)
}

check_population_totals <- function(con, dat, type = "personal") {
# do not apply this check for numerical calibration
check_population_totals <- function(con, dat, w = NULL, type = "personal") {

# check weights
if(!is.null(w)){
if(!w%in%colnames(dat)){
stop("Base weight ",w," is not a column name in dat")
}
if(any(is.na(dat[[w]]))){
stop("Base weight ",w," contains missing values")
}
if(!is.numeric(dat[[w]])){
stop("Base weight ",w," must be a numeric column")
}
}

# check constraints for non numerical calibration
# and numerical calibration
if (is.null(names(con))) {
ind <- seq_along(con)
indNum <- NULL
} else {
ind <- which(names(con) == "")
indNum <- which(names(con) != "")
}



# check constraints for numerical calibration
if(!is.null(indNum)){
namesNum <- names(con)[indNum]

if(any(!namesNum %in% colnames(dat))){
stop("Numerical constraints must be named by variables in dat")
}

numNA <- dat[,lapply(.SD,function(z){any(is.na(z))}),.SDcols=c(namesNum)]
numNA <- unlist(numNA)
numNA <- names(numNA)[numNA]
if(length(numNA)>0){
mult <- (length(numNA)>1)+1
stop("Numeric variable",c(" ","s ")[mult],
paste(numNA,collapse=", "),
" contain",c("s "," ")[mult],"missing values")
}
}

# do not apply this check for constraints that only cover the population
# partially
ind <- ind[vapply(
@@ -542,8 +579,8 @@ ipf <- function(
computeLinear, check_hh_vars = TRUE, conversion_messages = FALSE,
nameCalibWeight = "calibWeight") {

check_population_totals(conP, dat, "personal")
check_population_totals(conH, dat, "household")
check_population_totals(conP, dat, w = w, type = "personal")
check_population_totals(conH, dat, w = w, type = "household")
variableKeepingTheBaseWeight <- w
variableKeepingTheCalibWeight <- nameCalibWeight
if ("variableKeepingTheBaseWeight" %in% names(dat))
@@ -32,6 +32,7 @@ epsH1 <- conH1
epsH1[1:4, ] <- 0.005
epsH1[5, ] <- 0.2


test_that("ipf with a numerical variable works as expected - computeLinear", {
# without array epsP1
calibweights1 <- ipf(
@@ -138,3 +139,35 @@ test_that("ipf works as expected calibWeight renamed", {
subset = !duplicated(hid)) - conH1) / conH1)))
expect_true(err < .01)
})



test_that("ipf errors work as expected", {

expect_error(ipf(
eusilc, hid = "hid", conP = list(conP1, conP2),
w = "baseWeight9"),
"Base weight baseWeight9 is not a column name in dat")

expect_error(ipf(
eusilc, hid = "hid", conP = list(conP1, eqincome = conP3),
w = "baseWeight"),
"Numerical constraints must be named by variables in dat")

setNA <- eusilc[,sample(hid,1),by=.(hsize,region)]
eusilc[.(hid=setNA$V1),eqIncome:=NA,on=.(hid)]

expect_error(ipf(
eusilc, hid = "hid", conP = list(conP1, eqIncome = conP3),
w = "baseWeight"),
"Numeric variable eqIncome contains missing values")

setNA <- eusilc[,sample(hid,1),by=.(hsize,region)]
eusilc[.(hid=setNA$V1),baseWeight:=NA,on=.(hid)]

expect_error(ipf(
eusilc, hid = "hid", conP = list(conP1, eqIncome = conP3),
w = "baseWeight"),
"Base weight baseWeight contains missing values")
})

1 comment on commit 33fba8d

@lintr-bot

This comment has been minimized.

Copy link

@lintr-bot lintr-bot commented on 33fba8d Jan 24, 2020

R/ipf.r:82:1: style: Trailing whitespace is superfluous.

^~

R/ipf.r:84:5: style: Place a space before left parenthesis, except in a function call.

if(!is.null(w)){
    ^

R/ipf.r:84:17: style: There should be a space between right parenthesis and an opening curly brace.

if(!is.null(w)){
                ^~

R/ipf.r:85:7: style: Place a space before left parenthesis, except in a function call.

if(!w%in%colnames(dat)){
      ^

R/ipf.r:85:10: style: Put spaces around all infix operators.

if(!w%in%colnames(dat)){
        ~^~~~~

R/ipf.r:85:27: style: There should be a space between right parenthesis and an opening curly brace.

if(!w%in%colnames(dat)){
                          ^~

R/ipf.r:86:27: style: Commas should always have a space after.

​      stop("Base weight ",w," is not a column name in dat")
                          ^

R/ipf.r:86:29: style: Commas should always have a space after.

​      stop("Base weight ",w," is not a column name in dat")
                            ^

R/ipf.r:88:7: style: Place a space before left parenthesis, except in a function call.

if(any(is.na(dat[[w]]))){
      ^

R/ipf.r:88:28: style: There should be a space between right parenthesis and an opening curly brace.

if(any(is.na(dat[[w]]))){
                           ^~

R/ipf.r:89:27: style: Commas should always have a space after.

​      stop("Base weight ",w," contains missing values")
                          ^

R/ipf.r:89:29: style: Commas should always have a space after.

​      stop("Base weight ",w," contains missing values")
                            ^

R/ipf.r:91:7: style: Place a space before left parenthesis, except in a function call.

if(!is.numeric(dat[[w]])){
      ^

R/ipf.r:91:29: style: There should be a space between right parenthesis and an opening curly brace.

if(!is.numeric(dat[[w]])){
                            ^~

R/ipf.r:92:27: style: Commas should always have a space after.

​      stop("Base weight ",w," must be a numeric column")
                          ^

R/ipf.r:92:29: style: Commas should always have a space after.

​      stop("Base weight ",w," must be a numeric column")
                            ^

R/ipf.r:95:1: style: Trailing whitespace is superfluous.

^~

R/ipf.r:105:1: style: Trailing whitespace is superfluous.

^~

R/ipf.r:106:1: style: Trailing whitespace is superfluous.

^~

R/ipf.r:108:5: style: Place a space before left parenthesis, except in a function call.

if(!is.null(indNum)){
    ^

R/ipf.r:108:22: style: There should be a space between right parenthesis and an opening curly brace.

if(!is.null(indNum)){
                     ^~

R/ipf.r:110:1: style: Trailing whitespace is superfluous.

^~~~

R/ipf.r:111:7: style: Place a space before left parenthesis, except in a function call.

if(any(!namesNum %in% colnames(dat))){
      ^

R/ipf.r:111:41: style: There should be a space between right parenthesis and an opening curly brace.

if(any(!namesNum %in% colnames(dat))){
                                        ^~

R/ipf.r:114:1: style: Trailing whitespace is superfluous.

^~~~

R/ipf.r:115:19: style: Commas should always have a space after.

numNA <- dat[,lapply(.SD,function(z){any(is.na(z))}),.SDcols=c(namesNum)]
                  ^

R/ipf.r:115:30: style: Commas should always have a space after.

numNA <- dat[,lapply(.SD,function(z){any(is.na(z))}),.SDcols=c(namesNum)]
                             ^

R/ipf.r:115:40: style: There should be a space between right parenthesis and an opening curly brace.

numNA <- dat[,lapply(.SD,function(z){any(is.na(z))}),.SDcols=c(namesNum)]
                                       ^~

R/ipf.r:115:41: style: Opening curly braces should never go on their own line and should always be followed by a new line.

numNA <- dat[,lapply(.SD,function(z){any(is.na(z))}),.SDcols=c(namesNum)]
                                        ^

R/ipf.r:115:58: style: Commas should always have a space after.

numNA <- dat[,lapply(.SD,function(z){any(is.na(z))}),.SDcols=c(namesNum)]
                                                         ^

R/ipf.r:115:65: style: Put spaces around all infix operators.

numNA <- dat[,lapply(.SD,function(z){any(is.na(z))}),.SDcols=c(namesNum)]
                                                               ~^~

R/ipf.r:118:7: style: Place a space before left parenthesis, except in a function call.

if(length(numNA)>0){
      ^

R/ipf.r:118:21: style: Put spaces around all infix operators.

if(length(numNA)>0){
                   ~^~

R/ipf.r:118:23: style: There should be a space between right parenthesis and an opening curly brace.

if(length(numNA)>0){
                      ^~

R/ipf.r:119:29: style: Put spaces around all infix operators.

mult <- (length(numNA)>1)+1
                           ~^~

R/ipf.r:119:32: style: Put spaces around all infix operators.

mult <- (length(numNA)>1)+1
                              ~^~

R/ipf.r:120:31: style: Commas should always have a space after.

​      stop("Numeric variable",c(" ","s ")[mult],
                              ^

R/ipf.r:120:37: style: Commas should always have a space after.

​      stop("Numeric variable",c(" ","s ")[mult],
                                    ^

R/ipf.r:121:24: style: Commas should always have a space after.

​           paste(numNA,collapse=", "),
                       ^

R/ipf.r:121:32: style: Put spaces around all infix operators.

​           paste(numNA,collapse=", "),
                              ~^~

R/ipf.r:122:23: style: Commas should always have a space after.

" contain",c("s "," ")[mult],"missing values")
                      ^

R/ipf.r:122:30: style: Commas should always have a space after.

" contain",c("s "," ")[mult],"missing values")
                             ^

R/ipf.r:122:41: style: Commas should always have a space after.

" contain",c("s "," ")[mult],"missing values")
                                        ^

R/ipf.r:125:1: style: Trailing whitespace is superfluous.

^~

tests/testthat/test_ipf.R:146:1: style: Trailing whitespace is superfluous.

^~

tests/testthat/test_ipf.R:151:1: style: Trailing whitespace is superfluous.

^~

tests/testthat/test_ipf.R:156:1: style: Trailing whitespace is superfluous.

^~

tests/testthat/test_ipf.R:157:20: style: Commas should always have a space after.

setNA <- eusilc[,sample(hid,1),by=.(hsize,region)]
                   ^

tests/testthat/test_ipf.R:157:31: style: Commas should always have a space after.

setNA <- eusilc[,sample(hid,1),by=.(hsize,region)]
                              ^

tests/testthat/test_ipf.R:157:34: style: Commas should always have a space after.

setNA <- eusilc[,sample(hid,1),by=.(hsize,region)]
                                 ^

tests/testthat/test_ipf.R:157:36: style: Put spaces around all infix operators.

setNA <- eusilc[,sample(hid,1),by=.(hsize,region)]
                                  ~^~

tests/testthat/test_ipf.R:157:45: style: Commas should always have a space after.

setNA <- eusilc[,sample(hid,1),by=.(hsize,region)]
                                            ^

tests/testthat/test_ipf.R:158:15: style: Put spaces around all infix operators.

eusilc[.(hid=setNA$V1),eqIncome:=NA,on=.(hid)]
             ~^~

tests/testthat/test_ipf.R:158:26: style: Commas should always have a space after.

eusilc[.(hid=setNA$V1),eqIncome:=NA,on=.(hid)]
                         ^

tests/testthat/test_ipf.R:158:34: style: Put spaces around all infix operators.

eusilc[.(hid=setNA$V1),eqIncome:=NA,on=.(hid)]
                                ~^~~

tests/testthat/test_ipf.R:158:39: style: Commas should always have a space after.

eusilc[.(hid=setNA$V1),eqIncome:=NA,on=.(hid)]
                                      ^

tests/testthat/test_ipf.R:158:41: style: Put spaces around all infix operators.

eusilc[.(hid=setNA$V1),eqIncome:=NA,on=.(hid)]
                                       ~^~

tests/testthat/test_ipf.R:159:1: style: Trailing whitespace is superfluous.

^~

tests/testthat/test_ipf.R:164:1: style: Trailing whitespace is superfluous.

^~

tests/testthat/test_ipf.R:165:20: style: Commas should always have a space after.

setNA <- eusilc[,sample(hid,1),by=.(hsize,region)]
                   ^

tests/testthat/test_ipf.R:165:31: style: Commas should always have a space after.

setNA <- eusilc[,sample(hid,1),by=.(hsize,region)]
                              ^

tests/testthat/test_ipf.R:165:34: style: Commas should always have a space after.

setNA <- eusilc[,sample(hid,1),by=.(hsize,region)]
                                 ^

tests/testthat/test_ipf.R:165:36: style: Put spaces around all infix operators.

setNA <- eusilc[,sample(hid,1),by=.(hsize,region)]
                                  ~^~

tests/testthat/test_ipf.R:165:45: style: Commas should always have a space after.

setNA <- eusilc[,sample(hid,1),by=.(hsize,region)]
                                            ^

tests/testthat/test_ipf.R:166:15: style: Put spaces around all infix operators.

eusilc[.(hid=setNA$V1),baseWeight:=NA,on=.(hid)]
             ~^~

tests/testthat/test_ipf.R:166:26: style: Commas should always have a space after.

eusilc[.(hid=setNA$V1),baseWeight:=NA,on=.(hid)]
                         ^

tests/testthat/test_ipf.R:166:36: style: Put spaces around all infix operators.

eusilc[.(hid=setNA$V1),baseWeight:=NA,on=.(hid)]
                                  ~^~~

tests/testthat/test_ipf.R:166:41: style: Commas should always have a space after.

eusilc[.(hid=setNA$V1),baseWeight:=NA,on=.(hid)]
                                        ^

tests/testthat/test_ipf.R:166:43: style: Put spaces around all infix operators.

eusilc[.(hid=setNA$V1),baseWeight:=NA,on=.(hid)]
                                         ~^~

tests/testthat/test_ipf.R:167:1: style: Trailing whitespace is superfluous.

^~

tests/testthat/test_ipf.R:173:1: style: Trailing blank lines are superfluous.

^
Please sign in to comment.
You can’t perform that action at this time.