R/BW2stagePPS.R

Defines functions BW2stagePPS

Documented in BW2stagePPS

BW2stagePPS <- function(X, pp, psuID, lonely.SSU = "mean"){
    M <- length(unique(psuID))
    Ni <- table(psuID)
    cl.tots <- by(X, INDICES = psuID, FUN = sum)
    cl.vars <- by(X, INDICES = psuID, FUN = var)
    cl.vars.miss <- is.na(cl.vars)
    if (lonely.SSU == "mean"){
      cl.vars[cl.vars.miss] <- mean(cl.vars[!cl.vars.miss])
    }
    else if (lonely.SSU == "zero"){
      cl.vars[cl.vars.miss] <- 0
    }
    else {stop("Illegal value of lonely.SSU: ", lonely.SSU, "\n")}

    tU <- sum(cl.tots)
    S2U1 <- sum(pp * (cl.tots/pp - tU)^2)
    B2 <- S2U1 / tU^2

    ybarU <- mean(X)
    W2 <- sum(Ni^2 * cl.vars / pp) / tU^2
    S2U <- var(X)
    k <- (B2 + W2) / (S2U/ybarU^2)

    c("B2"=B2,
      "W2"=W2,
      "unit relvar" = S2U/ybarU^2,
      "B2+W2"=B2 + W2,
      "k"=k,
      "delta"=B2 / (B2 + W2))
}

Try the PracTools package in your browser

Any scripts or data that you put into this service are public.

PracTools documentation built on Jan. 19, 2022, 1:08 a.m.