tests/testthat/test_agreement_matrix.R

context("agreement matrix creation")

test_that("agreement matrix creating works", {
    
    r = 5
    s = 5
    
    for (rr in 1:r) {
        
        k = 3
        X = matrix(sample(1:k, 50, replace = T), nrow = 5)
        m_val = sample.int(k, 1L)
        X[1, 2] = X[4, 2] = NA
        A = gen_agreemat(X, missing = m_val)
        B = gen_agreemat(X, dim = "col", missing = m_val)
        
        # check dimensions
        expect_identical(NROW(X), NCOL(A))
        expect_identical(NCOL(X), NROW(B))
        
        # check diagonals
        expect_true(all(diag(A) == rep(0, NROW(X))))
        expect_true(all(diag(B) == rep(0, NCOL(X))))
        
        # check score is correct
        for (ss in 1:s) {
            
            ij = sample.int(NROW(X), 2L)
            
            ex = sum(
                X[ij[1L],] == X[ij[2L],] & 
                X[ij[1L],] != m_val & 
                !is.na(X[ij[1L], ]) &
                !is.na(X[ij[2L], ])
            )
            
            expect_true(ex == A[ij[1L], ij[2L]])
            
            ij2 = sample.int(NCOL(X), 2L)
            ex = sum(
                X[,ij2[1L]] == X[,ij2[2L]] & 
                X[,ij2[1L]] != m_val &
                !is.na(X[, ij2[1L]]) &
                !is.na(X[, ij2[2L]])
            )
            expect_true(ex == B[ij2[1L], ij2[2L]])
        
        }
        
    }
    
    X_err = X
    X_err[ij[1L], ij[2L]] = X_err[ij[1L], ij[2L]] + 0.1
    
    expect_error(gen_agreemat(X_err))
    expect_error(gen_agreemat(X, dim = "slice"))

})
baruuum/btoolbox documentation built on Aug. 17, 2020, 1:29 a.m.