Translated function, added unit tests

This commit is contained in:
Waldir Leoncio 2020-01-14 12:09:27 +01:00
parent 3bd031515b
commit 4a7c6f6a7a
6 changed files with 50 additions and 17 deletions

View file

@ -2,6 +2,7 @@
export(admix1) export(admix1)
export(calculatePopLogml) export(calculatePopLogml)
export(computeIndLogml)
export(computeRows) export(computeRows)
export(learn_simple_partition) export(learn_simple_partition)
export(ownNum2Str) export(ownNum2Str)

View file

@ -522,23 +522,6 @@ admix1 <- function(tietue) {
# end # end
# end # end
# %---------------------------------------------------------------------------
# function loggis = computeIndLogml(omaFreqs, osuusTaulu)
# % Palauttaa yksilön logml:n, kun oletetaan yksilön alkuperät
# % määritellyiksi kuten osuusTaulu:ssa.
# apu = repmat(osuusTaulu', [1 size(omaFreqs,2)]);
# apu = apu .* omaFreqs;
# apu = sum(apu);
# apu = log(apu);
# loggis = sum(apu);
# %-------------------------------------------------------------------------- # %--------------------------------------------------------------------------

21
R/computeIndLogml.R Normal file
View file

@ -0,0 +1,21 @@
#' @title computeIndLogml
#' @description Palauttaa yksilön logml:n, kun oletetaan yksilön alkuperät
#' määritellyiksi kuten osuusTaulu:ssa.
#' @param omaFreqs omaFreqs
#' @param osuusTaulu osuusTaulu
#' @export
computeIndLogml <- function (omaFreqs, osuusTaulu) {
apu <- repmat(t(osuusTaulu), c(1, dim(omaFreqs)[2]))
apu <- c(apu) * omaFreqs # c() avoids deprecation error re. matrix ops
if (length(apu) > 1) {
apu <- colSums(as.matrix(apu))
} else {
apu <- sum(apu)
}
apu = log(apu)
loggis <- sum(apu)
return (loggis)
}

View file

@ -30,6 +30,7 @@ Function | Argument | Value | Matlab output | R output
`ownNum2Str` | `number` | `NaN` | `'NAN'` | error `ownNum2Str` | `number` | `NaN` | `'NAN'` | error
`ownNum2Str` | `number` | `<vector>` | `'<vector elements>'` | `'<vector elements>'` + warning `ownNum2Str` | `number` | `<vector>` | `'<vector elements>'` | `'<vector elements>'` + warning
`repmat` | `length(n)` | `> 2` | > 2D matrix | 2D matrix `repmat` | `length(n)` | `> 2` | > 2D matrix | 2D matrix
`computeIndLogml` | only one of the arguments is negative | complex number | `NaN`
As general remarks, one should keep in mind that: As general remarks, one should keep in mind that:

17
man/computeIndLogml.Rd Normal file
View file

@ -0,0 +1,17 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/computeIndLogml.R
\name{computeIndLogml}
\alias{computeIndLogml}
\title{computeIndLogml}
\usage{
computeIndLogml(omaFreqs, osuusTaulu)
}
\arguments{
\item{omaFreqs}{omaFreqs}
\item{osuusTaulu}{osuusTaulu}
}
\description{
Palauttaa yksilön logml:n, kun oletetaan yksilön alkuperät
määritellyiksi kuten osuusTaulu:ssa.
}

View file

@ -108,3 +108,13 @@ test_that("computeRows behaves like on Matlab", {
expected = matrix(rep(-26:-24, 10)) expected = matrix(rep(-26:-24, 10))
) )
}) })
test_that("computeIndLogml works like on Matlab", {
expect_equivalent(computeIndLogml(10, 1), 2.3026, tol = .0001)
expect_equivalent(computeIndLogml(0, 1), -Inf)
expect_equivalent(computeIndLogml(1, 0), -Inf)
expect_equivalent(computeIndLogml(0, 0), -Inf)
expect_equivalent(computeIndLogml(-pi, -8), 3.2242, tol = .0001)
expect_equivalent(computeIndLogml(2:3, 2), 2.3026, tol = .0001)
expect_equivalent(computeIndLogml(matrix(8:5, 2), 100), 14.316, tol = .001)
})