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
library(dplyr) | |
library(tidyr) | |
library(survey) | |
# Load example data | |
# 'apipop' has one row per school | |
data('api', package = 'survey') | |
# Draw a sample of school districts |
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
library(dplyr) | |
library(broom) | |
library(tidyr) | |
# Make example data | |
my_data <- data.frame(x = sample(1:5, size = 10, replace = TRUE), | |
y = sample(1:5, size = 10, replace = TRUE)) | |
# Generate every pair of variables | |
var_list <- colnames(my_data) |
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
describe_design( | |
srs_stage(id = "PSU_ID", method = "SRS", | |
stratum = "PSU_STRATUM_ID"), | |
nonresponse_stage(response_indicator = "PSU_RESPONDENT"), | |
pps_stage(id = "SSU_ID", method = "PPS", | |
stratum = "SSU_STRATUM_ID"), | |
nonresponse_stage(response_indicator = "SSU_RESPONDENT") | |
) |
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
n = 5 | |
# Establish quadratic form | |
quad_form_matrix <- matrix(nrow = n, ncol = n) | |
for (i in seq(n)) { | |
for (j in seq(n)) { | |
if (i == j) { | |
quad_form_matrix[i,j] <- (1/n) | |
} else { |
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
#' @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()} |
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
#' @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 | |
#' |
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
suppressPackageStartupMessages({ | |
library(survey) | |
library(svrep) | |
library(polyapost) | |
}) | |
set.seed(1999) | |
# Load example survey data ---- | |
data("lou_vax_survey", package = 'svrep') |
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
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> |
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
# 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, |
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
# 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 ---- |
NewerOlder