This file contains hidden or 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 hidden or 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 hidden or 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 ---- |
This file contains hidden or 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
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 |
This file contains hidden or 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(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") |
This file contains hidden or 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(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)))) | |
This file contains hidden or 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
# 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") |
This file contains hidden or 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(survey) | |
# Get example data | |
data(api) | |
dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc) | |
# Estimate means by subpopulation | |
# Use `covmat = TRUE` to also estimate covariances | |
mns <- svyby(~api99, ~stype, | |
dclus1, |
This file contains hidden or 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(magrittr) | |
library(tidyverse) | |
library(readr) | |
library(readxl) | |
library(schneidr) | |
theme_set(theme_schneidr(base_font_family = 'sans', | |
titles_font_family = 'sans')) | |
# Generate population |
This file contains hidden or 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
# Define the function | |
get_hessian <- function(f, as_matrix = FALSE, eval_at = NULL) { | |
fn_inputs <- all.vars(f); names(fn_inputs) <- fn_inputs | |
n_inputs <- length(fn_inputs) | |
# Obtain the symbolic Hessian as a nested list | |
result <- lapply(fn_inputs, function(x) lapply(fn_inputs, function(x) NULL)) |