Creating replicate weights for a survey design object (with ANES 2016 data).¶
The following line loads data created with a previous R script or notebook.
load("anes-2016-prevote-desgn.RData")
The following makes use of the survey package. You may need to install it from
CRAN using the code
install.packages("survey")
if you want to run this on your computer. (The
package is already installed on the notebook container, however.)
library(survey)
Loading required package: grid
Loading required package: Matrix
Loading required package: survival
Attaching package: ‘survey’
The following object is masked from ‘package:graphics’:
dotchart
The ‘automatic’ type gives jackknife replicates
anes_2016_prevote_jk <- as.svrepdesign(anes_2016_prevote_desgn,
type="auto")
The number of replicates is determined by the number of clusters
anes_2016_prevote_jk
Call: as.svrepdesign(anes_2016_prevote_desgn, type = "auto")
Stratified cluster jackknife (JKn) with 65 replicates.
Here we select the multistage rescaled bootstrap
anes_2016_prevote_boo <- as.svrepdesign(anes_2016_prevote_desgn,
type="mrbbootstrap")
Warning message in mrbweights(design$cluster, design$strata, design$fpc, ...):
“Design is sampled with replacement: only first stage used”
anes_2016_prevote_boo
Call: as.svrepdesign(anes_2016_prevote_desgn, type = "mrbbootstrap")
Multistage rescaled bootstrap with 50 replicates.
By default the number of bootstrap replicates is 50, we can change it to 200
anes_2016_prevote_boo <- as.svrepdesign(anes_2016_prevote_desgn,
type="mrbbootstrap",
replicates=200)
Warning message in mrbweights(design$cluster, design$strata, design$fpc, ...):
“Design is sampled with replacement: only first stage used”
anes_2016_prevote_boo
Call: as.svrepdesign(anes_2016_prevote_desgn, type = "mrbbootstrap",
replicates = 200)
Multistage rescaled bootstrap with 200 replicates.
A function to compute the percentage of 2012 Democratic and Republican voters who vote for a candidate of the same party in 2016:
StayerPerc <- function(weights,data){
tab <- xtabs(weights~vote16+recall12,data=data)
# Remove 'inap' responses
tab <- tab[-6,-5]
# Column percentages
ptab <- 100*prop.table(tab,2)
# The diagonal are the percentages of 'stayers'
# among the voters of 2012.
# The first two elements of the diagonal are
# the Democratic and Republican stayers.
structure(diag(ptab)[1:2],
names=c("Democratic",
"Republican"))
}
Estimates and replication based standard errors based on jackknife
withReplicates(anes_2016_prevote_jk,
StayerPerc)
theta SE
Democratic 68.765 3.8462
Republican 69.058 3.6052
withReplicates(anes_2016_prevote_boo,
StayerPerc)
theta SE
Democratic 68.765 3.9823
Republican 69.058 3.3640
- R file: survey-replication-weights-ANES2016.R
- Rmarkdown file: survey-replication-weights-ANES2016.Rmd
- Jupyter notebook file: survey-replication-weights-ANES2016.ipynb
- Interactive version of the Jupyter notebook (shuts down after 60s):
- Interactive version of the Jupyter notebook (sign in required):