Implemented simulateIndividuals

This commit is contained in:
Waldir Leoncio 2020-01-30 16:20:36 +01:00
parent e645d00fca
commit 2e8ad9a89d
5 changed files with 60 additions and 25 deletions

View file

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

View file

@ -453,29 +453,4 @@ admix1 <- function(tietue) {
# simuloidut = randdir(counts(1:noalle(j),j,i) , noalle(j));
# allfreqs(1:noalle(j),j,i) = simuloidut;
# end
# end
# %--------------------------------------------------------------------------
# function refData = simulateIndividuals(n,rowsFromInd,allfreqs,pop, missing_level)
# % simulate n individuals from population pop, such that approximately
# % proportion "missing_level" of the alleles are present.
# nloci = size(allfreqs,2);
# refData = zeros(n*rowsFromInd,nloci);
# counter = 1; % which row will be generated next.
# for ind = 1:n
# for loc = 1:nloci
# for k=0:rowsFromInd-1
# if rand<missing_level
# refData(counter+k,loc) = simuloiAlleeli(allfreqs,pop,loc);
# else
# refData(counter+k,loc) = -999;
# end
# end
# end
# counter = counter+rowsFromInd;
# end

27
R/simulateIndividuals.R Normal file
View file

@ -0,0 +1,27 @@
#' @title Simulate individuals
#' @description simulate n individuals from population pop, such that
#' proportion "missing_level" of the alleles are present.
#' @export
simulateIndividuals <- function(n, rowsFromInd, allfreqs, pop, missing_level) {
nloci <- size(allfreqs, 2)
refData <- zeros(n * rowsFromInd, nloci)
counter <- 1 # which row will be generated next.
for (ind in 1:n) {
for (loc in 1:nloci) {
for (k in 0:(rowsFromInd - 1)) {
if (runif(1) < missing_level) {
refData[counter + k, loc] <- simuloiAlleeli(
allfreqs, pop, loc
)
} else {
refData[counter + k, loc] <- -999
}
}
}
counter <- counter + rowsFromInd
}
return(refData)
}

View file

@ -0,0 +1,12 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/simulateIndividuals.R
\name{simulateIndividuals}
\alias{simulateIndividuals}
\title{Simulate individuals}
\usage{
simulateIndividuals(n, rowsFromInd, allfreqs, pop, missing_level)
}
\description{
simulate n individuals from population pop, such that
proportion "missing_level" of the alleles are present.
}

View file

@ -189,4 +189,24 @@ test_that("simuloiAlleeli works like on Matlab", {
expect_equal(simuloiAlleeli(ra1, 2, 1), 1)
expect_equal(simuloiAlleeli(mx1, 1, 2), 2)
expect_equal(simuloiAlleeli(mx2, 1, 3), 1)
})
test_that("simulateIndividuals works like on Matlab", {
expect_equal(
object = simulateIndividuals(1, 3, 2, 0, .1),
expected = matrix(rep(-999, 3), 3)
)
expect_equal(
object = simulateIndividuals(5, 3, 1:3, 4, 0),
expected = matrix(rep(-999, 15 * 3), 15)
)
expect_equal(
object = simulateIndividuals(3, 3, 2, 1, 1),
expected = matrix(rep(1, 9), 9)
)
set.seed(2)
expect_equal(
object = sum(simulateIndividuals(3, 3, 2, 1, .5) == 1),
expected = 6
)
})