Skip to content

Instantly share code, notes, and snippets.

View turgeonmaxime's full-sized avatar

Max Turgeon turgeonmaxime

View GitHub Profile
@turgeonmaxime
turgeonmaxime / setwd.R
Created November 23, 2015 18:27
setwd which allows for path containing Unix-like environment variables
#' Set Working Directory
#'
#' \code{setwd(dir)} is used to set the working directory to \code{dir}.
#'
#' This version of \code{setwd} is different from the one in \code{base} in that it allows for paths containing Unix-like
#' environment variables. For example, if HOME is set (globally) to your home directory, then \code{base::setwd($HOME)}
#' will not work. However, the version below will work.
#'
#' @param dir A character string: tilde expansion will be done.
@turgeonmaxime
turgeonmaxime / mapping_symb2path.R
Last active May 12, 2017 20:56
Mapping gene symbols to KEGG pathways
library(clusterProfiler)
library(org.Hs.eg.bd)
library(KEGGREST)
library(dplyr)
library(magrittr)
# List of genes of interest
x <- c("GPX3", "GLRX", "LBP", "CRYAB", "DEFB1", "HCLS1", "SOD2", "HSPA2",
"ORM1", "IGFBP1", "PTHLH", "GPC3", "IGFBP3","TOB1", "MITF", "NDRG1",
"NR1H4", "FGFR3", "PVR", "IL6", "PTPRM", "ERBB2", "NID2", "LAMB1",
library(tidyverse)
# 1. Theoretical answer
# It's the probability that both teams score no goal,
# plus the probability they both score one goal,
# plus ... plus the probability they both score five goals
#
# If we assume the scores are binomial random variables
# and that the scores of each team are independent,
# this is simply a sum of products of binomial probabilities.
# This is a short script that shows how to compute the KL divergence using the kernel density estimator
# The key point is to use linear interpolation to evaluate the density at the same points
# 1. Generate a sample of standard normal variates
x <- rnorm(100)
# 2. Compute kernel density estimator
dens_obs <- density(x)
# 3. Use linear interpolation to evaluate over a grid
library(tidyverse)
# Create dataset with fantasy scores
data_ottawa <- tibble::tribble(
~harris, ~Sinopoli, ~Spencer, ~Ellingson,
19.8, 13.4, 20.2, 18.4,
3.4, 8, 4.1, 21.7,
25.7, 31.8, 5.9, 9.9,
-0.3, 7.7, 6.6, 5.6,
18.5, 34.1, 7.8, 7.7,
library(tidyverse)
library(lubridate)
library(weathercan)
# Lethbridge and Saskatoon
data <- weather_dl(station_ids = c(2265, 50091),
start = "2012-01-01", end = "2018-08-15")
# Plot full data
data %>%
# Loop through list of departments
list_depts <- c("Adult Medicine",
"Heart Health",
"Maternal Services")
for (dept in list_depts) {
# Add underscores
dept_f <- stringr::str_replace_all(dept, " ", "_")
# Change the name of output
output <- "report_template.Rmd" %>%
@turgeonmaxime
turgeonmaxime / tampa_totalPoints.R
Created February 27, 2019 15:36
Add a trend line to see with how many points the Tampa Bay Lightnings could finish the season
library(rvest)
library(tidyverse)
library(lubridate)
url <- "https://www.hockey-reference.com/teams/TBL/2019_games.html"
webpage <- read_html(url)
results <- webpage %>%
html_table(fill = TRUE) %>%
@turgeonmaxime
turgeonmaxime / dynamic_filter.R
Created March 4, 2019 18:09
For a Shiny app: filter data based on user input. We don't know how many filtering variables will be used.
library(tidyverse)
# What I want
target <- mtcars %>%
filter(cyl %in% c(4, 8),
gear %in% c(3, 4))
# Solution
dynamic_filter <- function(df, variables, conditions){
filter_conditions <- purrr::map2(variables, conditions,
@turgeonmaxime
turgeonmaxime / typeM_error.R
Last active March 5, 2019 14:58
In noisy studies, we tend to overestimate the effect size when we select on statistical significance
set.seed(12345)
mu <- 0.5
sigma <- 1
n <- 25
B <- 1000
results <- replicate(B, {
data <- rnorm(n, mu, sigma)