Test 001: Creation of the BinaryMatrix object Class"

This document tests the following functions:

02.binaryMatrix.R - BinaryMatrix and subsidiaries (validBinaryMatrix, [], dim, summary)

We assume that the input for all tests in the BinaryMatrix package should be in the form of the BinaryMatrix class.
The BinaryMatrix class builds a list containing at least 2 basic components:
1. A binary matrix of 0s and 1s.
2. A features set, in the form of a dataframe.

Acceptable Matrix Input

First, we must determine what class of matrix input the BinaryMatrix object accepts.
We assume our data are well-behaved in that they only consist of 0s and 1s.
We begin by creating a small, simple matrix (20x20) of randomly generated binary values (0 or 1, p = 0.5) in each class.
We create a meaningless feature set of class data.frame, which we will test further later.

library(BinaryMatrix)

set.seed(1987)
f20 <- data.frame(as.character(c(1:20)))

t_int <- matrix(as.integer(rbinom(400, 1, 0.5)), nrow = 20, ncol = 20)
bm_int <- BinaryMatrix(t_int, f20)
summary(bm_int)

t_num <- matrix(as.numeric(rbinom(400, 1, 0.5)), nrow = 20, ncol = 20)
bm_num <- BinaryMatrix(t_num, f20)
summary(bm_num)

t_char <- matrix(as.character(rbinom(400, 1, 0.5)), nrow = 20, ncol = 20)
bm_char <- BinaryMatrix(t_char, f20)
summary(bm_char)

t_log <- matrix(as.logical(rbinom(400, 1, 0.5)), nrow = 20, ncol = 20)
bm_log <- BinaryMatrix(t_log, f20)
summary(bm_log)

myfac <- factor(rbinom(400, 1, 0.5), levels = c("0","1"), labels = c("Zero", "One"))
t_fac <- matrix(myfac, nrow = 200, ncol = 200, byrow = TRUE)
try(bm_fac <- BinaryMatrix(t_fac, f20))
try(summary(bm_fac))

The BinaryMatrix object will build with matrix input of 0s and 1s in integer, numeric, or character form, or as a matrix of logicals (true/false). The object fails to build with a factor.

Acceptable Object

Clearly, the BinaryMatrix object accepts input in the form of a matrix. Does it accept other object structures?

options(try.outFile = stdout())
t_df <- data.frame(matrix(as.numeric(rbinom(400, 1, 0.5)), nrow = 20, ncol = 20))
try(bm_df <- BinaryMatrix(t_df, f20))
try(summary(bm_df))

t_vec <- rbinom(400, 1, 0.5)
try(bm_vec <- BinaryMatrix(t_vec, f20))
try(summary(bm_vec))

Only the matrix class is accepted. Data frames and vectors are not.

Acceptable Feature Set

The only components a BinaryMatrix must contain to be created are a matrix and a feature set, in the form of a data.frame.
The BinaryMatrix object cannot be created without a feature set.
For example, we know that we can load our small test matrix t_int inthe presence of the feature set f20, but we cannot load it alone.

try(mybin <- BinaryMatrix(t_int))

We confirm that we cannot form the BinaryMatrix if our features are stored as a vector or a matrix:

f_vec <- c(1:20)
try(bm_vec <- BinaryMatrix(t_int, f_vec))

f_mat <- matrix(f_vec)
try(bm_mat <- BinaryMatrix(t_int, f_mat))

The features may include data of any class.

char20 <- data.frame(as.character(c(1:20)))
bm_char <- BinaryMatrix(t_int, char20)
summary(bm_char)

num20 <- data.frame(as.numeric(c(1:20)))
bm_num <- BinaryMatrix(t_int, num20)
summary(bm_num)

int20 <- data.frame(as.integer(c(1:20)))
bm_int <- BinaryMatrix(t_int, int20)
summary(bm_int)

log20 <- data.frame(as.logical(c(1:20)))
bm_log <- BinaryMatrix(t_int, log20)
summary(bm_log)

fac20 <- data.frame(factor(c(1:20), levels = c("0", "1"), labels = c("zero", "one")))
bm_fac <- BinaryMatrix(t_int, fac20)
summary(bm_fac)

We confirm that the BinaryMatrix only accepts a feature set with a number of rows exactly equal to the number of columns in the matrix.

t_rect <- matrix(rbinom(200, 1, 0.5), nrow = 5, ncol = 10)

f10 <- data.frame(1:10)
bm_10 <- BinaryMatrix(t_rect, f10)
summary(bm_10)
bm_10

f9 <- data.frame(1:9)
try(bm_9 <- BinaryMatrix(t_rect, f9))

f11 <- data.frame(1:11)
try(bm_11 <- BinaryMatrix(t_rect, f11))

Acceptable Input

We attempt this with a matrix containing only 2, but inappropriate values. BinaryMatrix accepts an input consisting of only 1s and 2s.

t_10 <- matrix(rbinom(100, 1, 0.5), nrow = 10, ncol = 10)
t_12 <- t_10 + 1
bm_12 <- BinaryMatrix(t_12, f10)
summary(bm_12)
bm_12

BinaryMatrix will, in fact, create an inappropriate matrix containing any numerical values.

t_norm <- matrix(rnorm(100, sd = 100), nrow = 10)
bm_norm <- BinaryMatrix(t_norm, f10)
summary(bm_norm)
bm_norm

In fact, we can make a "BinaryMatrix" out of all sorts of things that are non-binary, including characters.

mych <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
t_char <- rbind(mych, mych, mych, mych, mych, mych, mych, mych, mych, mych)
dim(t_char)
bm_char <- BinaryMatrix(t_char, f10)
summary(bm_char)
bm_char

Subsetting a Matrix

We confirm that we can subset an appropriate matrix in a variety of predictable ways.

whole.mat <- matrix(rbinom(500*500, 1, 0.5), nrow=500)
whole.df <- data.frame(1:500)
whole.bm <- BinaryMatrix(whole.mat, whole.df)

part.bm <- whole.bm[100,100]
dim(part.bm)
summary(part.bm)

part2.bm <- whole.bm[1:100,1:100]
dim(part2.bm)
summary(part2.bm)

part3.bm <- whole.bm[100:199,300:349]
dim(part3.bm)
summary(part3.bm)


Try the Mercator package in your browser

Any scripts or data that you put into this service are public.

Mercator documentation built on July 27, 2023, 3:01 a.m.