Skip to content

Instantly share code, notes, and snippets.

View romunov's full-sized avatar

Roman Luštrik romunov

View GitHub Profile
@romunov
romunov / difflengths.R
Last active June 3, 2020 10:37
two different length data.frames on one plot
xy1 <- data.frame(x = rnorm(10), y = rnorm(10))
xy2 <- data.frame(x = rnorm(5), y = rnorm(5))
library(ggplot2)
ggplot() +
theme_bw() +
geom_point(data = xy1, aes(x = x, y = y)) +
geom_point(data = xy2, aes(x = x, y = y), color = "red")
@romunov
romunov / rename.R
Last active February 6, 2016 14:15
rename column names given a data.frame of old and new values
xy <- data.frame(x = 1:3, y = letters[1:3], z = LETTERS[1:3])
chg <- data.frame(old = colnames(xy), new = toupper(colnames(xy)))
library(plyr)
colnames(xy) <- mapvalues(x = colnames(xy),
from = as.character(chg$old),
to = as.character(chg$new))
@romunov
romunov / .md
Created October 28, 2015 08:18
how to label a busy plot
library(vegan)
N <- 1000
xy <- data.frame(x = rnorm(N), y = rnorm(N), z = rnorm(N))

pca <- prcomp(xy)
plot(pca$x, type = "n")
orditorp(x = pca, display = "sites", air = 2)
@romunov
romunov / gist:5d551058128cb8c35747
Created June 22, 2015 10:48
daily question rate for [r] tag on SO
qs <- read.table("number_r_qs_per_day.csv", header = TRUE, sep = ",")
qs$r <- NULL
qs$Datum <- as.Date(with(qs, paste(Year, Month, Day, sep = "-")), format = "%Y-%m-%d")
qs$Weekday <- as.POSIXlt(qs$Datum)$wday
qs <- qs[order(qs$Datum), ]
qs$CumSum <- cumsum(qs$Total)
qs$Workday <- NA
qs$Workday[qs$Weekday %in% c(0, 6)] <- "Weekend"
qs$Workday[qs$Weekday %in% 1:5] <- "Non-Weekend"
@romunov
romunov / gist:4bc695e168cc14f3f0b8
Created April 22, 2015 11:03
write Ne_LD_sp results to a file
def splitCalcMerge(pop, param):
# Calculate population sizes to help with splitting (see below).
pop_size_croatia = pop.subPopSize(0)
pop_size_slovenia = pop.subPopSize(1)
pop_sizes = {"1":pop_size_croatia, "3":pop_size_slovenia}
# Split population into two. First subpopulation is the main one minus the
# sample size (ss).
sim.splitSubPops(pop = pop, subPops = 0, sizes = [pop_size_croatia - param["croatia"], param["croatia"]])
sim.splitSubPops(pop = pop, subPops = 2, sizes = [pop_size_slovenia - param["slovenia"], param["slovenia"]])
@romunov
romunov / gist:02a030498312a7937538
Created April 15, 2015 09:52
writing to a file from a function
# -*- coding: utf-8 -*-
import simuPOP as sim
import numpy as np
from simuPOP.utils import viewVars
pop = sim.Population(size = [550, 400], loci = 10, infoFields = ["age", "migrate_to"], subPopNames = ["croatia", "slovenia"])
sim.initInfo(pop = pop, values = np.random.choice([1, 2, 3, 4, 5], replace = True, size = 200), infoFields = "age")
sim.initGenotype(pop = pop, prop = [0.1]*10)
sim.initSex(pop, maleProp = 0.5)
@romunov
romunov / gist:c8e64d1f38d54dd687f6
Last active August 29, 2015 14:17
migrating individuals from one VSP to a neighboring SP
# this example demostrantes that two individuals are not migrated from VSP [1, 1] to subpopulation 0
import simuPOP as sim
import numpy as np
pop = sim.Population(size = [100, 100], loci = 3, infoFields = ["age", "migrate_to"])
sim.initInfo(pop = pop, values = np.random.negative_binomial(n = 3, p = 0.4, size = 20), infoFields = "age")
sim.initSex(pop, maleProp = 0.5)
pop.setVirtualSplitter(sim.ProductSplitter(splitters = [
sim.SexSplitter(),
# -*- coding: utf-8 -*-
import simuPOP as sim
import numpy as np
import random
from math import exp
#from scipy.stats import poisson # for poisson pmf
popHR = 2000
popSI = 1000
population_size = popHR + popSI
@romunov
romunov / gist:27b392b2092b989ff036
Last active August 29, 2015 14:15
problem installing adegenet from github through devtools

I'm trying to install adegenet package using install_github(), but I get a few errors. I'm able to install the package if I build and install the package from console. For the latter output see below. I'm using 64-bit Windows 7. I wonder to what can we attribute the discrepancy between these two ways of installing a package?

Installation through devtools (fail)

> install_github("thibautjombart/adegenet/pkg")
Downloading github repo thibautjombart/adegenet@master
Installing adegenet
"C:/PROGRA~1/R/R-31~1.2/bin/x64/R" --vanilla CMD INSTALL  \
  "C:/Users/romunov/AppData/Local/Temp/RtmpSo6w8C/devtools15882f7c4022/thibautjombart-adegenet-8cca9e1/pkg"  \
  --library="C:/Users/romunov/Documents/R/win-library/3.1" --install-tests 
@romunov
romunov / gist:9e65d246146095166c57
Last active August 29, 2015 14:15
sample a population based on variable according to a parametric distribution

Imagine having a population where age of individuals is known. I would like to sample from this population individuals by age according to a parametric distribution (e.g. X ~ Poisson(1.82)). In other words, I would like to remove a known number of individuals from each class, where class sizes follows a Poisson distribution.

Is there a more elegant way?

ma <- 10 # max age
ss <- 50 # sample size
l <- 1.82 # lambda in Poisson distribution

set.seed(357)