Skip to content

Instantly share code, notes, and snippets.

View sachsmc's full-sized avatar

Michael Sachs sachsmc

View GitHub Profile
@sachsmc
sachsmc / example.html
Created February 1, 2022 21:13
Product integral on the web
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="prodint.css">
<title>Product integral</title>
</head>
<body>
#data source:
# https://scb.se/om-scb/nyheter-och-pressmeddelanden/overdodligheten-fortsatter-att-sjunka-efter-toppen-i-april/
library(readxl)
library(excessmort)
library(tidyr)
dset <- read_excel("2020-05-22-preliminar_statistik_over_doda_inkl_eng.xlsx", sheet = 2,
skip = 6)
dset2 <- dset[-nrow(dset),]
@sachsmc
sachsmc / forest-plots.R
Created November 21, 2017 10:17
gg forest plots
ggplot(test, aes(x = group.long, y = est, ymin = low, ymax = high, alpha = factor(alpha))) +
geom_hline(yintercept = 1, color = "grey45", linetype = 2) +
geom_hline(yintercept = 0, color = "black") +
geom_text(aes(y = yev, label = ev, alpha = factor(alpha)), size = 3) +
geom_pointrange(shape = 18) + coord_flip() + facet_grid(~ type) +
scale_y_log10(limits = c(1e-3, max(test$high)), breaks = c(0.01, 1, 10), labels = c(0.01, 1, 10)) +
scale_x_discrete(labels = group.labs) +
theme(legend.position = "bottom") +
ylab("Adjusted hazard ratio versus matched general population") +
@sachsmc
sachsmc / dpp-poc.R
Created April 25, 2017 16:29
trial level sampling
X <- rnorm(n, 1)
Y <- -.5 + 1.5 * X + rnorm(n, sd = .5)
enns <- sample(ceiling(runif(n, 200, 2000)))
samp.indi.data <- function(i) {
Z <- rbinom(enns[i], 1, .5)
mu.S <- 2.5 + X[i] * Z
lambda.T <- -1 + Y[i] * Z
@sachsmc
sachsmc / delay_code.Rmd
Created August 24, 2016 08:30
How to delay chunk echo and easily print all code at the end using knitr
---
title: "Untitled"
author: "Michael C Sachs"
date: "24 augusti 2016"
output: html_document
---
```{r setup, include=FALSE}
library(knitr)
@sachsmc
sachsmc / permute.R
Created June 15, 2016 14:36
A clever (?) but probably dangerous way to do resampling
env.copy <- new.env(parent = parent.env(environment(psdesign$likelihood)))
ps.copy <- psdesign
objs <- ls(environment(psdesign$likelihood))
for(j in objs){
assign(j, get(j, environment(psdesign$likelihood)), envir = env.copy)
}
assign("Y.trt", mixup(env.copy$Y.trt, env.copy$trtmat[, "Z"]), envir = env.copy)
assign("Y.untrt", mixup(env.copy$Y.untrt, env.copy$untrt.expand[, "Z"]), envir = env.copy)
@sachsmc
sachsmc / download-data.R
Created April 8, 2016 17:46
Exporting data and figures from Shiny apps
## Inside shinyServer(function(input, output, session) {})
output$downloadData <- downloadHandler(
filename = function() { paste("NIH-percent-", input$yvar, ".xlsx", sep = "") },
content = function(file) {
myStyles <- c(rep("textStyle", ifelse(is.null(byvar2()), 2, 3)),
ifelse(input$yvar == "investigators", "countStyle",
"moneyStyle"), "percStyle")
@sachsmc
sachsmc / plotROC-custom-labels.R
Created February 25, 2016 21:45
Workaround to plot labels at precise points using plotROC
library(plotROC)
D <- rbinom(100, 1, .4)
fkd <- data.frame(D = D, Y = rnorm(100, mean = D, sd = .5))
p1 <- ggplot(fkd, aes(d = D, m = Y)) + geom_roc(n.cuts = 0)
dat <- ggplot_build(p1)$data[[1]]
mycut.i <- c(10, 50, 90) # find the indices of the labels you want to plot in dat above
p1 + geom_point(data = dat[mycut.i, ],
@sachsmc
sachsmc / .block
Last active February 17, 2016 18:47
Spotify web API
license: mit
@sachsmc
sachsmc / .block
Last active February 17, 2016 18:00
US Budget Proposal Data 2017
license: mit