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

Create separate function to calc popsize and sampsize #139

Closed
smishr opened this issue Dec 14, 2022 · 1 comment
Closed

Create separate function to calc popsize and sampsize #139

smishr opened this issue Dec 14, 2022 · 1 comment
Labels
enhancement New feature or request logic heavy For if-elseif-else ladders, type checking, logic heavy tasks

Comments

@smishr
Copy link
Contributor

smishr commented Dec 14, 2022

In R survey there is an inner function called as.fpc which returns popsize and sampsize for any arbitrary design given strata and cluster arguments. It should be a long term goal to achieve similar in Survey.jl

function (df, strata, ids, pps = FALSE) 
{
  count <- function(x) sum(!duplicated(x))
  sampsize <- matrix(ncol = ncol(ids), nrow = nrow(ids))
  for (i in 1:ncol(ids)) split(sampsize[, i], strata[, i]) <- lapply(split(ids[, 
    i], strata[, i]), count)
  if (is.null(df)) {
    rval <- list(popsize = NULL, sampsize = sampsize)
    class(rval) = "survey_fpc"
    return(rval)
  }
  fpc <- as.matrix(df)
  if (xor(ispopsize <- any(df > 1), all(df >= 1))) {
    big <- which(fpc >= 1, arr.ind = TRUE)
    small <- which(fpc < 1, arr.ind = TRUE)
    cat("record", big[1, 1], " stage", big[1, 2], ": fpc=", 
      fpc[big[1, , drop = FALSE]], "\n")
    cat("record", small[1, 1], " stage ", small[1, 2], ": fpc=", 
      fpc[small[1, , drop = FALSE]], "\n")
    stop("Must have all fpc>=1 or all fpc<=1")
  }
  if (ispopsize) {
    if (pps) 
      stop("fpc must be specified as sampling fraction for PPS sampling")
    popsize <- fpc
  }
  else {
    popsize <- sampsize/(fpc)
  }
  if (any(popsize < sampsize)) {
    toobig <- which(popsize < sampsize, arr.ind = TRUE)
    cat("record", toobig[1, 1], "stage", toobig[1, 2], ": popsize=", 
      popsize[toobig[1, , drop = FALSE]], " sampsize=", 
      sampsize[toobig[1, , drop = FALSE]], "\n")
    stop("FPC implies >100% sampling in some strata")
  }
  if (!ispopsize && any(is.finite(popsize) & (popsize > 1e+10))) {
    big <- which(popsize > 1e+10 & is.finite(popsize), arr.ind = TRUE)
    warning("FPC implies population larger than ten billion (record", 
      big[1, 1], " stage ", big[1, 2], ")")
  }
  if (!pps) {
    for (i in 1:ncol(popsize)) {
      diff <- by(popsize[, i], list(strata[, i]), count)
      if (any(as.vector(diff) > 1)) {
        j <- which(as.vector(diff) > 1)[1]
        warning("`fpc' varies within strata: stratum ", 
          names(diff)[j], " at stage ", i)
      }
    }
  }
  else {
    diff <- by(popsize[, i], list(ids[, i]), count)
    if (any(as.vector(diff) > 1)) {
      j <- which(as.vector(diff) > 1)[1]
      warning("`fpc' varies within cluster: cluster ", 
        names(diff)[j], " at stage ", i)
    }
  }
  rval <- list(popsize = popsize, sampsize = sampsize)
  class(rval) <- "survey_fpc"
  rval
}
@smishr smishr added enhancement New feature or request logic heavy For if-elseif-else ladders, type checking, logic heavy tasks labels Dec 14, 2022
@smishr
Copy link
Contributor Author

smishr commented Jan 4, 2023

designs updated too much

@smishr smishr closed this as completed Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request logic heavy For if-elseif-else ladders, type checking, logic heavy tasks
Projects
None yet
Development

No branches or pull requests

2 participants