Merge branch 'setdiff' into unite-laskeMuutokset

This commit is contained in:
Waldir Leoncio 2020-10-19 15:58:26 +02:00
commit 5dd2889c54
9 changed files with 131 additions and 14 deletions

View file

@ -23,6 +23,7 @@ export(linkage)
export(logml2String) export(logml2String)
export(lueGenePopData) export(lueGenePopData)
export(lueNimi) export(lueNimi)
export(max_MATLAB)
export(min_MATLAB) export(min_MATLAB)
export(noIndex) export(noIndex)
export(ownNum2Str) export(ownNum2Str)
@ -34,6 +35,7 @@ export(randdir)
export(repmat) export(repmat)
export(rivinSisaltamienMjonojenLkm) export(rivinSisaltamienMjonojenLkm)
export(selvitaDigitFormat) export(selvitaDigitFormat)
export(setdiff_MATLAB)
export(simulateAllFreqs) export(simulateAllFreqs)
export(simulateIndividuals) export(simulateIndividuals)
export(simuloiAlleeli) export(simuloiAlleeli)

View file

@ -1,4 +1,4 @@
addToSummary <- funciton(logml, partitionSummary, worstIndex) { addToSummary <- function(logml, partitionSummary, worstIndex) {
# Tiedet<65><74>n, ett<74> annettu logml on isompi kuin huonoin arvo # Tiedet<65><74>n, ett<74> annettu logml on isompi kuin huonoin arvo
# partitionSummary taulukossa. Jos partitionSummary:ss<73> ei viel<65> ole # partitionSummary taulukossa. Jos partitionSummary:ss<73> ei viel<65> ole
# annettua logml arvoa, niin lis<69>t<EFBFBD><74>n worstIndex:in kohtaan uusi logml ja # annettua logml arvoa, niin lis<69>t<EFBFBD><74>n worstIndex:in kohtaan uusi logml ja
@ -6,13 +6,13 @@ addToSummary <- funciton(logml, partitionSummary, worstIndex) {
apu <- find(abs(partitionSummary[, 2] - logml) < 1e-5) apu <- find(abs(partitionSummary[, 2] - logml) < 1e-5)
if (isempty(apu)) { if (isempty(apu)) {
# Nyt l<>ydetty partitio ei ole viel<65> kirjattuna summaryyn. # Nyt l<>ydetty partitio ei ole viel<65> kirjattuna summaryyn.
npops <- length(unique(PARTITION)) npops <- length(unique(PARTITION))
partitionSummary[worstIndex, 1] <- npops partitionSummary[worstIndex, 1] <- npops
partitionSummary[worstIndex, 2] <- logml partitionSummary[worstIndex, 2] <- logml
added <- 1 added <- 1
} else { } else {
added <- 0 added <- 0
} }
return(list(partitionSummary = partitionSummary, added = added)) return(list(partitionSummary = partitionSummary, added = added))
} }

17
R/setdiff.R Normal file
View file

@ -0,0 +1,17 @@
#' @title Set differences of two arrays
#' @description Loosely replicates the behavior of the homonym Matlab function
#' @param A first array
#' @param B second awway
#' @param legacy if `TRUE`, preserves the behavior of
#' @return
#' @author Waldir Leoncio
#' @export
setdiff_MATLAB <- function(A, B, legacy = FALSE) {
if (is(A, "numeric") & is(B, "numeric")) {
values <- sort(unique(A[is.na(match(A, B))]))
} else if (is(A, "data.frame") & is(B, "data.frame")) {
stop("Not implemented for data frames")
}
# TODO: add support for indices (if necessary)
return(values)
}

View file

@ -0,0 +1,11 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/admixture_initialization.R
\name{admixture_initialization}
\alias{admixture_initialization}
\title{Seuraavat kolme funktiota liittyvat alkupartition muodostamiseen.}
\usage{
admixture_initialization(data_matrix, nclusters, Z)
}
\description{
Seuraavat kolme funktiota liittyvat alkupartition muodostamiseen.
}

View file

@ -1,5 +1,5 @@
% Generated by roxygen2: do not edit by hand % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/laskeMuutokset4.R % Please edit documentation in R/laskeMuutokset12345.R
\name{laskeMuutokset4} \name{laskeMuutokset4}
\alias{laskeMuutokset4} \alias{laskeMuutokset4}
\title{Calculate changes?} \title{Calculate changes?}
@ -20,6 +20,6 @@ laskeMuutokset4(osuus, osuusTaulu, omaFreqs, logml, COUNTS = matrix(0))
\description{ \description{
Palauttaa npops*npops taulun, jonka alkio (i,j) kertoo, mik?on Palauttaa npops*npops taulun, jonka alkio (i,j) kertoo, mik?on
muutos logml:ss? mikäli populaatiosta i siirretään osuuden verran muutos logml:ss? mikäli populaatiosta i siirretään osuuden verran
todennäköisyysmassaa populaatioon j. Mikäli populaatiossa i ei ole mitään todennäköisyysmassaa populaatioon j. Mikäli populaatiossa i ei ole mitään
siirrettävää, on vastaavassa kohdassa rivi nollia. siirrettävää, on vastaavassa kohdassa rivi nollia.
} }

22
man/max_MATLAB.Rd Normal file
View file

@ -0,0 +1,22 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/min_max_MATLAB.R
\name{max_MATLAB}
\alias{max_MATLAB}
\title{Maximum (MATLAB version)}
\usage{
max_MATLAB(X, indices = TRUE)
}
\arguments{
\item{X}{matrix}
\item{indices}{return indices?}
}
\value{
Either a list or a vector
}
\description{
Finds the minimum value for each column of a matrix, potentially returning the indices instead
}
\author{
Waldir Leoncio
}

View file

@ -1,11 +1,15 @@
% Generated by roxygen2: do not edit by hand % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/min.R, R/min_MATLAB.R % Please edit documentation in R/min.R, R/min_MATLAB.R, R/min_max_MATLAB.R
\name{min_MATLAB} \name{min_MATLAB}
\alias{min_MATLAB} \alias{min_MATLAB}
\title{Minimum (MATLAB version)} \title{Minimum (MATLAB version)}
\usage{ \usage{
min_MATLAB(X, indices = TRUE) min_MATLAB(X, indices = TRUE)
min_MATLAB(X, indices = TRUE)
min_MATLAB(X, indices = TRUE)
min_MATLAB(X, indices = TRUE) min_MATLAB(X, indices = TRUE)
} }
\arguments{ \arguments{
@ -16,15 +20,27 @@ min_MATLAB(X, indices = TRUE)
\value{ \value{
Either a list or a vector Either a list or a vector
Either a list or a vector
Either a list or a vector
Either a list or a vector Either a list or a vector
} }
\description{ \description{
Finds the minimum value for each column of a matrix, potentially returning the indices instead Finds the minimum value for each column of a matrix, potentially returning the indices instead
Finds the minimum value for each column of a matrix, potentially returning the indices instead
Finds the minimum value for each column of a matrix, potentially returning the indices instead
Finds the minimum value for each column of a matrix, potentially returning the indices instead Finds the minimum value for each column of a matrix, potentially returning the indices instead
} }
\author{ \author{
Waldir Leoncio Waldir Leoncio
Waldir Leoncio
Waldir Leoncio
Waldir Leoncio Waldir Leoncio
} }

24
man/setdiff_MATLAB.Rd Normal file
View file

@ -0,0 +1,24 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/setdiff.R
\name{setdiff_MATLAB}
\alias{setdiff_MATLAB}
\title{Set differences of two arrays}
\usage{
setdiff_MATLAB(A, B, legacy = FALSE)
}
\arguments{
\item{A}{first array}
\item{B}{second awway}
\item{legacy}{if `TRUE`, preserves the behavior of}
}
\value{
}
\description{
Loosely replicates the behavior of the homonym Matlab function
}
\author{
Waldir Leoncio
}

View file

@ -158,12 +158,13 @@ test_that("find works as expected", {
}) })
test_that("sortrows works as expected", { test_that("sortrows works as expected", {
mx <- matrix(c(3, 2, 2, 1, 1, 10, 0, pi), 4) mx <- matrix(c(3, 2, 2, 1, 1, 10, 0, pi), 4)
expect_equal(sortrows(mx), matrix(c(1, 2, 2, 3, pi, 10, 0, 1), 4)) expect_equal(sortrows(mx), matrix(c(1, 2, 2, 3, pi, 10, 0, 1), 4))
expect_equal(sortrows(mx, 2), matrix(c(2, 3, 1, 2, 0, 1, pi, 10), 4)) expect_equal(sortrows(mx, 2), matrix(c(2, 3, 1, 2, 0, 1, pi, 10), 4))
expect_equal(sortrows(mx, 1:2), mx[order(mx[, 1], mx[, 2]), ]) expect_equal(sortrows(mx, 1:2), mx[order(mx[, 1], mx[, 2]), ])
}) })
# FIXME: failing tests
test_that("cell works as expected", { test_that("cell works as expected", {
expect_equal(cell(0), array(dim = c(0, 0))) expect_equal(cell(0), array(dim = c(0, 0)))
expect_equal(cell(1), array(dim = c(1, 1))) expect_equal(cell(1), array(dim = c(1, 1)))
@ -217,4 +218,28 @@ test_that("nargin works correctly", {
expect_equal(addme(13, 42), 55) expect_equal(addme(13, 42), 55)
expect_equal(addme(13), 26) expect_equal(addme(13), 26)
expect_equal(addme(), 0) expect_equal(addme(), 0)
})
test_that("setdiff works as expected", {
A <- c(3, 6, 2, 1, 5, 1, 1)
B <- c(2, 4, 6)
C <- c(1, 3, 5)
expect_equal(setdiff(A, B), C)
A <- data.frame(
Var1 = 1:5,
Var2 = LETTERS[1:5],
Var3 = c(FALSE, TRUE, FALSE, TRUE, FALSE)
)
B <- data.frame(
Var1 = seq(1, 9, by = 2),
Var2 = LETTERS[seq(1, 9, by = 2)],
Var3 = rep(FALSE, 5)
)
C <- data.frame(
Var1 = c(2, 4),
Var2 = c('B', 'D'),
Var3 = c(TRUE, TRUE)
)
expect_equal(setdiff(A, B), C)
# TODO: add more examples from https://se.mathworks.com/help/matlab/ref/double.setdiff.html;jsessionid=0d8d42582d4d299b8224403899f1
}) })