generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
566 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
Package: surveytable | ||
Title: Streamlined output from the `survey` package | ||
Title: Formatted Survey Estimates | ||
Version: 0.9 | ||
Authors@R: | ||
person("Alex", "Strashny", , "[email protected]", role = c("aut", "cre")) | ||
Description: Streamlined output from the `survey` package. | ||
Description: Formatted Survey Estimates. | ||
Date/Publication: 2023 | ||
License: Apache License (>= 2) | ||
Encoding: UTF-8 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#' Confidence intervals for proportions, adjusted for degrees of freedom | ||
#' | ||
#' A version of `survey::svyciprop` that adjusts for the degrees of freedom when `method == "beta"`. | ||
#' | ||
#' Written by Makram Talih in 2019. | ||
#' | ||
#' To use this function in tabulations, type: `options(surveytable.adjust_svyciprop = TRUE)`. | ||
#' | ||
#' @param formula see `survey::svyciprop`. | ||
#' @param design see `survey::svyciprop`. | ||
#' @param method see `survey::svyciprop`. | ||
#' @param level see `survey::svyciprop`. | ||
#' @param df_method how `df` should be calculated: "default" or "NHIS". | ||
#' @param ... see `survey::svyciprop`. | ||
#' | ||
#' `df_method`: for "default", `df = degf(design)`; for "NHIS", `df = nrow(design) - 1`. | ||
#' | ||
#' @return The point estimate of the proportion, with the confidence interval as an attribute. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' set_survey("vars2019") | ||
#' options(surveytable.adjust_svyciprop = TRUE) | ||
#' tab("AGER") | ||
#' options(surveytable.adjust_svyciprop = FALSE) | ||
#' tab("AGER") | ||
svyciprop_adjusted = function(formula | ||
, design | ||
, method = c("logit", "likelihood", "asin", "beta", "mean") | ||
, level = 0.95 | ||
, df_method | ||
, ...) { | ||
assert_that(df_method %in% c("default", "NHIS")) | ||
df = switch(df_method, | ||
default = degf(design), | ||
NHIS = nrow(design) - 1 | ||
) | ||
method = match.arg(method) | ||
if (method == "beta") { | ||
m = eval(bquote(svymean(~as.numeric(.(formula[[2]])), design, ...))) | ||
rval = coef(m)[1] | ||
|
||
#Effective sample size | ||
n.eff = coef(m) * (1 - coef(m))/stats::vcov(m) | ||
|
||
attr(rval, "var") = stats::vcov(m) | ||
alpha = 1 - level | ||
|
||
# Degrees of freedom used only for adjusting effective sample size, below | ||
|
||
# For NHIS, provisional guideline is to override the R default | ||
# df = nrow(design) - 1 ## uncomment this row to override R default | ||
|
||
# R-default from degf(design) uses subdomain Strata and PSUs | ||
|
||
if (df >0) { #Korn-Graubard df-adjustment factor | ||
rat.squ = (qt(alpha/2, nrow(design) - 1)/qt(alpha/2, df))^2 | ||
} else { | ||
rat.squ = 0 # limit case: set to zero | ||
} | ||
|
||
if (rval > 0) { #Adjusted effective sample size | ||
n.eff = min(nrow(design), n.eff*rat.squ) | ||
} else { | ||
n.eff = nrow(design) #limit case: set to sample size | ||
} | ||
|
||
ci = c(stats::qbeta(alpha/2, n.eff*rval, n.eff*(1-rval)+1), stats::qbeta(1-alpha/2, n.eff*rval+1, n.eff*(1 - rval))) | ||
} | ||
else { | ||
svyciprop(formula, design, method, level, df, ...) | ||
} | ||
halfalpha = (1 - level)/2 | ||
names(ci) = paste(round(c(halfalpha, (1 - halfalpha))*100, 1), "%", sep = "") | ||
names(rval) = deparse(formula[[2]]) | ||
attr(rval, "ci") = ci | ||
class(rval) = "svyciprop" | ||
rval | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,4 @@ reference: | |
- show_options | ||
- surveytable-options | ||
- survey_subset | ||
- svyciprop_adjusted |
Oops, something went wrong.