Skip to content

Instantly share code, notes, and snippets.

@bschneidr
bschneidr / Franco-et-al-CIs-from-Supp.R
Created February 25, 2021 17:42
Calculate Confidence Intervals using Franco et al. R Code
# Download Franco et al. supplementary materials if you trust this code to safely download materials ----
trust <- FALSE
if (trust) {
download.file("https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6690503/bin/smy019_supplementary_data.zip",
destfile = "franco-et-al-ci-supp.zip")
}
# Unzip the folder ----
unzip("franco-et-al-ci-supp.zip", exdir = "franco-et-al-ci-supp")
@bschneidr
bschneidr / survey-multinomial-and-generalized-ordered-logit-models.r
Last active April 8, 2021 19:23
Fitting multinomial and generalized ordered logistic models with `survey` and `svyVGAM`
library(survey)
library(svyVGAM)
data(api)
# Prepare data with binary, categorical, and ordered outcomes ----
apiclus2 <- transform(apiclus2, mealcat = cut(meals,c(0,25,50,75,100)))
apiclus2 <- transform(apiclus2, mealcat_ordered = as.ordered(cut(meals,c(0,25,50,75,100))))
apiclus2 <- transform(apiclus2, mealcat_high = as.ordered(cut(meals, c(0,50,100))))
library(survey)
library(svyVGAM)
# Load some example data
data(nhanes_sxq)
# Save it to a file so it can also be used by Stata
haven::write_dta(data = nhanes_sxq,
path = "nhanes_sxq.dta")
Step 1: Run the following code.
git svn clone svn://r-forge.r-project.org/svnroot/r-survey/pkg/survey/
Step 2: Edit the '.git/config' file with the following:
[svn-remote "r-forge"]
url = svn://r-forge.r-project.org/svnroot/r-survey/pkg/survey
fetch = :refs/remotes/r-forge-svn
@bschneidr
bschneidr / variance-of-nr-bias.R
Created April 5, 2022 18:03
Estimate the variance of non-response bias estimate, using the survey package
# Generate example population and sample ----
population <- data.frame(
vax_status = sample(x = c(0,1), prob = c(0.25, 0.75), size = 1000, replace = TRUE),
response_status = sample(x = c("Respondent", "Nonrespondent"),
size = 1000, replace = TRUE, prob = c(0.8, 0.2))
)
sample_data <- population[sample(x = 1000,size=150),]
# Create a survey design object ----
@bschneidr
bschneidr / survey-pv-analysis.R
Last active August 27, 2022 15:18
Analysis of plausible values with the survey package
# https://nces.ed.gov/surveys/piaac/datafiles.asp
# Load the survey data and prepare it for analysis ----
piaac17_puf <- file.path(
"C:\\Users\\benja\\Downloads\\spss\\prgusap1_puf.sav"
) |> haven::read_sav()
piaac17_rep_svy <- svrepdesign(
data = piaac17_puf,
repweights = "SPFWT[1-9]{1}0{0,1}",
weights = ~ SPFWT0,
@bschneidr
bschneidr / sort-matrix-using-vectors.R
Last active September 9, 2022 13:45
RcppArmadillo sorting matrix based on orders of corresponding vectors
library(Rcpp)
# Create an R function for sorting a matrix
# based on an accompanying vectors
cpp_code <- (
'using namespace Rcpp;
//#include <Rcpp.h>
#include <RcppArmadillo.h>
@bschneidr
bschneidr / finite-population-bayesian-bootstrap.R
Created September 22, 2022 14:25
Example of FPBB with the Louisville Vaccination Survey
suppressPackageStartupMessages({
library(survey)
library(svrep)
library(polyapost)
})
set.seed(1999)
# Load example survey data ----
data("lou_vax_survey", package = 'svrep')
@bschneidr
bschneidr / wilson-interval-for-complex-surveys.R
Last active October 12, 2022 14:30
Wilson's confidence interval for complex surveys
#' @title Wilson's confidence interval for complex survey designs
#' @description Calculate Wilson's confidence interval for a proportion,
#' with the effective sample size determined using a design-unbiased
#' estimate of the complex survey design effect.
#'
#' @param x A formula, vector, or matrix.
#' @param design A survey.design or svyrep.design object
#' @param na.rm Should cases with missing values be dropped?
#' @param level The confidence level required
#'
@bschneidr
bschneidr / svy_prop_with_wilson_ci.R
Last active November 11, 2023 10:19
Wilson Confidence Interval for Complex Surveys
#' @title Wilson's confidence interval for complex survey designs
#' @description Calculate Wilson's confidence interval for a proportion,
#' with the effective sample size determined using a design-unbiased
#' estimate of the complex survey design effect.
#'
#' @param x A formula, vector, or matrix.
#' @param design A survey.design or svyrep.design object
#' @param na.rm Should cases with missing values be dropped?
#' @param level The confidence level required
#' @param ... Additional arguments to pass on to \code{svymean()}