Added code and tests for simuloiAlleeli

This commit is contained in:
Waldir Leoncio 2020-01-30 14:57:11 +01:00
parent 0bc86d8005
commit 309519dd08
5 changed files with 43 additions and 9 deletions

View file

@ -14,6 +14,7 @@ export(proportion2str)
export(rand) export(rand)
export(randdir) export(randdir)
export(repmat) export(repmat)
export(simuloiAlleeli)
export(suoritaMuutos) export(suoritaMuutos)
export(times) export(times)
importFrom(stats,runif) importFrom(stats,runif)

View file

@ -479,11 +479,3 @@ admix1 <- function(tietue) {
# end # end
# counter = counter+rowsFromInd; # counter = counter+rowsFromInd;
# end # end
# function all = simuloiAlleeli(allfreqs,pop,loc)
# % Simuloi populaation pop lokukseen loc alleelin.
# freqs = allfreqs(:,loc,pop);
# cumsumma = cumsum(freqs);
# arvo = rand;
# isommat = find(cumsumma>arvo);
# all = min(isommat);

17
R/simuloiAlleeli.R Normal file
View file

@ -0,0 +1,17 @@
#' @title simuloiAlleeli
#' @description Simuloi populaation pop lokukseen loc alleelin.
#' @note This function is (only?) called by `simulateIndividuals()`. Therefore, exporting it is probably unnecessary.
#' @export
simuloiAlleeli <- function(allfreqs, pop, loc) {
if (length(dim(allfreqs)) == 3) { # distinguish between arrays and matrices
freqs <- allfreqs[, loc, pop]
} else {
freqs <- allfreqs[, loc]
}
cumsumma <- cumsum(freqs)
arvo <- runif(1)
isommat <- which(cumsumma > arvo)
all <- min(isommat)
return(all)
}

14
man/simuloiAlleeli.Rd Normal file
View file

@ -0,0 +1,14 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/simuloiAlleeli.R
\name{simuloiAlleeli}
\alias{simuloiAlleeli}
\title{simuloiAlleeli}
\usage{
simuloiAlleeli(allfreqs, pop, loc)
}
\description{
Simuloi populaation pop lokukseen loc alleelin.
}
\note{
This function is (only?) called by `simulateIndividuals()`. Therefore, exporting it is probably unnecessary.
}

View file

@ -180,3 +180,13 @@ test_that("computePersonalAllFreqs works like on Matlab", {
expect_equal(computePersonalAllFreqs(1, 1:3, mx, 1), c(15, 40)) expect_equal(computePersonalAllFreqs(1, 1:3, mx, 1), c(15, 40))
expect_equal(computePersonalAllFreqs(1, 1:2, mx, 1), c(15, 40)) expect_equal(computePersonalAllFreqs(1, 1:2, mx, 1), c(15, 40))
}) })
test_that("simuloiAlleeli works like on Matlab", {
# TODO: test on vector
ra1 <- array(1:12, c(2, 2, 3))
mx1 <- matrix(c(3, 5, 0, 9), 2)
mx2 <- matrix(c(3, 5, 0, 9, 5, 8), 2)
expect_equal(simuloiAlleeli(ra1, 2, 1), 1)
expect_equal(simuloiAlleeli(mx1, 1, 2), 2)
expect_equal(simuloiAlleeli(mx2, 1, 3), 1)
})