Skip to content

Instantly share code, notes, and snippets.

View simmwill's full-sized avatar
🐛

Will Simmons simmwill

🐛
View GitHub Profile
@simmwill
simmwill / see_sql_tables.sql
Created April 2, 2025 14:31
See list of all tables in SQL database (maybe incomplete)
use [database];
select * from INFORMATION_SCHEMA.tables;
@simmwill
simmwill / dbplyr_copy_error.txt
Last active January 14, 2025 18:56
copy = TRUE error in dbplyr joins
When you get this error or similar:
`error in `db_copy_to()`: can't copy data to table ... `
Check the installed version of DBI in R. You may need to install an earlier version (1.1.3 has worked in the past) instead of anything later than that.
You can do this using:
devtools::install_version("DBI", version = "1.1.3")
table |>
cols_label_with(fn = ~md(glue::glue("**{.}**")))
@simmwill
simmwill / table_labels_from_redcap.R
Last active December 6, 2023 18:20
Setting labels for Table 1 variables using crosswalk of variable labels, e.g. from REDCap dictionary
labelled::set_variable_labels(
.labels = set_names(
as.list(dic$field_label), # label
dic$field_name # column name
),
.strict = FALSE
)
@simmwill
simmwill / rubins_rules.R
Created March 8, 2023 18:25
Rubin’s Rules
#' Rubin's Rules
#'
#' @param x A data.frame or similar containing one column of imputed point estimates and one column of imputed standard errors
#' @param estims character; name of the column in \code{x} containing imputed point estimates
#' @param stds character; name of column in \code{x} containing imputed standard errors
#'
#' @return A data.frame with two columns containing one row each: theta, the aggregate point estimate; and std.error, the aggregate standard error
#'
#' @examples
rubins_rules <- function(x, estims, stds) {
@simmwill
simmwill / remove_header_tbl_summary.R
Created December 14, 2022 19:40
Removing the "Characteristic" header from tbl_summary table
tbl |>
modify_header(label = "")
```{r}
# Function to create tabs - we'll iterate over this
# Uses Pandoc's div structure (where ::: is analogous to RMarkdown's #) and panel structure (using {.panel})
make_tab <- function(tab_name) {
cat("::: {.panel}\n") # Open tab
cat("##", as.character(tab_name), "{.panel-name}\n") # Label tab
@simmwill
simmwill / fisher_sim_p.R
Last active September 22, 2022 18:55
Using simulated p-values for Fisher's Exact Tests in gtsummary tables
# Sometimes, due to convergence issues, Fisher's Exact Test will exclude a p-value from a tbl_summary() table.
# To get around this, create a custom stat that will be used in your tbl_summary() call:
fisher_sim_p <- function(data, variable, by, ...) {
result <- list()
result$p <- stats::fisher.test(x = data %>% pull({{variable}}), y = data %>% pull({{by}}), simulate.p.value = T)$p.value
result$test <- "Fisher's test with simulated p-value"
result
}
# If you want to annotate a faceted ggplot (e.g., one that uses facet_wrap() to facet across a factor variable), use the following form:
# first, create a dataframe to contain your annotation for each facet, making sure to include a column for the facet it'll be included in
ann_text <- data.frame(year = rep(2006, 3),
n = rep(2000000, 3),
x = c(2005, 2005, 2010),
y = rep(1750000, 3),
xend = c(2001, 1998, 2020),
yend = c(1157, 75621, 101503),
lab = c("Office visits go back to\napproximately 2001.",
library(crimCV)
library(dplyr)
library(ggplot2)
# load toy data included with crimCV package
data(TO1adj)
# fitting model - don't need to run this, but helpful if need toy data to plot
out1 <- crimCV(TO1adj, 2, dpolyp = 2, rcv = TRUE)