-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
11,809 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
^\.travis\.yml$ | ||
^appveyor\.yml$ | ||
CONTRIBUTING.md | ||
^data-raw$ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ | |
*.dll | ||
|
||
inst/doc | ||
*bak |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
## Read in the Roberts Court data (working directory should be bggum/data-raw/) | ||
options(stringsAsFactors = FALSE) | ||
roberts_court <- read.csv("../vignettes/roberts_court.csv") | ||
## Spread it into a response matrix after recoding the votes | ||
roberts_court$vote <- ifelse(roberts_court$vote %in% c(2, 7), 0, 1) | ||
library(dplyr) | ||
library(tidyr) | ||
library(bggum) | ||
responses <- roberts_court %>% | ||
select(-caseId, -term) %>% | ||
spread(lexisCite, vote) | ||
rownames(responses) <- responses$justiceName | ||
responses <- as.matrix(responses[ , -1]) | ||
unanimous <- apply(responses, 2, function(x) length(unique(na.omit(x))) == 1) | ||
responses <- responses[ , !unanimous] | ||
## Tune hyperparameters | ||
library(bggum) | ||
set.seed(123) | ||
proposal_sds <- tune_proposals(responses, 5000) | ||
sapply(proposal_sds, mean) | ||
set.seed(456) | ||
temps <- tune_temperatures(responses, n_temps = 6, proposal_sds = proposal_sds) | ||
round(temps, 2) | ||
temps <- temps[1:6] | ||
## Run two chains in parallel | ||
library(parallel) | ||
cl <- makeCluster(2, type = "FORK", outfile = "bggum-log2.txt") | ||
clusterSetRNGStream(cl = cl, iseed = 789) | ||
chains <- parLapplyLB(cl = cl, X = 1:2, fun = function(x) { | ||
ggumMC3(data = responses, | ||
sample_iterations = 50000, | ||
burn_iterations = 5000, | ||
proposal_sds = proposal_sds, | ||
temps = temps) | ||
}) | ||
stopCluster(cl) | ||
## Post-process | ||
constraint <- which(rownames(responses) == "RBGinsburg") | ||
processed_chains <- lapply(chains, post_process, constraint = constraint, | ||
expected_sign = "-") | ||
## Summarize posterior | ||
posterior_summary <- summary(processed_chains) | ||
## Save the posterior summary | ||
saveRDS(posterior_summary, file = "../vignettes/posterior_summary.rds") | ||
## Check convergence | ||
library(coda) | ||
convergence_stats <- gelman.diag(processed_chains) | ||
write.csv(convergence_stats$psrf, file = "../vignettes/convergence.csv", | ||
row.names = FALSE) | ||
## Write out Roberts' draws | ||
roberts <- which(rownames(responses) == "JGRoberts") | ||
iters <- nrow(chains[[1]]) | ||
idx <- seq(floor(iters / 1000), iters, floor(iters / 1000)) | ||
chain1_raw <- chains[[1]][idx, roberts] | ||
chain2_raw <- chains[[2]][idx, roberts] | ||
chain1_pp <- processed_chains[[1]][idx, roberts] | ||
chain2_pp <- processed_chains[[2]][idx, roberts] | ||
roberts_draws <- cbind(chain1_raw, chain2_raw, chain1_pp, chain2_pp) | ||
write.csv(roberts_draws, file = "../vignettes/roberts_draws.csv", | ||
row.names = FALSE) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
## I do not like strings as factors | ||
options(stringsAsFactors = FALSE) | ||
## Read in the raw SCDB data. I will not keep this in the repo, | ||
## so it must be downloaded from http://supremecourtdatabase.org/ | ||
scdb <- read.csv("SCDB_2019_01_justiceCentered_Citation.csv") | ||
## We only need a few columns (users can connect to the rest of the variables | ||
## since they know the caseId if they're curious about something) | ||
columns_to_keep <- c("caseId", "lexisCite", "term", "justiceName", "vote") | ||
## We only want Roberts Court cases that are not evenly divided or | ||
## per curiam decisions without oral argument | ||
rows_to_keep <- scdb$chief == "Roberts" & !scdb$decisionType %in% c(2, 5) | ||
## Now we can subset the data and write it out | ||
roberts_court_scdb_subset <- scdb[rows_to_keep, columns_to_keep] | ||
## Notice this assumes your working directory is bggum/data-raw/ | ||
write.csv(roberts_court_scdb_subset, | ||
file = "../vignettes/roberts_court.csv", | ||
row.names = FALSE) |
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
Oops, something went wrong.