Skip to content

Instantly share code, notes, and snippets.

@wjhopper
Last active November 10, 2024 20:40
Show Gist options
  • Select an option

  • Save wjhopper/de847b02a87587b080a3f4b83f589aae to your computer and use it in GitHub Desktop.

Select an option

Save wjhopper/de847b02a87587b080a3f4b83f589aae to your computer and use it in GitHub Desktop.
mask_hypothesize <- new.env()
## This masks the infer::hypothesize function, and does one extra step of processing before returning the data
## The response variable is re-centered to have a mean equal to the hypothesized mean
## Normally the re-centering is postponed till the generate step, but since we're not using generate (it can't take a sample of arbitrary size from an empirical distribution)
## we've got to do it a little earlier!
mask_hypothesize$hypothesize <- function(...) {
x <- infer::hypothesise(...)
response_var <- as.character(attr(x, "response"))
mu <- attr(x, "params")
x[[response_var]] <- x[[response_var]] - mean(x[[response_var]]) + mu
return(x)
}
mask_hypothesize$hypothesise <- mask_hypothesize$hypothesize
attach(mask_hypothesize, warn.conflicts = FALSE)
rm(mask_hypothesize)
ncbirths_pop <- readr::read_csv("https://bit.ly/ncbirths_pop") |>
tidyr::drop_na(mage, gained, fage, weight)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment