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

Downloadable R script and interactive version

Explanation

The link with the “jupyterhub” icon directs you to an interactive Jupyter1 notebook, which runs inside a Docker container2. There are two variants of the interative notebook. One shuts down after 60 seconds and does not require a sign it. The other requires sign in using your ORCID3 credentials, yet shuts down only after 24 hours. (There is no guarantee that such a container persists that long, it may be shut down earlier for maintenance purposes.) After shutdown all data within the container will be reset, i.e. all files created by the user will be deleted.4

Above you see a rendered version of the Jupyter notebook.5

1

For more information about Jupyter see http://jupyter.org. The Jupyter notebooks make use of the IRKernel package.

2

For more information about Docker see https://docs.docker.com/. The container images were created with repo2docker, while containers are run with docker spawner.

3

ORCID is a free service for the authentication of researchers. It also allows to showcase publications and contributions to the academic community such as peer review.. See https://info.orcid.org/what-is-orcid/ for more information.

4

The Jupyter notebooks come with NO WARRANTY whatsoever. They are provided for educational and illustrative purposes only. Do not use them for production work.

5

The notebook is rendered with the help of the nbsphinx extension.