tests/testthat/test_survey.R

context("Survey designs")

data(api, package = "survey")
dclus1 <- svydesign(id = ~dnum, weights = ~pw, 
    data = apiclus1, fpc = ~fpc)

test_that("Survey designs work", {
    expect_is(
        iNZightPlot(api00, api99, design = dclus1, plot = FALSE),
        "inzplotoutput"
    )
})

test_that("Summary information is correct - dot plot", {
    x <- getPlotSummary(enroll, design = dclus1)
    pe <- which(grepl("Population estimates", x)) + 3

    xpe <- gsub("\\|", "", x[pe])
    expect_equal(
        round(scan(textConnection(xpe), quiet = TRUE)),
        round(c(
            as.numeric(
                svyquantile(~enroll, 
                    design = dclus1, quantiles = c(0.25, 0.5, 0.75)
                )
            ),
            as.numeric(svymean(~enroll, design = dclus1)),
            sqrt(as.numeric(svyvar(~enroll, design = dclus1))),
            as.numeric(svytotal(~enroll, design = dclus1)),
            coef(svytotal(cbind(rep(1, nrow(dclus1$variables))), dclus1)),
            nrow(apiclus1),
            min(apiclus1$enroll),
            max(apiclus1$enroll)
        ))
    )

    ## standard errors ...
    # ...
    se <- which(grepl("Standard error of estimates", x)) + 2
    xse <- gsub("\\|", "", x[se])
    expect_equivalent(
        round(
            scan(text = xse, quiet = TRUE), 
            c(2, 2, 2, 2, 2, 0, 0)
        ),
        round(
            c(
                SE(svyquantile(~enroll, 
                    design = dclus1, quantiles = c(0.25, 0.5, 0.75),
                    se = TRUE
                )),
                SE(svymean(~enroll, design = dclus1)),
                sqrt(
                    vcov(svyvar(~enroll, dclus1)) / 
                        4 / coef(svyvar(~enroll, dclus1))
                ),
                SE(svytotal(~enroll, design = dclus1)),
                SE(svytotal(cbind(rep(1, nrow(dclus1$variables))), dclus1))
            ), 
            c(2, 2, 2, 2, 2, 0, 0)
        )
    )
})

test_that("Summary information is correct - dot plot (by factor)", {
    x <- getPlotSummary(enroll, stype, design = dclus1)
    pe <- which(grepl("Population estimates", x)) + 3:5
    xpe <- gsub("\\||[A-Z]", "", x[pe])
    expect_equivalent(
        round(do.call(
            rbind,
            lapply(xpe, function(z) round(scan(textConnection(z), quiet = TRUE)))
        )),
        round(cbind(
            as.matrix(svyby(~enroll, ~stype, dclus1, svyquantile, keep.var = FALSE,
                quantiles = c(0.25, 0.5, 0.75))[,-1]),
            mean=coef(svyby(~enroll, ~stype, dclus1, svymean)),
            sd=sqrt(coef(svyby(~enroll, ~stype, dclus1, svyvar))),
            total=coef(svyby(~enroll, ~stype, dclus1, svytotal)),
            pop=tapply(weights(dclus1), dclus1$variables$stype, sum),
            n=table(dclus1$variables$stype),
            min=tapply(dclus1$variables$enroll, dclus1$variables$stype, min),
            max=tapply(dclus1$variables$enroll, dclus1$variables$stype, max)
        ))
    )

    ## standard errors
    se <- which(grepl("Standard error of estimates", x)) + 2:4
    xse <- gsub("\\||[A-Z]", "", x[se])
    expect_equivalent(
        do.call(rbind,
            lapply(xse, function(z) 
                round(
                    scan(text = z, quiet = TRUE),
                    c(2, 2, 2, 2, 2, 0, 1)
                )
            )
        ),
        as.matrix(cbind(
            suppressWarnings(
                round(SE(svyby(~enroll, ~stype, dclus1, svyquantile, 
                    ci = TRUE, se = TRUE,
                    quantiles = c(0.25, 0.5, 0.75))), 2)
            ),
            mean = round(SE(svyby(~enroll, ~stype, dclus1, svymean)), 2),
            sd = {
                vv <- svyby(~enroll, ~stype, dclus1, svyvar)
                vc <- suppressWarnings(diag(vcov(vv)))
                round(sqrt(vc / 4 / coef(vv)), 2)
            },
            total = round(SE(svyby(~enroll, ~stype, dclus1, svytotal))),
            pop = round(SE(svytotal(~stype, dclus1)), 1)
        ))
    )
})
iNZightVIT/iNZightPlots documentation built on Nov. 13, 2019, 7:09 a.m.