Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
explodecomputer / ios_aom.R
Created April 11, 2017 12:00
Age of menarche index of suspicion
library(TwoSampleMR)
library(MRInstruments)
library(ggplot2)
library(dplyr)
library(reshape2)
library(classInt)
calculate_ios <- function(exposure_dat, outcome_dat)
{
@explodecomputer
explodecomputer / survival_bias.r
Created April 6, 2017 10:21
alternative survival bias
library(dplyr)
# X causes DEATH
# P causes DEATH
# AGE causes DEATH
# G causes X
# AGE causes Y
# - IF cases and controls are AGE matched then NO SURVIVAL BIAS
n <- 1000000
@explodecomputer
explodecomputer / make_residuals.R
Created March 27, 2017 12:13
make phenotype residuals from covariates
library(data.table)
arguments <- commandArgs(T)
phenfile <- arguments[1]
covfile <- arguments[2]
outfile <- arguments[3]
lmodel <- arguments[4]
# expect lmodel to be "logistic" or "linear"
@explodecomputer
explodecomputer / example_plink.sh
Last active March 16, 2017 12:39
example plink
@explodecomputer
explodecomputer / ld.r
Created March 16, 2017 09:45
ld and imputation demo
n <- 1000
nsnp <- 10
r <- matrix(0.8, nsnp, nsnp)
diag(r) <- 1
x1 <- rmvnorm(n, rep(0, nsnp), sigma=r)
x2 <- rmvnorm(n, rep(0, nsnp), sigma=r)
x1[1:500, nsnp] <- x2[1:500, 1]
@explodecomputer
explodecomputer / snp_to_kegg_go.r
Created March 13, 2017 11:30
SNP IDs to KEGG and GO terms
# source("https://bioconductor.org/biocLite.R")
# biocLite("KEGGREST")
# biocLite("org.Hs.eg.db")
library(biomaRt)
library(KEGGREST)
library(org.Hs.eg.db)
# example snp list
snp <- c("rs1007648", "rs112275031", "rs1124639", "rs11731570", "rs11743154", "rs12439909")
@explodecomputer
explodecomputer / behaviour.r
Created March 10, 2017 22:37
genetic models of parent - offspring behaviour
n <- 10000
gm <- rbinom(n, 2, 0.5)
gp <- rbinom(n, 2, 0.5)
# approximate child genotypes
gc <- round((gm + gp) / 2)
cor(gm, gc)^2
cor(gp, gc)^2
@explodecomputer
explodecomputer / biv.rmd
Created March 6, 2017 01:51
bivariate heritability
---
title: "Bivariate heritabilities greater than 1"
date: "`r format(Sys.time(), '%d %B %Y')`"
output: pdf_document
---
the model -
$$
\begin{aligned}
@explodecomputer
explodecomputer / smaller_gwama.r
Created February 23, 2017 20:42
making smaller gwama files
1M = 16mb
a <- read.table('results_9.gz', he=T)
snplist <- as.character(unique(a$SNP))
snplist <- data.frame(SNP=snplist, snpid=1:length(snplist), stringsAsFactors=FALSE)
cpglist <- as.character(unique(a$PHENOTYPE))
cpglist <- data.frame(CPG=cpglist, cpgid=1:length(cpglist), stringsAsFactors=FALSE)
@explodecomputer
explodecomputer / ldscore_simplistic.R
Created February 10, 2017 18:15
ld score regression simplistic simulation
library(dplyr)
# Generate some LD blocks for 1000000 SNPs
# Some blocks have more SNPs than others
ld <- sample(1:1000, 1000000, replace=TRUE, prob=runif(1000))
range(table(ld))
# Generate some xsq stats for each SNP
xsq <- rchisq(1000000, df=1)