w8margin_matched {svyweight} | R Documentation |
Check if w8margin Matches Observed Data
Description
Checks whether specified w8margin
object and variable in observed
data are compatible, and are expected to produce valid call to
rake
. Returns a logical true/false, and generates
warning messages to specify likely issues. Intended to help quickly
diagnose incompatibilities between w8margins and observed data.
Usage
w8margin_matched(w8margin, observed, refactor = FALSE,
na.targets.allow = FALSE, zero.targets.allow = FALSE)
Arguments
w8margin |
w8margin object, or other object type that can be coerced to w8margin with a temporary variable name. |
observed |
factor vector (or, if |
refactor |
logical, specifying whether to factor observed variable before checking match. |
na.targets.allow |
logical, indicating whether NA values in target should produce error ( |
zero.targets.allow |
logical, indicating whether zero values in target should produce error ( |
Details
With default parameters (na.targets.allow = FALSE
, zero.targets.allow = FALSE
,
and refactor = FALSE
), the function checks whether a w8margin
object is in the strict format required by rake
; this format
will also be accepted by rakesvy()
and rakew8()
. Changing
the default parameters relaxes some checks. With the parameters
altered, the function will only assess whether w8margin
objects are
usable by rakesvy()
and rakew8()
, which
accept a more flexible range of target formats.
It should not generally be necessary to call w8margin_matched()
manually when
using rakesvy()
and rakew8()
to compute weights.
However, may be useful to call directly, when manually calling underlying
weighting functions from the survey
package, or for diagnostic purposes.
Value
A logical, indicating whether w8margin is compatible with observed.
Examples
gender_w8margin <- as.w8margin(
c(Male = .49, Female = .51),
varname = "gender",
samplesize = 2179)
# Returns TRUE
w8margin_matched(gender_w8margin, gles17$gender)
gender_w8margin_alt <- as.w8margin(
c(man = .49, woman = .51),
varname = "gender",
samplesize = 2179)
# Returns FALSE - level names in gles17$gender do not match level names in gender_w8margin_alt
w8margin_matched(gender_w8margin_alt, gles17$gender)
agecat_50plus_w8margin <- as.w8margin(
c("50-59" = .35, "60-69" = .27, ">=70" = .38),
varname = "educ",
samplesize = 2179
)
gles17_50plus <- gles17[gles17$agecat %in% c("50-59", "60-69", ">=70"),]
# Returns FALSE - gles17$agecat has empty factor levels for <=29, 30-39, 40-49
w8margin_matched(agecat_50plus_w8margin, gles17_50plus$agecat)
# Returns TRUE - gles17$agecat is refactored to drop empty levels
w8margin_matched(agecat_50plus_w8margin, gles17_50plus$agecat, refactor = TRUE)