Skip to content

Commit

Permalink
Keeping up with 'survey' 4.3: allow the user to specify a degf argu…
Browse files Browse the repository at this point in the history
…ment in `as_survey_rep()`.
  • Loading branch information
bschneidr committed Mar 9, 2024
1 parent 1917f75 commit 6b5237d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# (development)
* `as_survey_rep()` now has an argument `degf`, corresponding to the same argument in the survey function `svrepdesign()`. This argument can be useful for large data sets, since specifying a value for `degf` avoids a calculation which can be slow for very large data sets.

# srvyr 1.2.0
* `survey_prop()` now uses proportions as the default, which should confidence interval improve coverage, but does mean results may slightly change (#141, #142, thanks @szimmer)
* New function `survey_corr()` calculates the correlation between 2 variables, (#150, #151, thanks @szimmer & @bschneidr)
Expand Down
10 changes: 8 additions & 2 deletions R/as_survey_rep.r
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
#' @param fpctype Finite population correction information
#' @param mse if \code{TRUE}, compute variances based on sum of squares
#' around the point estimate, rather than the mean of the replicates
#' @param degf Design degrees of freedom: a single number, or \code{NULL},
#' in which case a value will be computed automatically, which can be slow
#' for very large data sets. See \code{\link[survey]{svrepdesign}}
#' for more details.
#' @param ... ignored
#' @param compress if \code{TRUE}, store replicate weights in compressed form
#' (if converting from design)
Expand Down Expand Up @@ -73,7 +77,7 @@ as_survey_rep.data.frame <-
"other"), combined_weights = TRUE,
rho = NULL, bootstrap_average = NULL, scale = NULL,
rscales = NULL, fpc = NULL, fpctype = c("fraction", "correction"),
mse = getOption("survey.replicates.mse"), ...) {
mse = getOption("survey.replicates.mse"), degf = NULL, ...) {
variables <- srvyr_select_vars(rlang::enquo(variables), .data)
repweights <- srvyr_select_vars(rlang::enquo(repweights), .data)
weights <- srvyr_select_vars(rlang::enquo(weights), .data)
Expand All @@ -85,6 +89,7 @@ as_survey_rep.data.frame <-
repweights = repweights,
weights = weights,
data = .data,
degf = degf,
type = type,
combined.weights = combined_weights,
rho = rho,
Expand Down Expand Up @@ -112,7 +117,7 @@ as_survey_rep.tbl_lazy <-
"other"), combined_weights = TRUE,
rho = NULL, bootstrap_average = NULL, scale = NULL,
rscales = NULL, fpc = NULL, fpctype = c("fraction", "correction"),
mse = getOption("survey.replicates.mse"), ...) {
mse = getOption("survey.replicates.mse"), degf = NULL, ...) {

variables <- rlang::enquo(variables)
repweights <- rlang::enquo(repweights)
Expand All @@ -134,6 +139,7 @@ as_survey_rep.tbl_lazy <-
repweights = repweights,
weights = weights,
data = survey_vars_local,
degf = degf,
type = type,
combined.weights = combined_weights,
rho = rho,
Expand Down
7 changes: 7 additions & 0 deletions man/as_survey_rep.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/testthat/test_as_survey_rep.r
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,18 @@ test_that("as_survey_rep works when using SDR/ACS method of replicate weights",
expect_equal(c(out_survey[[1]], sqrt(attr(out_survey, "var"))),
c(out_srvyr_acs[[1]][[1]], out_srvyr_acs[[2]][[1]]))
})

# ------------------------------------------------------------------
# Test user-specified degrees of freedom
# ------------------------------------------------------------------

sdr_srvyr <- cbind(sdr_sample, as.data.frame(sdr_factors)) %>%
as_survey_rep(repweights = starts_with("REP_"),
weights = "weights",
type = "successive-difference",
combined = FALSE,
degf = 4)

test_that("as_survey_rep accepts user-specified degf", {
expect_equal(degf(sdr_srvyr), 4)
})

0 comments on commit 6b5237d

Please sign in to comment.