Skip to content

Commit e0cc221

Browse files
committed
move arguments from ... into shape's explicit signature
It's a long sig but makes documentation and usage clearer. Also rename the `target_proportion_name` of `shape()` and the `prop_name` argument of `poststratify` for consistency -> `proportion_name`.
1 parent eb48965 commit e0cc221

14 files changed

+332
-279
lines changed

R/class-control.r

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
setClass("Control",
2-
slots = list(constant_item = "logical",
2+
slots = list(# item data
3+
item_names = "character",
4+
time_name = "character",
35
geo_name = "character",
4-
geo_filter = "character",
56
group_names = "ANY",
6-
item_names = "character",
77
id_vars = "ANY",
8-
aggregate_item_names = "ANY",
9-
min_survey_filter = "numeric",
8+
# restrictions
9+
time_filter = "ANY",
10+
geo_filter = "ANY",
1011
min_t_filter = "numeric",
11-
modifier_names = "character",
12-
prop_name = "character",
12+
min_survey_filter = "numeric",
13+
survey_name = "ANY",
14+
# aggregate data
15+
aggregate_item_names = "ANY",
16+
# modifier data
17+
modifier_names = "ANY",
18+
t1_modifier_names = "ANY",
19+
standardize = "logical",
20+
# target data
1321
raking = "ANY",
22+
weight_name = "ANY",
23+
proportion_name = "character",
1424
rake_names = "character",
15-
standardize = "logical",
16-
sample_margins = "list",
17-
survey_name = "ANY",
18-
target_group_names = "character",
19-
target_proportion_name = "character",
20-
t1_modifier_names = "character",
21-
time_name = "character",
22-
time_filter = "numeric",
23-
weight_name = "ANY"),
24-
prototype = prototype(constant_item = TRUE,
25-
min_t_filter = 1L,
26-
min_survey_filter = 1L,
27-
standardize = TRUE,
28-
prop_name = "proportion"),
25+
# modeling options
26+
constant_item = "logical"),
2927
validity = function(object) {
3028
if (!length(object@time_name) == 1L)
3129
"\"time_name\" should be a single variable name"
@@ -37,8 +35,16 @@ setClass("Control",
3735
"if specified \"survey_name\" should be a single variable name"
3836
else if (length(object@group_names) && !is.character(object@group_names))
3937
"if specified \"group_names\" should give variable names in a character vector"
38+
else if (length(object@modifier_names) && !is.character(object@modifier_names))
39+
"if specified \"modifier_names\" should give variable names in a character vector"
40+
else if (length(object@t1_modifier_names) && !is.character(object@t1_modifier_names))
41+
"if specified \"t1_modifier_names\" should give variable names in a character vector"
4042
else if (length(object@id_vars) && !is.character(object@id_vars))
4143
"if specified \"id_vars\" should give variable names in a character vector"
44+
else if (length(object@time_filter) && !is.numeric(object@time_filter))
45+
"if specified \"time_filter\" should give numeric values of the `time_name` variable"
46+
else if (length(object@geo_filter) && !is.character(object@geo_filter))
47+
"if specified \"geo_filter\" should give character values of the `geo_name` variable"
4248
else if (length(object@aggregate_item_names) && !is.character(object@aggregate_item_names))
4349
"if specified \"aggregate_item_names\" should give values in an \"item\" column of aggregate_data"
4450
else if (!length(object@standardize) == 1L)
@@ -47,6 +53,8 @@ setClass("Control",
4753
"if specified \"weight_name\" should be a single variable name"
4854
else if (length(object@weight_name) > 1)
4955
"if specified \"weight_name\" should be a single variable name"
56+
else if (length(object@proportion_name) && length(object@proportion_name) != 1L)
57+
"if specified \"proportion_name\" should be a single variable name"
5058
else if (length(object@raking) && !is.list(object@raking) &
5159
!"formula" %in% class(object@raking))
5260
"\"raking\" should be a formula or a list of formulas"
@@ -64,8 +72,6 @@ setClass("Control",
6472
"\"min_survey_filter\" should be a positive integer"
6573
else if (!length(object@min_t_filter) == 1L && object@min_t_filter > 0L)
6674
"\"min_t_filter\" should be a positive integer"
67-
else if (length(object@sample_margins) && !is.list(object@sample_margins))
68-
"\"sample_margins\" should be a list of formulas or data frames"
6975
else
7076
TRUE
7177
})

R/constants.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var_types <- list(group_names = c("character", "factor"),
2929
survey_name = c("character", "factor"),
3030
weight_name = "numeric",
3131
strata_names = c("character", "factor", "integer", "numeric"),
32-
target_proportion_name = "numeric",
32+
proportion_name = "numeric",
3333
modifier_names = c("integer", "numeric"),
3434
t1_modifier_names = c("integer", "numeric"),
3535
item = c("character", "factor"),

R/methods-control.r

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,55 @@
22
`%chin%` <- data.table::`%chin%`
33

44
# Constructor for Control
5-
init_control <- function(item_data,
6-
item_names,
7-
time_name,
8-
geo_name,
9-
group_names,
10-
weight_name,
11-
survey_name,
12-
raking,
13-
id_vars,
14-
aggregate_data,
15-
aggregate_item_names,
5+
init_control <- function(item_data = item_data,
6+
item_names = item_names,
7+
time_name = time_name,
8+
geo_name = geo_name,
9+
group_names = group_names,
10+
id_vars = id_vars,
11+
time_filter = time_filter,
12+
geo_filter = geo_filter,
13+
min_t_filter = min_t_filter,
14+
min_survey_filter = min_survey_filter,
15+
survey_name = survey_name,
16+
aggregate_data = aggregate_data,
17+
aggregate_item_names = aggregate_item_names,
18+
modifier_data = modifier_data,
19+
modifier_names = modifier_names,
20+
t1_modifier_names = t1_modifier_names,
21+
standardize = standardize,
22+
target_data = target_data,
23+
raking = raking,
24+
weight_name = weight_name,
25+
proportion_name = proportion_name,
26+
constant_item = constant_item,
1627
...) {
17-
ctrl <- new("Control", item_names = item_names,
18-
time_name = time_name, geo_name = geo_name, group_names =
19-
group_names, weight_name = weight_name, survey_name =
20-
survey_name, raking = raking, id_vars = id_vars,
21-
aggregate_item_names = aggregate_item_names, ...)
22-
28+
ctrl <- new("Control",
29+
# item data
30+
item_names = item_names,
31+
time_name = time_name,
32+
geo_name = geo_name,
33+
group_names = group_names,
34+
id_vars = id_vars,
35+
# restrictions
36+
time_filter = time_filter,
37+
geo_filter = geo_filter,
38+
min_t_filter = min_t_filter,
39+
min_survey_filter = min_survey_filter,
40+
survey_name = survey_name,
41+
# aggregate data
42+
aggregate_item_names = aggregate_item_names,
43+
# modifier data
44+
modifier_names = modifier_names,
45+
t1_modifier_names = t1_modifier_names,
46+
standardize = standardize,
47+
# target data
48+
raking = raking,
49+
weight_name = weight_name,
50+
proportion_name = proportion_name,
51+
# modeling options
52+
constant_item = constant_item,
53+
...)
2354
is_item_name <- valid_names(item_data, ctrl, 1L)
2455
is_item_name(c("time_name", "geo_name"))
2556
has_type(c("time_name", "geo_name"), item_data, ctrl)
@@ -34,6 +65,12 @@ init_control <- function(item_data,
3465
}
3566
}
3667

68+
if (length(ctrl@modifier_names)) {
69+
if (!length(ctrl@t1_modifier_names)) {
70+
ctrl@t1_modifier_names <- ctrl@modifier_names
71+
}
72+
}
73+
3774
if (!length(ctrl@time_filter)) {
3875
ctrl@time_filter <- sort(unique(item_data[[ctrl@time_name]]))
3976
if (length(aggregate_data)) {
@@ -50,7 +87,6 @@ init_control <- function(item_data,
5087
}
5188
}
5289

53-
5490
if (length(raking)) {
5591
if (is.list(ctrl@raking)) {
5692
ctrl@rake_names = unlist(lapply(ctrl@raking, all.vars))

R/methods-dgirtfit-poststratify.r

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ utils::globalVariables(c("value", "scaled_prop"))
1616
#' @param ... Additional arguments to methods.
1717
setGeneric("poststratify", signature = "x",
1818
function(x, target_data, strata_names, aggregated_names,
19-
prop_name = "proportion", ...)
19+
proportion_name = "proportion", ...)
2020
standardGeneric("poststratify"))
2121

2222
#' @param pars Selected parameter names.
@@ -38,9 +38,9 @@ setGeneric("poststratify", signature = "x",
3838
#' @export
3939
setMethod("poststratify", c("dgo_fit"),
4040
function(x, target_data, strata_names, aggregated_names,
41-
prop_name = "proportion", pars = "theta_bar") {
41+
proportion_name = "proportion", pars = "theta_bar") {
4242
x <- as.data.frame(x, pars = pars)
43-
callGeneric(x, target_data, strata_names, aggregated_names, prop_name)
43+
callGeneric(x, target_data, strata_names, aggregated_names, proportion_name)
4444
})
4545

4646
#' @param x A \code{data.frame} or \code{dgo_fit} object.
@@ -50,19 +50,19 @@ setMethod("poststratify", c("dgo_fit"),
5050
#' population strata.
5151
#' @param aggregated_names Names of variables to be aggregated over in
5252
#' poststratification.
53-
#' @param prop_name Name of the column in \code{target_data} that gives
53+
#' @param proportion_name Name of the column in \code{target_data} that gives
5454
#' strata proportions.
5555
#' @return A table of poststratified estimates.
5656
#' @rdname poststratify
5757
#' @importFrom stats pnorm
5858
#' @export
5959
setMethod("poststratify", "data.frame",
6060
function(x, target_data, strata_names, aggregated_names,
61-
prop_name = "proportion", pars = "theta_bar") {
61+
proportion_name = "proportion", pars = "theta_bar") {
6262
assert(is.data.frame(target_data))
6363
assert(all_strings(strata_names))
6464
assert(all_strings(strata_names))
65-
assert(assertthat::is.string(prop_name))
65+
assert(assertthat::is.string(proportion_name))
6666
assert(all_strings(pars))
6767

6868
x <- data.table::setDT(data.table::copy(x))
@@ -103,7 +103,7 @@ setMethod("poststratify", "data.frame",
103103
}
104104

105105
extra_cols <- setdiff(names(targets), c(strata_names, aggregated_names,
106-
prop_name))
106+
proportion_name))
107107
if (length(extra_cols)) {
108108
targets[, c(extra_cols) := NULL]
109109
}
@@ -118,7 +118,7 @@ setMethod("poststratify", "data.frame",
118118
"and this may indicate a larger problem.")
119119
}
120120

121-
props <- scale_props(props, prop_name, strata_names)
121+
props <- scale_props(props, proportion_name, strata_names)
122122

123123
check_proportions(props, strata_names)
124124
res <- props[, list(value = sum(value * scaled_prop)), by = strata_names]
@@ -130,11 +130,11 @@ check_estimates <- function(estimates, strata_names) {
130130
estimates
131131
}
132132

133-
scale_props <- function(props, prop_name, strata_names) {
134-
strata_sums <- props[, list(strata_sum = sum(get(prop_name))),
133+
scale_props <- function(props, proportion_name, strata_names) {
134+
strata_sums <- props[, list(strata_sum = sum(get(proportion_name))),
135135
by = strata_names]
136136
props <- merge(props, strata_sums, all = FALSE, by = strata_names)
137-
props[, c("scaled_prop") := get(prop_name) / get("strata_sum")]
137+
props[, c("scaled_prop") := get(proportion_name) / get("strata_sum")]
138138
return(props)
139139
}
140140

R/reweight_item_responses.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ rake_weights <- function(item_data, target_data, control) {
2626
item_design <- survey::svydesign(ids = ~1, data = item_data,
2727
weights = formula(paste0("~", control@weight_name)))
2828
target_design <- survey::svydesign(ids = ~1, data = target_data,
29-
weights = formula(paste0("~", control@target_proportion_name)))
29+
weights = formula(paste0("~", control@proportion_name)))
3030
target_tables <- lapply(formulas, survey::svytable,
3131
design = target_design)
3232
raked_design <- rake_partial(design = item_design,

0 commit comments

Comments
 (0)