x <- declared( c(1:5, -1), labels = c(Good = 1, Bad = 5, DK = -1), na_values = -1 ) hx <- haven::labelled_spss( c(1:5, -1), labels = c(Good = 1, Bad = 5, DK = -1), na_values = -1 ) test_that("w_standardize() works", { expect_equal(w_standardize(x), w_standardize(x, wt = rep(1, 6))) expect_equal(w_standardize(x), w_standardize(hx)) expect_warning(w_standardize(list(A = 1)), "should be a numerical") expect_error(w_standardize(x, wt = "weight")) }) set.seed(215) fx <- factor(sample(letters[1:5], 215, replace = TRUE)) xc <- sample(0:4, 215, replace = TRUE) values <- sample(c(1:7, -91), 215, replace = TRUE) x2 <- declared(values, labels = c("Good" = 1, "Bad" = 7)) values <- sample(c(1:7, -91, NA), 215, replace = TRUE) x4 <- declared( values, labels = c("Good" = 1, "Bad" = 7, "Don't know" = -91), na_values = -91 ) DF <- data.frame( Area = declared( sample(1:2, 215, replace = TRUE, prob = c(0.45, 0.55)), labels = c(Rural = 1, Urban = 2) ), Gender = declared( sample(1:2, 215, replace = TRUE, prob = c(0.55, 0.45)), labels = c(Males = 1, Females = 2) ), Age = sample(18:90, 215, replace = TRUE), Children = sample(0:5, 215, replace = TRUE) ) # Weighting: observed proportions op <- proportions(with(DF, table(Gender, Area))) # Theoretical proportions: 53% Rural, and 50% Females tp <- rep(c(0.53, 0.47), each = 2) * rep(c(0.498, 0.502), 2) / op # The frequency weights DF$fweight <- tp[match(10 * DF$Area + DF$Gender, c(11, 12, 21, 22))] library(admisc) test_that("tests have the same output", { expect_snapshot(x) expect_snapshot(w_standardize(x)) expect_snapshot(using(DF, head(w_standardize(Age)))) expect_snapshot(using(DF, head(w_standardize(Age, wt = fweight)))) })