Commit bc60fd29 authored by Facundo Muñoz's avatar Facundo Muñoz ®️
Browse files

bugfix: check for reciprocity

parent 528f13c1
Package: mapMCDA
Title: Produce an epidemiological risk map by weighting multiple risk factors
Version: 0.3.0.9000
Version: 0.3.1.9000
Date: 2018-06-27
Authors@R: c(
person("Elena", "Arsevska", email = "elena.arsevska@cirad.fr", role = c("ctb")),
......
......@@ -25,15 +25,12 @@
#' compute_weights(matrix(1, 5, 5))
compute_weights <- function(x) {
is.reciprocal <- function(x) {
all(diag(x) == 1) && all(x[lower.tri(x)]*x[upper.tri(x)] == 1)
}
stopifnot(
is.numeric(x),
is.matrix(x),
diff(dim(x)) == 0,
is.reciprocal(x)
is.reciprocal(x),
all(x > 0)
)
# ## Dummy outcome
......
#' Check reciprocal matrix
#'
#' Check whether a matrix is reciprocal. This is, whether the
#' entrywise product of itself with its transpose (Hadamard product)
#' is 1 for all its elements.
#'
#' @param x square numeric matrix or number
#'
#' @return logical
#'
#' @examples
#' Xf <- matrix(1:16, 4, 4)
#' Xr <- Xf
#' Xr[upper.tri(Xf)] <- 1/t(Xf)[upper.tri(Xf)]
#' diag(Xr) <- 1
#' is.reciprocal(Xf) # FALSE
#' is.reciprocal(Xr) # TRUE
is.reciprocal <- function(x) {
all(x * t(x) == 1)
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/is.reciprocal.R
\name{is.reciprocal}
\alias{is.reciprocal}
\title{Check reciprocal matrix}
\usage{
is.reciprocal(x)
}
\arguments{
\item{x}{square numeric matrix or number}
}
\value{
logical
}
\description{
Check whether a matrix is reciprocal. This is, whether the
entrywise product of itself with its transpose (Hadamard product)
is 1 for all its elements.
}
\examples{
Xf <- matrix(1:16, 4, 4)
Xr <- Xf
Xr[upper.tri(Xf)] <- 1/t(Xf)[upper.tri(Xf)]
diag(Xr) <- 1
is.reciprocal(Xf) # FALSE
is.reciprocal(Xr) # TRUE
}
......@@ -37,3 +37,28 @@ test_that("compute weights", {
# expect_equal(compute_weights(x), exp_w, tol = .001)
})
test_that("is.reciprocal() works as expected", {
# Non-reciprocal matrix
Xf <- matrix(1:16, 4, 4)
# Reciprocal matrix
Xr <- Xf
Xr[upper.tri(Xf)] <- 1/t(Xf)[upper.tri(Xf)]
diag(Xr) <- 1
expect_false(is.reciprocal(Xf))
expect_true(is.reciprocal(Xr))
## -1 is also a valid value in the diagonal
## and negative values are also valid as long as they are reciprocal
expect_true(is.reciprocal(replace(Xr, 1, -1)))
expect_true(is.reciprocal(replace(Xr, c(2, 5), c(-2, -1/2))))
## Single numbers are reciprocal if 1 or -1
expect_true(is.reciprocal(1))
expect_true(is.reciprocal(-1))
expect_false(is.reciprocal(2))
})
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment