tests/testthat/test-PartInv_multi.R

result <- structure(
  list(`1` = list(
    lambda = structure(
      c(0.321851420828946, 
        0.65505328221763, 0.644928823284184, 0.745498551641102, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.569789973707324, 
        0.341398323887283, 0.397589912762831, 0.745400693889335, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.6572331474069, 
        0.803018980949713, 0.798604339572203, 0.685778873051152, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.50691173408154, 
        0.824339800976872, 0.599603442972015, 0.825307748488813, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.511822774613899, 
        0.425281197599131, 0.484803397473042, 0.569706848613303), 
      .Dim = c(20L, 5L), 
      .Dimnames = list(c("a2", "a5", "a7", "a9", "c3", "c4", "c8", 
                         "c9", "e1", "e4", "e6", "e7", "n1", "n2", "n6", "n8", "i2", "i8", 
                         "i9", "i10"), 
                       c("A", "C", "E", "N", "O")), 
      class = c("lavaan.matrix", "matrix")), 
    theta = structure(
      c(0.543886582321731, 0.172041746020309, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0120358623284618, 0, 0, 0, 0, 0, 
        0, 0, 0, 0.172041746020309, 0.536443955046005, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.410358018613791, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.356096075938922, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.148959243230043, 
        0, 0, 0, 0, 0, 0.934929805416671, 0, 0, 0, 0, 0, -0.232254971317054, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.409621144057789, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.495104906416798, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.924828156814879, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.78444201074091, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.991446629333851, 
        0, -0.1069952336659, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.232254971317054, 
        0, 0, 0, 0, 0, 0.880871827235737, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0.0120358623284618, 0, 0, 0, 0, 0, 0, 0, 0, -0.1069952336659, 
        0, 0.611527086912206, 0, -0.0226876197802865, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.66269941620454, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.0226876197802865, 
        0, 0.569648257556627, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0.747797205162342, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.651913135519311, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.73485217216192, 
        0, 0, 0.312250352868153, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 1.18247806913975, 0.457286841547525, 0, 0, 0, 
        0, 0.148959243230043, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0.457286841547525, 1.01537514466963, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0.312250352868153, 0, 0, 0.785695412105443
      ),
      .Dim = c(20L, 20L), 
      .Dimnames = list(c("a2", "a5", "a7", "a9", 
                         "c3", "c4", "c8", "c9", "e1", "e4", "e6", "e7", "n1", "n2", "n6", 
                         "n8", "i2", "i8", "i9", "i10"), 
                       c("a2", "a5", "a7", "a9", "c3", 
                         "c4", "c8", "c9", "e1", "e4", "e6", "e7", "n1", "n2", "n6", "n8", 
                         "i2", "i8", "i9", "i10")), 
      class = c("lavaan.matrix.symmetric", "matrix")), 
    psi = structure(
      c(1, 0.0307300527642325, 0.397826036072362, 
        -0.190732145703416, 0.416212032411235, 0.0307300527642325, 1, 
        0.0516464797818452, -0.1590078212592, -0.00935598159738081, 0.397826036072362, 
        0.0516464797818452, 1, -0.0409034475100417, 0.672259531068557, 
        -0.190732145703416, -0.1590078212592, -0.0409034475100417, 1, 
        -0.0589307820291875, 0.416212032411235, -0.00935598159738081, 
        0.672259531068557, -0.0589307820291875, 1), 
      .Dim = c(5L, 5L), 
      .Dimnames = list(c("A", "C", "E", "N", "O"), c("A", "C", "E", "N", "O")),
      class = c("lavaan.matrix.symmetric","matrix")), 
    nu = structure(
      c(3.94424798096106, 3.56191664603162, 
        3.99887279416986, 3.61846568375092, 3.28727563899424, 4.26318382086251, 
        4.18115621522395, 3.59932137934746, 2.26973604341351, 2.92379355460568, 
        3.01186745338878, 3.09054924993737, 2.25941455787935, 2.46014063678953, 
        2.32370361993874, 2.27867084045946, 3.97168549581889, 3.59000455755382, 
        3.61505997818372, 4.07151827816703),
      .Dim = c(20L, 1L), 
      .Dimnames = list(
        c("a2", "a5", "a7", "a9", "c3", "c4", "c8", "c9", "e1", "e4", 
          "e6", "e7", "n1", "n2", "n6", "n8", "i2", "i8", "i9", "i10"
        ), "intercept"), class = c("lavaan.matrix", "matrix")), 
    alpha = structure(c(0, 0, 0, 0, 0), 
                      .Dim = c(5L, 1L), 
                      .Dimnames = list(c("A", "C", "E", "N", "O"), "intercept"), 
                      class = c("lavaan.matrix", "matrix"))), 
    `2` = list(
      lambda = structure(
        c(0.321851420828946, 0.65505328221763, 
          0.644928823284185, 0.745498551641103, 0, 0, 0, 0, 0, 0, 0, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.569789973707324, 
          0.341398323887284, 0.397589912762831, 0.745400693889336, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
          0, 0.6572331474069, 0.803018980949713, 0.798604339572203, 
          0.685778873051152, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
          0, 0, 0, 0, 0, 0, 0, 0.50691173408154, 0.824339800976872, 
          0.599603442972015, 0.825307748488813, 0, 0, 0, 0, 0, 0, 0, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.511822774613899, 
          0.425281197599131, 0.484803397473042, 0.569706848613303), 
        .Dim = c(20L, 5L), 
        .Dimnames = list(c("a2", "a5", "a7", "a9", "c3", "c4", 
                           "c8", "c9", "e1", "e4", "e6", "e7", "n1", "n2", "n6", "n8", 
                           "i2", "i8", "i9", "i10"), 
                         c("A", "C", "E", "N", "O")), 
        class = c("lavaan.matrix", "matrix")), 
      theta = structure(
        c(0.543886582321732, 0.0991628559089238, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, -0.115517592631624, 0, 0, 0, 0, 
          0, 0, 0, 0, 0.0991628559089238, 0.536443955046005, 0, 0, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.410358018613791, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
          0, 0.356096075938922, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
          0, 0, -0.00550535625446915, 0, 0, 0, 0, 0, 0.934929805416671, 
          0, 0, 0, 0, 0, -0.0296478578193999, 0, 0, 0, 0, 0, 0, 0, 
          0, 0, 0, 0, 0, 0, 0, 0.409621144057789, 0, 0, 0, 0, 0, 0, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.70483713633214, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
          0, 0.924828156814879, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
          0, 0, 0, 0, 0, 0, 0, 0, 0.78444201074091, 0, 0, 0, 0, 0, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.991446629333851, 
          0, -0.082909427455581, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
          -0.0296478578193999, 0, 0, 0, 0, 0, 0.880871827235738, 0, 
          0, 0, 0, 0, 0, 0, 0, 0, -0.115517592631624, 0, 0, 0, 0, 0, 
          0, 0, 0, -0.082909427455581, 0, 0.611527086912206, 0, -0.146167079574639, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.943687415419278, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.146167079574639, 
          0, 0.958706250415071, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
          0, 0, 0, 0, 0, 0, 0, 0, 0.747797205162342, 0, 0, 0, 0, 0, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.651913135519311, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
          0, 0.73485217216192, 0, 0, 0.275475340095472, 0, 0, 0, 0, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.18247806913975, 
          0.579953153217946, 0, 0, 0, 0, -0.00550535625446915, 0, 0, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.579953153217946, 1.01537514466963, 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.275475340095472, 
          0, 0, 0.785695412105443), .Dim = c(20L, 20L), 
        .Dimnames = list(c("a2", "a5", "a7", "a9", "c3", "c4", "c8", "c9", "e1", 
                           "e4", "e6", "e7", "n1", "n2", "n6", "n8", "i2", "i8", 
                           "i9", "i10"), 
                         c("a2", "a5", "a7", "a9", "c3", "c4", "c8", 
                           "c9", "e1", "e4", "e6", "e7", "n1", "n2", "n6", "n8", 
                           "i2", "i8", "i9", "i10")), 
        class = c("lavaan.matrix.symmetric", "matrix")), 
      psi = structure(
        c(0.561994064185727, -0.0358719479236162, 
          0.135253238440016, -0.159404773244813, 0.293918421161048, 
          -0.0358719479236162, 1.4549742724029, 0.114382670912166, 
          -0.309950522703722, -0.244829380643405, 0.135253238440016, 
          0.114382670912166, 0.979290499947525, -0.175364444285233, 
          0.454638527570875, -0.159404773244813, -0.309950522703722, 
          -0.175364444285233, 1.11370301060926, -0.0548613801146908, 
          0.293918421161048, -0.244829380643405, 0.454638527570875, 
          -0.0548613801146908, 1.85938365019618), .Dim = c(5L, 5L), 
        .Dimnames = list(
            c("A", "C", "E", "N", "O"), c("A", "C", "E", "N", "O"
            )), class = c("lavaan.matrix.symmetric", "matrix")), 
      nu = structure(
        c(4.09824764469264, 3.56191664603162, 
          3.99887279416986, 3.61846568375092, 3.28727563899424, 
          4.26318382086251, 4.18115621522395, 3.59932137934746, 
          2.26973604341351, 2.92379355460568, 3.4278175784955, 
          3.09054924993737, 2.56642940207529, 2.69946144321671, 
          2.32370361993874, 2.27867084045946, 3.97168549581889, 
          3.59000455755382, 3.61505997818372, 4.07151827816703), 
        .Dim = c(20L, 1L), 
        .Dimnames = list(c("a2", "a5", "a7", "a9", "c3", 
                           "c4", "c8", "c9", "e1", "e4", "e6", "e7", "n1", "n2", 
                           "n6", "n8", "i2", "i8", "i9", "i10"), "intercept"), 
        class = c("lavaan.matrix", 
                  "matrix")), 
      alpha = structure(
        c(0.742530416810915, 0.140276996932641, 
          -0.0193402230099476, -0.0491017394271967, -0.458225607619261
        ), .Dim = c(5L, 1L), 
        .Dimnames = list(c("A", "C", "E",  "N", "O"), "intercept"), 
        class = c("lavaan.matrix", "matrix")))), 
  class = c("lavaan.list", "list"))                      
# Create sample output
pimout_eq <- PartInv(
  propsel = .25,
  weights_item = c(0.008125, 0.008125, 0.008125, 0.008125,
                   0.044875, 0.044875, 0.044875, 0.044875,
                   0.117325, 0.117325, 0.117325, 0.117325,
                   -0.048775, -0.048775, -0.048775, -0.048775,
                   0.0309, 0.0309, 0.0309, 0.0309),
  weights_latent = c(0.0325, 0.1795, 0.4693, -0.1951, 0.1236),
  alpha_r = result[[2]]$alpha, 
  alpha_f = result[[1]]$alpha,
  psi_r = result[[2]]$psi, 
  psi_f = result[[1]]$psi,
  lambda_r = (result[[2]]$lambda + result[[1]]$lambda) / 2,
  nu_r = (result[[2]]$nu + result[[1]]$nu) / 2,
  Theta_r = (result[[2]]$theta + result[[1]]$theta) / 2
)

pimout <- PartInv(
  propsel = .25,
  weights_item = c(0.008125, 0.008125, 0.008125, 0.008125,
                   0.044875, 0.044875, 0.044875, 0.044875,
                   0.117325, 0.117325, 0.117325, 0.117325,
                   -0.048775, -0.048775, -0.048775, -0.048775,
                   0.0309, 0.0309, 0.0309, 0.0309),
  # Agreeableness Conscientiousness Extraversion Neuroticism Openness
  weights_latent = c(0.0325, 0.1795, 0.4693, -0.1951, 0.1236),
  alpha_r = result[[2]]$alpha,
  alpha_f = result[[1]]$alpha,
  psi_r = result[[2]]$psi,
  psi_f = result[[1]]$psi,
  lambda_r = result[[2]]$lambda,
  nu_r = result[[2]]$nu,
  nu_f = result[[1]]$nu,
  Theta_r = result[[2]]$theta,
  Theta_f = result[[1]]$theta
)

## test weights_latent is null

test_that("PartInv() runs with no latent weight input", {
  pimout_latwei_null <- PartInv(
    propsel = .25,
    weights_item = c(0.008125, 0.008125, 0.008125, 0.008125,
                     0.044875, 0.044875, 0.044875, 0.044875,
                     0.117325, 0.117325, 0.117325, 0.117325,
                     -0.048775, -0.048775, -0.048775, -0.048775,
                     0.0309, 0.0309, 0.0309, 0.0309),
    alpha_r = result[[2]]$alpha,
    alpha_f = result[[1]]$alpha,
    psi_r = result[[2]]$psi,
    psi_f = result[[1]]$psi,
    lambda_r = result[[2]]$lambda,
    nu_r = result[[2]]$nu,
    nu_f = result[[1]]$nu,
    Theta_r = result[[2]]$theta,
    Theta_f = result[[1]]$theta
  )
  pimout_latwei_ba <- PartInv(
    propsel = .25,
    weights_item = c(0.008125, 0.008125, 0.008125, 0.008125,
                     0.044875, 0.044875, 0.044875, 0.044875,
                     0.117325, 0.117325, 0.117325, 0.117325,
                     -0.048775, -0.048775, -0.048775, -0.048775,
                     0.0309, 0.0309, 0.0309, 0.0309),
    weights_latent = c(rep(1, 5)),
    alpha_r = result[[2]]$alpha,
    alpha_f = result[[1]]$alpha,
    psi_r = result[[2]]$psi,
    psi_f = result[[1]]$psi,
    lambda_r = result[[2]]$lambda,
    nu_r = result[[2]]$nu,
    nu_f = result[[1]]$nu,
    Theta_r = result[[2]]$theta,
    Theta_f = result[[1]]$theta
  )
  
  expect_equal(pimout_latwei_ba[1:4], pimout_latwei_null[1:4])
})

## test weights_item is null

test_that("PartInv() runs with no item weight input", {
  pimout_itwei_null <- PartInv(
    propsel = .25,
    weights_latent = c(0.0325, 0.1795, 
                       0.4693, -0.1951, 0.1236),
    alpha_r = result[[2]]$alpha,
    alpha_f = result[[1]]$alpha,
    psi_r = result[[2]]$psi,
    psi_f = result[[1]]$psi,
    lambda_r = result[[2]]$lambda,
    nu_r = result[[2]]$nu,
    nu_f = result[[1]]$nu,
    Theta_r = result[[2]]$theta,
    Theta_f = result[[1]]$theta
  )
  pimout_itwei_ba <- PartInv(
    propsel = .25,
    weights_item = c(rep(1, 20)),
    weights_latent = c(0.0325, 0.1795, 
                       0.4693, -0.1951, 0.1236),
    alpha_r = result[[2]]$alpha,
    alpha_f = result[[1]]$alpha,
    psi_r = result[[2]]$psi,
    psi_f = result[[1]]$psi,
    lambda_r = result[[2]]$lambda,
    nu_r = result[[2]]$nu,
    nu_f = result[[1]]$nu,
    Theta_r = result[[2]]$theta,
    Theta_f = result[[1]]$theta
  )
  expect_equal(pimout_itwei_ba[1:4], pimout_itwei_null[1:4])
})

## test weights_item is fraction 

test_that("PartInv() runs with no latent weight input", {
  expect_warning(
    pimout_latwei_null <- PartInv(
      cut_z = 80,
      weights_item = c(rep(1/3, 20)),
      alpha_r = result[[2]]$alpha,
      alpha_f = result[[1]]$alpha,
      psi_r = result[[2]]$psi,
      psi_f = result[[1]]$psi,
      lambda_r = result[[2]]$lambda,
      nu_r = result[[2]]$nu,
      nu_f = result[[1]]$nu,
      Theta_r = result[[2]]$theta,
      Theta_f = result[[1]]$theta
    ), 
    "Proportion selected is 1% or less")
})

## test equivalence of two functions

test_that("PartInv() runs with no item weight input", {
  pi_res_eq <- PartInv(
    propsel = .25,
    weights_item = c(0.008125, 0.008125, 0.008125, 0.008125,
                     0.044875, 0.044875, 0.044875, 0.044875,
                     0.117325, 0.117325, 0.117325, 0.117325,
                     -0.048775, -0.048775, -0.048775, -0.048775,
                     0.0309, 0.0309, 0.0309, 0.0309),
    weights_latent = c(0.0325, 0.1795, 
                       0.4693, -0.1951, 0.1236),
    alpha_r = result[[2]]$alpha,
    alpha_f = result[[1]]$alpha,
    psi_r = result[[2]]$psi,
    psi_f = result[[1]]$psi,
    lambda_r = result[[2]]$lambda,
    nu_r = result[[2]]$nu,
    nu_f = result[[1]]$nu,
    Theta_r = result[[2]]$theta,
    Theta_f = result[[1]]$theta
  )
  expect_equal(pi_res_eq[1:4], pimout[1:4])
})

# test labels changed

test_that("PartInvMulti_we with customized label", {
  pimout_label <- PartInv(
    propsel = .25,
    weights_item = c(0.008125, 0.008125, 0.008125, 0.008125,
                     0.044875, 0.044875, 0.044875, 0.044875,
                     0.117325, 0.117325, 0.117325, 0.117325,
                     -0.048775, -0.048775, -0.048775, -0.048775,
                     0.0309, 0.0309, 0.0309, 0.0309),
    weights_latent = c(0.0325, 0.1795, 
                       0.4693, -0.1951, 0.1236),
    alpha_r = result[[2]]$alpha,
    alpha_f = result[[1]]$alpha,
    psi_r = result[[2]]$psi,
    psi_f = result[[1]]$psi,
    lambda_r = result[[2]]$lambda,
    nu_r = result[[2]]$nu,
    nu_f = result[[1]]$nu,
    Theta_r = result[[2]]$theta,
    Theta_f = result[[1]]$theta,
    labels = c("Female", "Male")
  )
  expect_identical(colnames(pimout_label[[4]]), c("Female", "Male", "E_R(Male)"))
})
claycantrell/PartInvShinyUI documentation built on March 29, 2022, 9:49 a.m.