Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
explodecomputer / simulate_correlated_snps.R
Created January 5, 2016 20:35
simulate correlated SNPs
simulate_two_correlated_snps <- function(n, p1, p2, r)
{
D <- r * sqrt((1-p1) * (1-p2) * p1 * p2)
p11 <- p1 * p2 + D
p12 <- p1 * (1 - p2) - D
p21 <- (1 - p1) * p2 - D
p22 <- (1-p1) * (1-p2) + D
if(any(c(p11, p12, p21, p22) < 0))
{
@explodecomputer
explodecomputer / survival.Rmd
Created December 31, 2015 18:45
survival notes
---
title: Notes on survival analysis
author: Gibran Hemani
date: "`r Sys.Date()`"
output: pdf_document
bibliography: survival.bib
---
These notes are based on [http://data.princeton.edu/wws509/notes](http://data.princeton.edu/wws509/notes).
@explodecomputer
explodecomputer / snp_lookup.R
Last active December 7, 2015 17:39
SNP lookup with ld proxies
# In R:
load("/panfs/panasas01/shared/alspac/deprecated/gib/aries_renormalised/condanal_results.RData")
mprobes <- c("cg06500161", "cg11024682", "cg07094298", "cg04011474", "cg10192877", "cg17782974", "cg20496314", "cg07202479", "cg09494176", "cg17501210", "cg21139312", "cg26403843")
b <- subset(condres, CPG %in% mprobes)
write.table(unique(b$SNP), file="~/snplist.txt", row=F, col=F, qu=F)
@explodecomputer
explodecomputer / missingness_pcs.R
Created December 4, 2015 12:37
missingness and PCs
impute <- function(X)
{
apply(X, 2, function(x) {
x[is.na(x)] <- mean(x, na.rn=T)
return(x)
})
}
# Original matrix
@explodecomputer
explodecomputer / extract_bgen.sh
Created November 25, 2015 15:04
extract SNPs and IDs from bgen files
#!/bin/bash
while [[ $# > 1 ]]
do
key="$1"
case $key in
-g|--rootname)
genort="${2}"
shift
@explodecomputer
explodecomputer / rowcors.R
Created November 22, 2015 17:46
row correlations between two matrices
rowCors = function(x, y)
{
sqr = function(x) x*x
if(!is.matrix(x)||!is.matrix(y)||any(dim(x)!=dim(y)))
stop("Please supply two matrices of equal size.")
x = sweep(x, 1, rowMeans(x))
y = sweep(y, 1, rowMeans(y))
cor = rowSums(x*y) / sqrt(rowSums(sqr(x))*rowSums(sqr(y)))
return(cor)
}
@explodecomputer
explodecomputer / aries.R
Created November 21, 2015 12:34
aries data
load("aries_funnorm.randomeffect.pc10.160915.Robj")
load("samplesheet_aries.160915.Robj")
# Make sure that all the sample_names in samplesheet match the colnames in norm.beta.random
all(samplesheet$Sample_Name == colnames(norm.beta.random))
# This is not the case so we need to match things up
# Get the intersect
ids <- intersect(samplesheet$Sample_Name, colnames(norm.beta.random))
@explodecomputer
explodecomputer / bmicor.R
Created November 16, 2015 22:35
bmi in alspac
library(reshape2)
library(ggplot2)
library(alspac)
b <- findVars("bmi")
b <- subset(b, cat2=="Clinic" & cat3 == "Child")
bmi <- extractVars(b)
c <- cor(bmi[,-c(1:3)], use="pair")
@explodecomputer
explodecomputer / prediction.R
Created November 12, 2015 23:07
Prediction using absolute values vs positive and negative values
m <- 100
n <- 100
eff <- rnorm(m)
x <- matrix(rbinom(m*n,2,0.5), n, m)
yhat <- x %*% eff
@explodecomputer
explodecomputer / smoking_age.Rmd
Last active May 4, 2016 09:54
smoking and age simulation
---
title: A frailty effect is sufficient to explain the apparent association of a smoking heaviness variant on smoking initiation in a cross sectional population
date: "`r Sys.Date()`"
output: pdf_document
bibliography: smoking_age.bib
---
```{r, echo=FALSE}
opts_chunk$set(warning=FALSE, message=FALSE, echo=FALSE)
```