Skip to content

Commit

Permalink
Merge pull request #6 from DeclareDesign/cran-patch
Browse files Browse the repository at this point in the history
version 1.0.6
  • Loading branch information
macartan authored Feb 18, 2024
2 parents 44dd4c6 + 690f937 commit bc4898d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 53 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: rdss
Title: Companion Datasets and Functions for Research Design in the Social Sciences
Version: 1.0.5
Version: 1.0.6
Authors@R:
c(person("Graeme", "Blair", email = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-9164-2102")),
person("Alexander", "Coppock", email = "[email protected]", role = c("aut"), comment = c(ORCID = "0000-0002-5733-2386")),
Expand All @@ -11,7 +11,7 @@ License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Suggests:
testthat (>= 3.0.0),
rdrobust,
Expand Down
2 changes: 1 addition & 1 deletion R/helpers-conjoint-experiments.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#' cluster = TRUE, respondent.id = "CaseID", design = immigrationdesign)
#'
#' # Print summary
#' tidy(results)
#' # tidy(results)
#' }
#'
tidy.amce <-
Expand Down
68 changes: 26 additions & 42 deletions R/helpers-process-tracing.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,46 @@
#'
#' @export
#' @examples
#'
#' # Simple example showing ambiguity in attribution
#' process_tracing_estimator(
#' CausalQueries::make_model("X -> Y"),
#' "Y[X=1] > Y[X=0]",
#' data.frame(X=1, Y = 1),
#' "X-Y")
#' causal_model = CausalQueries::make_model("X -> Y"),
#' query = "Y[X=1] > Y[X=0]",
#' data = data.frame(X=1, Y = 1),
#' strategies = "X-Y")
#'
#'# Example where M=1 acts as a hoop test
#' process_tracing_estimator(
#' causal_model = CausalQueries::make_model("X -> M -> Y") |>
#' CausalQueries::set_restrictions("Y[M=1] < Y[M=0]") |>
#' CausalQueries::set_restrictions("M[X=1] < M[X=0]"),
#' query = "Y[X=1] > Y[X=0]",
#' data = data.frame(X=1, Y = 1, M = 0),
#' strategies = c("Y", "X-Y", "X-M-Y"))

process_tracing_estimator <- function(causal_model, query, data, strategies) {

if(!requireNamespace("CausalQueries")){
message("The process_tracing_estimator function requires the 'CausalQueries' package.")
return(invisible())
}

strategy_elements <- strategies %>% lapply(function(x) strsplit(x, "-")[[1]])
if(nrow(data) !=1) {
stop("please provide a single row dataset")
}

given <- strategy_statements(data, strategy_elements)
given <-
lapply(strategies, function(x) strsplit(x, "-")[[1]]) |>
lapply(function(s)
paste((sapply(s, function(x)
paste(x, "==", data[x]))[sapply(s, function(x)
! is.na(data[x]))]), collapse = " & "))

causal_model %>%
CausalQueries::query_model(query = query, given = given) %>%
mutate(term = strategies, XY = paste0("X", data$X, "Y", data$Y)) %>%
causal_model |>
CausalQueries::query_model(query = query, given = given) |>
mutate(term = strategies, XY = paste0("X", data$X, "Y", data$Y)) |>
select(term, XY, estimate = mean)

}


#' Generate strategy statements given data
#'
#' Helper to generate statements of the form "X = 1 & Y = 0"
#' from realized data on one observation
#'
#' @param data A data frame with one row
#' @param strategies A list of strategies where each strategy
#' is a set of nodes to be observed
#' @keywords helper
#' @export
#' @return A string
#' @examples
#' data.frame(X = 1, M = 0, Y = NA) %>%
#' strategy_statements(list(c("X", "M", "Y"), "X", "Y"))
#'
strategy_statements <- function(data, strategies){

if(nrow(data) !=1) {
stop("strategy_statements is designed for single row datasets")
}

if(!is.list(strategies)) {
stop("please provide strategies within a list (e.g.(list(c('X', 'Y'))")
}

lapply(strategies, function(s)
(sapply(s, function(x)
paste(x, "==", data[x]))[sapply(s, function(x)
! is.na(data[x]))]) %>%
paste(collapse = " & "))

}

3 changes: 1 addition & 2 deletions R/rdss.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
#'
#' Companion datasets and functions for the book "Research Design in the Social Sciences: Declaration, Diagnosis, and Redesign" (book.declaredesign.org)
#'
#' @docType package
#' @name rdss
NULL
"_PACKAGE"

#' @importFrom generics tidy
#' @export
Expand Down
18 changes: 13 additions & 5 deletions man/process_tracing_estimator.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions man/rdss.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/tidy.amce.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bc4898d

Please sign in to comment.