Skip to content

Instantly share code, notes, and snippets.

View malcolmbarrett's full-sized avatar

Malcolm Barrett malcolmbarrett

View GitHub Profile
# Prepare a question about this code and post it on the RStudio Community thread listed in the assignment.
# 1. Ask a clear question about the code in English. Tell us what you expect and what is happening.
# 2. Use the reprex package to create an example in R.
# Your reprex should be something that I can copy and paste on my machine and run right away.
# Do NOT just share a screenshot of your RStudio session.
# Consider these questions while preparing your reprex:
# What do we expect this code to do, and what is happening?
# Are the data accesible? Do we need to use the diabetes dataset, or will a built-in dataset do?
# Is every part of this code necessary to show the problem?
library(tidyverse)
library(broom)
library(rsample)
# remotes::install_github("malcolmbarrett/cidata")
library(cidata)
# fit ipw model for a single bootstrap sample
fit_ipw <- function(split, ...) {
# get bootstrapped data sample
.df <- analysis(split)
library(gt)
library(htmltools)
euro_table <- tibble::tribble(
~Country, ~`Pct Above Normal`, ~`Excess Deaths`, ~`Time Period`,
"United Kingdom", 67, 53300, "Mar. 14 - May 1",
"Spain", 60, 31500, "Mar. 16 - May 3",
"Belgium", 50, 5300, "Mar. 16 - Apr. 19",
"Netherlands", 50, 8700, "Mar. 16 - Apr. 26",
"Italy", 49, 24600, "March",
set.seed(1234)
c <- rnorm(1000)
e <- c * 2 + rnorm(1000)
o <- e * 3 + c *.5 + rnorm(1000)
s <- o * -1.5 + rnorm(1000)
df <- data.frame(c, e, o, s)
broom::tidy(lm(o ~ e + c, data = df))
broom::tidy(lm(o ~ e + c + s, data = df))
library(ggdag)
dag <- dagify(
x ~ z,
y ~ z,
exposure = "x",
outcome = "y"
) %>%
tidy_dagitty()
dag %>%
transform_to_rr <- function(estimate, type) {
if (type == "or") estimate <- sqrt(estimate)
if (type == "hr") {
estimate <- (1 - 0.5^sqrt(estimate)) / (1 - 0.5^sqrt(1 / estimate))
}
if (estimate < 1) estimate <- 1 / estimate
estimate
}
---
title: "Presentation Ninja"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
date: "2016/12/12 (updated: `r Sys.Date()`)"
output:
xaringan::moon_reader:
css: ["default", "hiddencloud"]
lib_dir: libs
nature:
library(ggplot2)
df <- data.frame(
y = c(42, 71, 76, 79),
ci_lower = c(40, 69, 74, 77),
ci_upper = c(44, 73, 78, 81),
year = c("baseline", "year 1", "year 2", "year 3")
)
ggplot(df, aes(x = year, y = y)) +
geom_col(fill = "#0072B2", width = .7) +
library(ggdag)
confounder_triangle() %>%
tidy_dagitty() %>%
dplyr::mutate(dashed = ifelse(to == "y", "dashed", "solid")) %>%
ggplot(aes(x, y, xend = xend, yend = yend)) +
geom_dag_point() +
geom_dag_text() +
geom_dag_edges_link(
aes(label = to, edge_linetype = dashed),
angle_calc = 'along',
@malcolmbarrett
malcolmbarrett / move_slides_to_web.R
Last active April 2, 2019 10:28
Move R Markdown HTML slides to Blogdown and Push to Web
# install.packages(c("here", "fs", "stringr", "purrr", "git2r"))
# to add invisibly in your R profile, open with usethis::edit_r_profile()
# then define it in an environment, e.g.
# .env <- new.env()
# .env$move_slides_to_web <- {function definition}
move_slides_to_web <- function(folder = NULL, index = NULL) {
if (is.null(folder)) {