Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
explodecomputer / shiny.R
Created March 19, 2016 20:40
shiny minimal examples
library(shiny)
ui <- bootstrapPage(
selectizeInput('foo', choices = NULL, label="Species")
)
server <- function(input, output, session)
{
updateSelectizeInput(session, 'foo', choices = state.name, server = TRUE)
# bash/zsh git prompt support
#
# Copyright (C) 2006,2007 Shawn O. Pearce <[email protected]>
# Distributed under the GNU General Public License, version 2.0.
#
# This script allows you to see the current branch in your prompt.
#
# To enable:
#
# 1) Copy this file to somewhere (e.g. ~/.git-prompt.sh).
@explodecomputer
explodecomputer / mr_comparison.R
Created April 12, 2016 06:41
compare 2sls with 2 sample mr
library(TwoSampleMR)
library(systemfit)
# test 2sls
b <- matrix(rbinom(10000, 2, 0.5), 1000)
eff <- rnorm(10)
g <- b %*% eff * 100
u <- rnorm(1000)
x <- scale(rnorm(1000) + g - u) * 100
@explodecomputer
explodecomputer / arachidonic.R
Last active April 15, 2016 11:42
arachidonic acid mr
library(TwoSampleMR)
ao <- available_outcome()
i <- grep("arachid", ao$trait)
b <- extract_instruments(ao$id[i[1]])
o <- extract_outcome_data(b$SNP[1], ao$id)
dat <- harmonise_data(b[1,], o)
m <- mr(dat)
ivw <- subset(m, method=="Fixed effects meta analysis (delta method)" & pval < 0.05)
ivw <- subset(ivw, !duplicated(outcome))
@explodecomputer
explodecomputer / pairings.R
Last active April 28, 2016 09:45
ProgOne pairings
library(reshape2)
get_pairs <- function(people)
{
require(reshape2)
# Create matrix of all possible pairings
# Only use pairings on diagonal with an offset of half the number of people
# This ensures that there is equal spacing between each person's turn to present (as def or pros)
p <- rep(people, 2)
@explodecomputer
explodecomputer / eff_from_z.R
Created May 6, 2016 13:15
effect and se from z
get_beta_se_from_p_z_n_vary <- function(z, n, vy, maf)
{
# qval <- qchisq(pnorm(abs(z), low=FALSE)*2, 1, low=F) / (qchisq(pnorm(abs(z), low=FALSE)*2, n-2, low=F)/(n-2))
# r <- sqrt(sum(qval / (n - qval)))
r <- sqrt(abs(z)^2 / (abs(z)^2 + n-2))
b <- sign(z) * sqrt(r^2 * vy / (2*maf*(1-maf)))
se <- b / z
return(list(b=b, se=se))
}
# library(TwoSampleMR)
library(MRInstruments)
data(gwas_catalog)
gwas_catalog_subset <- subset(gwas_catalog, Phenotype %in% c('Body mass index')& SNP %in% c('rs10938397', 'rs1516725', 'rs2568958', 'rs633715', 'rs7138803', 'rs8089364'))
exposure_dat <- format_gwas_catalog(gwas_catalog_subset)
exposure_dat <- format_data(gwas_catalog_subset, "exposure")
ao <- available_outcomes()
outcome_dat <- extract_outcome_data(exposure_dat$SNP, c(1), proxies = 1, rsq = 0.8, align_alleles = 1, palindromes = 1, maf_threshold = 0.3)
dat <- harmonise_data(exposure_dat, outcome_dat, action = 2)
mr_results <- mr(dat)
library(WeightedCluster)
data(mvad)
mvad.alphabet <- c("employment", "FE", "HE", "joblessness", "school", "training")
mvad.labels <- c("Employment", "Further Education", "Higher Education", "Joblessness", "School", "Training")
mvad.scodes <- c("EM", "FE", "HE", "JL", "SC", "TR")
mvadseq <- seqdef(mvad[, 17:86], alphabet = mvad.alphabet,
states = mvad.scodes, labels = mvad.labels,
weights = mvad$weight, xtstep = 6)
@explodecomputer
explodecomputer / romantics.txt
Created May 26, 2016 21:37
romantic poets animal rights
spring
@explodecomputer
explodecomputer / matched_logistic.R
Created May 27, 2016 10:11
matched case control logistic regression
n <- 1000
g <- rbinom(n, 2, 0.5)
y <- g + rnorm(n)
yb <- rep(0, n)
yb[y >= median(y)] <- 1
dat <- data.frame(y=y, yb=yb, g=g)
dat <- dat[order(dat$yb), ]
dat$group <- rep(1:sum(yb==0), 2)