R/assertions.R

Defines functions check_effect_type check_binary_0_1 check_for_missing

check_for_missing <- function(data, A, W, M, Z, C) {
	check <- data[, c(A, W, M, Z, C), drop = FALSE]

	if (any(is.na(check))) {
		return("Missing data found in treatment/covariate/mediator/observed nodes")
	}

	TRUE
}

assert_not_missing <- checkmate::makeAssertionFunction(check_for_missing)

check_binary_0_1 <- function(data, x) {
	if (is.null(x)) return(TRUE)

	var <- data[[x]]

	# Check if there are exactly two unique values and they are not 0 and 1
	if (length(unique(var)) == 2 && !all(var %in% c(0, 1))) {
		return("The outcome contains exactly two unique values, but they are not 0 and 1")
	}

	TRUE
}

assert_binary_0_1 <- checkmate::makeAssertionFunction(check_binary_0_1)

check_effect_type <- function(moc, effect) {
	if (is.null(moc) & effect == "RT") {
		return("Must provide mediator-outcome confounders for recanting twins")
	}

	if (is.null(moc) & effect == "RI") {
		return("Must provide mediator-outcome confounders for interventional effects")
	}

	if (!is.null(moc) & effect %in% c("N", "O", "D")) {
		return("Must not provide mediator-outcome confounders for natural, organic, or decision theoretic effects")
	}

	TRUE
}

assert_effect_type <- checkmate::makeAssertionFunction(check_effect_type)

Try the crumble package in your browser

Any scripts or data that you put into this service are public.

crumble documentation built on April 13, 2025, 5:10 p.m.