diff --git a/NAMESPACE b/NAMESPACE index 1487b23..82c7796 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export(admix1) export(calculatePopLogml) +export(computeIndLogml) export(computeRows) export(learn_simple_partition) export(ownNum2Str) diff --git a/R/admix1.R b/R/admix1.R index 460511a..a7e9b9b 100644 --- a/R/admix1.R +++ b/R/admix1.R @@ -522,23 +522,6 @@ admix1 <- function(tietue) { # 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); - - # %-------------------------------------------------------------------------- diff --git a/R/computeIndLogml.R b/R/computeIndLogml.R new file mode 100644 index 0000000..ff15d90 --- /dev/null +++ b/R/computeIndLogml.R @@ -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) +} \ No newline at end of file diff --git a/TODO.md b/TODO.md index af48ff9..1ba5df4 100644 --- a/TODO.md +++ b/TODO.md @@ -30,6 +30,7 @@ Function | Argument | Value | Matlab output | R output `ownNum2Str` | `number` | `NaN` | `'NAN'` | error `ownNum2Str` | `number` | `` | `''` | `''` + warning `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: diff --git a/man/computeIndLogml.Rd b/man/computeIndLogml.Rd new file mode 100644 index 0000000..6457c2b --- /dev/null +++ b/man/computeIndLogml.Rd @@ -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. +} diff --git a/tests/testthat/test-admix1.R b/tests/testthat/test-admix1.R index 557db66..2d4962e 100644 --- a/tests/testthat/test-admix1.R +++ b/tests/testthat/test-admix1.R @@ -107,4 +107,14 @@ test_that("computeRows behaves like on Matlab", { object = computeRows(3, Z, 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) }) \ No newline at end of file