Skip to content

Instantly share code, notes, and snippets.

@sashagusev
sashagusev / sibling_admixture.R
Created September 18, 2025 12:55
Simulation of within-sibling admixture analysis for a diverging trait
set.seed(42)
options(digits=3)
# --- Parameters
# sibling pairs
N = 100e3
# unadmixed individuals (for population estimate)
N_unadmixed = 10e3
# number of variants
M = 100
@sashagusev
sashagusev / proportion_epistasis_additive.R
Created August 31, 2025 01:10
Simulating epistatic phenotypes and estimating their additive heritability
library("RColorBrewer")
# ---
# Parameters
# ---
# Samples
N = 2e3
# Epistatic heritability :
hsq_epistatic = 0.5
# Allele frequencies to use
@sashagusev
sashagusev / epistasis_alleles.R
Created August 28, 2025 01:51
simple epistasis model
clr = c("#d53e4f","#fc8d59","#99d594")
n=500e3
hsq = 0.5
seeds = 10
par(mfrow=c(1,4))
for ( maf in c(0,0.05,0.2,0.5) ) {
plot(0,0,xaxt="n",las=1,type="n",pch=19,ylab="Phenotype",xlab="Genotype",ylim=c(-1,30),xlim=c(0,2),bty="n",main="")
axis(side=1,at=c(0,1,2),labels = c("AA","Aa","aa"))
hsq_add = rep(NA,seeds)
library(survival)
# ---
# Parameters
# ---
# number of individuals
n = 50e3
# desired hazard ratio
HR = 1.64
beta = log(HR)
# Fixed parameters
N = 10e6
AM_cor = 0.50
for ( Cultural_cor in c(sqrt(0.15),0.51) ) {
cat("\nSimulating cultural transmission of" , Cultural_cor , '\n' )
# Simulate two parental traits
y_mother = rnorm(N,0,1)
y_father = rnorm(N,0,1)
set.seed(42)
### --- REML
# Original code:
# https://github.com/sashagusev/SKK-REML-sim/blob/master/func_reml.R
library('msm')
# Utility function for calculating log of determinant
`logdet` <-
# Simulations and visualization of a heritable liability threshold model across generations and in families
# ---
# Liability functions (from Baselmans et al.)
# See: https://github.com/BartBaselmans/CHARRGe
risk_from_liability = function( prev , h2 ) {
# Threshold
Tx = -qnorm(prev,0,1)
z = dnorm(Tx)
i = z/prev
set.seed(1)
# Library for GLMM computations
library(PQLseq2)
# Library for mixed model / Wald test
library("GMMAT")
# convenience function to suppress prints
quiet <- function(x) {
sink(tempfile())
set.seed(42)
# Library for GLMM computations
library(PQLseq2)
# Library for mixed model / Wald test
library("GMMAT")
# convenience function to suppress prints
quiet <- function(x) {
sink(tempfile())
set.seed(42)
# Library for GLMM computations
library(PQLseq2)
quiet <- function(x) {
sink(tempfile())
on.exit(sink())
invisible(force(x))
}