tests/testthat/test-segmentcomparisontable.R

context("SegementComparisonTable")
findInstDirFile <- function(file)
{
    file.path(system.file("testdata", package = "flipAnalysisOfVariance", mustWork = TRUE),
              file)
}
load(findInstDirFile("segmentcomparisontabledata.rda"))

all.var <- c(formVariables[1:2], num.multi.vset, pick.any.vset)
ww <- formVariables[[2]]
ff <- ww < 100

test_that("No missing values",
{
    # Check basic output
    expect_error(segcmp.mean <- SegmentComparisonTable(all.var, formSegmentation,
        font.color.FDRcorrection = TRUE), NA)
    expect_error(segcmp.mean <- SegmentComparisonTable(all.var, formSegmentation,
        cond.shade = "Boxes"), NA)
    expect_error(segcmp.mean <- SegmentComparisonTable(all.var, formSegmentation,
        cond.shade = "Bars"), NA)

    expect_error(segcmp.mean <- SegmentComparisonTable(all.var, formSegmentation), NA)
    expect_equal(attr(segcmp.mean, "ChartData"), structure(c(99, 0.12375, 0.464646464646465, 0.535353535353535,
        1586.29292929293, NaN, -0.111111111111111, NaN, 0.181818181818182,
        0.0606060606060606, 0.0707070707070707, NaN, NaN, NaN, NaN, 1,
        1, 0.939393939393939, 1, 0.797979797979798, 0.95959595959596,
        1, 94, 0.1175, 0.48936170212766, 0.51063829787234, 1561.59574468085,
        NaN, -0.127659574468085, NaN, 0.148936170212766, 0.0957446808510638,
        0.0851063829787234, NaN, NaN, NaN, NaN, 1, 1, 0.968085106382979,
        1, 0.808510638297872, 0.946808510638298, 1, 83, 0.10375, 0.518072289156627,
        0.481927710843373, 1689.77108433735, NaN, 0, NaN, 0.156626506024096,
        -0.0120481927710843, 0.072289156626506, NaN, NaN, NaN, NaN, 1,
        1, 0.91566265060241, 1, 0.710843373493976, 0.879518072289157,
        1, 91, 0.11375, 0.406593406593407, 0.593406593406593, 1543.95604395604,
        NaN, -0.0549450549450549, NaN, 0.32967032967033, -0.131868131868132,
        0.021978021978022, NaN, NaN, NaN, NaN, 1, 1, 0.934065934065934,
        1, 0.868131868131868, 0.846153846153846, 1, 93, 0.11625, 0.548387096774194,
        0.451612903225806, 1638.15053763441, NaN, -0.0967741935483871,
        NaN, 0.247311827956989, 0.010752688172043, -0.010752688172043,
        NaN, NaN, NaN, NaN, 1, 1, 0.935483870967742, 1, 0.838709677419355,
        0.881720430107527, 1, 63, 0.07875, 0.507936507936508, 0.492063492063492,
        1582.12698412698, NaN, -0.0634920634920635, NaN, 0.0634920634920635,
        0.0317460317460317, 0.142857142857143, NaN, NaN, NaN, NaN, 1,
        1, 0.936507936507937, 1, 0.841269841269841, 0.841269841269841,
        1, 95, 0.11875, 0.431578947368421, 0.568421052631579, 1538.23157894737,
        NaN, -0.105263157894737, NaN, 0.189473684210526, 0.147368421052632,
        -0.0421052631578947, NaN, NaN, NaN, NaN, 1, 1, 0.873684210526316,
        1, 0.821052631578947, 0.978947368421053, 1, 126, 0.1575, 0.507936507936508,
        0.492063492063492, 1634.14285714286, NaN, -0.0396825396825397,
        NaN, 0.0634920634920635, 0.0238095238095238, 0.166666666666667,
        NaN, NaN, NaN, NaN, 1, 1, 0.904761904761905, 1, 0.857142857142857,
        0.928571428571429, 1, 56, 0.07, 0.625, 0.375, 1638.57142857143,
        NaN, -0.160714285714286, NaN, 0.232142857142857, -0.0357142857142857,
        0.125, NaN, NaN, NaN, NaN, 1, 1, 0.910714285714286, 1, 0.767857142857143,
        0.928571428571429, 1), .Dim = c(22L, 9L), .Dimnames = list(c("Age: Sample size",
        "Age: Percentage", "Gender: Male", "Gender: Female", "Unique Identifier",
        "Max-Diff Experiment: I: don't like to follow rules", "Max-Diff Experiment: I: try and get a lot done every day",
        "Max-Diff Experiment: I: think a lot about what I eat", "Max-Diff Experiment: I: care a lot about what others think",
        "Max-Diff Experiment: I: like to take chances", "Max-Diff Experiment: I: just want to have fun",
        "Max-Diff Experiment: I: spend as much time with my friends as I can",
        "Max-Diff Experiment: I: like to work on community projects",
        "Max-Diff Experiment: I: work hard to stay fit and healthy", "Max-Diff Experiment: I: SUM",
        "Awareness: Coca-Cola", "Awareness: Diet Coke", "Awareness: Coke Zero",
        "Awareness: Pepsi", "Awareness: Diet Pepsi", "Awareness: Pepsi Max",
        "Awareness: NET"), c("18 to 24", "25 to 29", "30 to 34", "35 to 39",
        "40 to 44", "45 to 49", "50 to 54", "55 to 64", "65 or more"))))
    expect_equal(attr(segcmp.mean, "p-values"), structure(c(NA, NA, 0.536082900256765, 0.536082900256765, 0.894562706990427,
        NA, 0.625536225468137, NA, 0.933372986805091, 0.563976874435404,
        0.974289196207276, NA, NA, NA, NA, NaN, NaN, 0.530957257776271,
        NaN, 0.616059312838578, 0.0825909201012083, NaN, NA, NA, 0.927820642717582,
        0.927820642717582, 0.713373495577983, NA, 0.445522625369339,
        NA, 0.695077387336881, 0.279853117806095, 0.791122730982829,
        NA, NA, NA, NA, NaN, NaN, 0.0846935732764606, NaN, 0.836598420418677,
        0.22423770635468, NaN, NA, NA, 0.639671305972622, 0.639671305972623,
        0.45582746523488, NA, 0.237602547848276, NA, 0.780713036873227,
        0.627678263858643, 0.953964541024679, NA, NA, NA, NA, NaN, NaN,
        0.769333392149678, NaN, 0.00881386837150375, 0.240617581819886,
        NaN, NA, NA, 0.0773167026691276, 0.0773167026691276, 0.597496019449243,
        NA, 0.689989368256211, NA, 0.0256582210917995, 0.0144390273951916,
        0.456689040784298, NA, NA, NA, NA, NaN, NaN, 0.693676299108538,
        NaN, 0.174629273291409, 0.0146914916822772, NaN, NA, NA, 0.262262932466246,
        0.262262932466246, 0.730140819437442, NA, 0.777609603968153,
        NA, 0.319970162888771, 0.819420457299516, 0.26821685683753, NA,
        NA, NA, NA, NaN, NaN, 0.650155944697677, NaN, 0.551900212281413,
        0.241839702120737, NaN, NA, NA, 0.81448023121581, 0.81448023121581,
        0.891428187030904, NA, 0.805713007150233, NA, 0.209126428857822,
        0.937915581632589, 0.34491841368296, NA, NA, NA, NA, NaN, NaN,
        0.69098010857273, NaN, 0.593171752195393, 0.0327571818857946,
        NaN, NA, NA, 0.196664361018653, 0.196664361018653, 0.581518532299045,
        NA, 0.649886387768158, NA, 0.856186567768026, 0.0647618580022847,
        0.079547311625153, NA, NA, NA, NA, NaN, NaN, 0.0501541492076658,
        NaN, 0.897551012844255, 0.0158962970559489, NaN, NA, NA, 0.728585150490917,
        0.728585150490917, 0.71434870248204, NA, 0.445572889406008, NA,
        0.0546730308837368, 0.981994776818039, 0.0602714731330172, NA,
        NA, NA, NA, NaN, NaN, 0.381599649504367, NaN, 0.19660446194558,
        0.518506066801872, NaN, NA, NA, 0.0416384099417915, 0.041638409941791,
        0.797225757807784, NA, 0.306546013678444, NA, 0.531515054259046,
        0.478138539415456, 0.541257326455509, NA, NA, NA, NA, NaN, NaN,
        0.703095660618453, NaN, 0.332228183702232, 0.682037523151487,
        NaN), .Dim = c(22L, 9L), .Dimnames = list(c("Sample size", "Percentage",
        "Male", "Female", "", "don't like to follow rules",
        "try and get a lot done every day", "think a lot about what I eat",
        "care a lot about what others think", "like to take chances",
        "just want to have fun", "spend as much time with my friends as I can",
        "like to work on community projects", "work hard to stay fit and healthy",
        "SUM", "Coca-Cola", "Diet Coke", "Coke Zero", "Pepsi", "Diet Pepsi",
        "Pepsi Max", "NET"), c("18 to 24", "25 to 29", "30 to 34", "35 to 39",
        "40 to 44", "45 to 49", "50 to 54", "55 to 64", "65 or more"))))

    expect_error(segcmp.index <- SegmentComparisonTable(all.var, formSegmentation,
        show.index.values = TRUE), NA)
    expect_equal(attr(segcmp.index, "ChartData"), structure(c(99, 0.12375, 0.941056130929549, 1.05748846489587,
          0.991123354759718, NaN, 1.36752136752137, NaN, 1.03159252095422,
          2.42424242424242, 1.02846648301194, NaN, NaN, NaN, NaN, 1, 1,
          1.01693525238857, 1, 0.977616904110013, 1.05017341679448, 1,
          94, 0.1175, 0.991112308106652, 1.0086682427108, 0.975692436539113,
          NaN, 1.57119476268412, NaN, 0.8450279161008, 3.82978723404255,
          1.23791102514507, NaN, NaN, NaN, NaN, 1, 1, 1.04799470244436,
          1, 0.990518393014239, 1.036178944611, 1, 83, 0.10375, 1.04926033246912,
          0.951955972036293, 1.05577699739916, NaN, 0, NaN, 0.888661027087072,
          -0.481927710843373, 1.05147864184009, NaN, NaN, NaN, NaN, 1,
          1, 0.991245088608833, 1, 0.87086477610288, 0.962536878018229,
          1, 91, 0.11375, 0.823480317151203, 1.17216117216117, 0.964671067763851,
          NaN, 0.676246830092984, NaN, 1.87046995557634, -5.27472527472527,
          0.31968031968032, NaN, NaN, NaN, NaN, 1, 1, 1.01116745230412,
          1, 1.06356124732848, 0.926023361043881, 1, 93, 0.11625, 1.11065741118824,
          0.892074870569494, 1.02352423469816, NaN, 1.19106699751861, NaN,
          1.40318767635171, 0.43010752688172, -0.156402737047898, NaN,
          NaN, NaN, NaN, 1, 1, 1.01270243135885, 1, 1.02751568443413, 0.964947119132724,
          1, 63, 0.07875, 1.02873216797267, 0.971977268273565, 0.988520452437978,
          NaN, 0.781440781440781, NaN, 0.360238658110999, 1.26984126984127,
          2.07792207792208, NaN, NaN, NaN, NaN, 1, 1, 1.01381102734283,
          1, 1.03065217919736, 0.920678348858924, 1, 95, 0.11875, 0.874083944037308,
          1.12280701754386, 0.961094394843717, NaN, 1.29554655870445, NaN,
          1.07502799552072, 5.89473684210526, -0.61244019138756, NaN, NaN,
          NaN, NaN, 1, 1, 0.945801581083969, 1, 1.00588377528814, 1.07135142918857,
          1, 126, 0.1575, 1.02873216797267, 0.971977268273565, 1.02102021689651,
          NaN, 0.488400488400488, NaN, 0.360238658110999, 0.952380952380952,
          2.42424242424242, NaN, NaN, NaN, NaN, 1, 1, 0.97944455183968,
          1, 1.05009844672938, 1.01622044166504, 1, 56, 0.07, 1.26582278481013,
          0.740740740740741, 1.02378720935422, NaN, 1.97802197802198, NaN,
          1.31712259371834, -1.42857142857143, 1.81818181818182, NaN, NaN,
          NaN, NaN, 1, 1, 0.98588826599652, 1, 0.940713191861737, 1.01622044166504,
          1), .Dim = c(22L, 9L), .Dimnames = list(c("Age: Sample size",
            "Age: Percentage", "Gender: Male", "Gender: Female", "Unique Identifier",
            "Max-Diff Experiment: I: don't like to follow rules", "Max-Diff Experiment: I: try and get a lot done every day",
            "Max-Diff Experiment: I: think a lot about what I eat", "Max-Diff Experiment: I: care a lot about what others think",
            "Max-Diff Experiment: I: like to take chances", "Max-Diff Experiment: I: just want to have fun",
            "Max-Diff Experiment: I: spend as much time with my friends as I can",
            "Max-Diff Experiment: I: like to work on community projects",
            "Max-Diff Experiment: I: work hard to stay fit and healthy",
            "Max-Diff Experiment: I: SUM", "Awareness: Coca-Cola", "Awareness: Diet Coke",
            "Awareness: Coke Zero", "Awareness: Pepsi", "Awareness: Diet Pepsi",
            "Awareness: Pepsi Max", "Awareness: NET"), c("18 to 24", "25 to 29",
            "30 to 34", "35 to 39", "40 to 44", "45 to 49", "50 to 54", "55 to 64",
            "65 or more"))))
    expect_equal(attr(segcmp.index, "p-values"), structure(c(NA, NA, 0.536082900256765, 0.536082900256765, 0.894562706990427,
            NA, 0.625536225468137, NA, 0.933372986805091, 0.563976874435404,
            0.974289196207276, NA, NA, NA, NA, NaN, NaN, 0.530957257776271,
            NaN, 0.616059312838578, 0.0825909201012083, NaN, NA, NA, 0.927820642717582,
            0.927820642717582, 0.713373495577983, NA, 0.445522625369339,
            NA, 0.695077387336881, 0.279853117806095, 0.791122730982829,
            NA, NA, NA, NA, NaN, NaN, 0.0846935732764606, NaN, 0.836598420418677,
            0.22423770635468, NaN, NA, NA, 0.639671305972622, 0.639671305972623,
            0.45582746523488, NA, 0.237602547848276, NA, 0.780713036873227,
            0.627678263858643, 0.953964541024679, NA, NA, NA, NA, NaN, NaN,
            0.769333392149678, NaN, 0.00881386837150375, 0.240617581819886,
            NaN, NA, NA, 0.0773167026691276, 0.0773167026691276, 0.597496019449243,
            NA, 0.689989368256211, NA, 0.0256582210917995, 0.0144390273951916,
            0.456689040784298, NA, NA, NA, NA, NaN, NaN, 0.693676299108538,
            NaN, 0.174629273291409, 0.0146914916822772, NaN, NA, NA, 0.262262932466246,
            0.262262932466246, 0.730140819437442, NA, 0.777609603968153,
            NA, 0.319970162888771, 0.819420457299516, 0.26821685683753, NA,
            NA, NA, NA, NaN, NaN, 0.650155944697677, NaN, 0.551900212281413,
            0.241839702120737, NaN, NA, NA, 0.81448023121581, 0.81448023121581,
            0.891428187030904, NA, 0.805713007150233, NA, 0.209126428857822,
            0.937915581632589, 0.34491841368296, NA, NA, NA, NA, NaN, NaN,
            0.69098010857273, NaN, 0.593171752195393, 0.0327571818857946,
            NaN, NA, NA, 0.196664361018653, 0.196664361018653, 0.581518532299045,
            NA, 0.649886387768158, NA, 0.856186567768026, 0.0647618580022847,
            0.079547311625153, NA, NA, NA, NA, NaN, NaN, 0.0501541492076658,
            NaN, 0.897551012844255, 0.0158962970559489, NaN, NA, NA, 0.728585150490917,
            0.728585150490917, 0.71434870248204, NA, 0.445572889406008, NA,
            0.0546730308837368, 0.981994776818039, 0.0602714731330172, NA,
            NA, NA, NA, NaN, NaN, 0.381599649504367, NaN, 0.19660446194558,
            0.518506066801872, NaN, NA, NA, 0.0416384099417915, 0.041638409941791,
            0.797225757807784, NA, 0.306546013678444, NA, 0.531515054259046,
            0.478138539415456, 0.541257326455509, NA, NA, NA, NA, NaN, NaN,
            0.703095660618453, NaN, 0.332228183702232, 0.682037523151487,
            NaN), .Dim = c(22L, 9L), .Dimnames = list(c("Sample size", "Percentage",
            "Male", "Female", "", "don't like to follow rules",
            "try and get a lot done every day", "think a lot about what I eat",
            "care a lot about what others think", "like to take chances",
            "just want to have fun", "spend as much time with my friends as I can",
            "like to work on community projects", "work hard to stay fit and healthy",
            "SUM", "Coca-Cola", "Diet Coke", "Coke Zero", "Pepsi", "Diet Pepsi",
            "Pepsi Max", "NET"), c("18 to 24", "25 to 29", "30 to 34", "35 to 39",
            "40 to 44", "45 to 49", "50 to 54", "55 to 64", "65 or more"))))

    expect_error(segcmp.date <- SegmentComparisonTable(list(date), formSegmentation), NA)
    expect_equal(attr(segcmp.date, "ChartData"), structure(c(99, 0.12375, 0, 0.0303030303030303, 0.0202020202020202,
            0.0303030303030303, 0.0303030303030303, 0.0707070707070707, 0.0404040404040404,
            0.0505050505050505, 0.0505050505050505, 0.0404040404040404, 0.0202020202020202,
            0.0202020202020202, 0.0404040404040404, 0.0606060606060606, 0.0101010101010101,
            0.0505050505050505, 0.0606060606060606, 0.0404040404040404, 0.0404040404040404,
            0.0202020202020202, 0.0404040404040404, 0.0303030303030303, 0.0606060606060606,
            0.0303030303030303, 0.0303030303030303, 0.0404040404040404, 0.0404040404040404,
            94, 0.1175, 0, 0.0319148936170213, 0.0851063829787234, 0.0106382978723404,
            0.0106382978723404, 0.0531914893617021, 0.0531914893617021, 0.0531914893617021,
            0.0638297872340425, 0.0319148936170213, 0.0106382978723404, 0.0319148936170213,
            0.0319148936170213, 0.0425531914893617, 0.0425531914893617, 0.0638297872340425,
            0.0531914893617021, 0.0425531914893617, 0.0106382978723404, 0.0319148936170213,
            0.0531914893617021, 0.0106382978723404, 0.0425531914893617, 0.0319148936170213,
            0.0319148936170213, 0.0425531914893617, 0.0319148936170213, 83,
            0.10375, 0, 0.0240963855421687, 0.0120481927710843, 0.0602409638554217,
            0.0481927710843374, 0.0602409638554217, 0.0240963855421687, 0.0481927710843374,
            0.036144578313253, 0.036144578313253, 0.0240963855421687, 0.0120481927710843,
            0.036144578313253, 0.0481927710843374, 0.036144578313253, 0.036144578313253,
            0.0120481927710843, 0.0240963855421687, 0.0481927710843374, 0.072289156626506,
            0.0120481927710843, 0.0481927710843374, 0.0481927710843374, 0.0481927710843374,
            0.0481927710843374, 0.0843373493975904, 0.0120481927710843, 91,
            0.11375, 0.010989010989011, 0.032967032967033, 0.010989010989011,
            0.010989010989011, 0.043956043956044, 0.043956043956044, 0.0659340659340659,
            0.0549450549450549, 0.043956043956044, 0.032967032967033, 0.0659340659340659,
            0.010989010989011, 0.021978021978022, 0.0549450549450549, 0.010989010989011,
            0.043956043956044, 0.021978021978022, 0.0879120879120879, 0.043956043956044,
            0.043956043956044, 0.0659340659340659, 0.021978021978022, 0.021978021978022,
            0.0549450549450549, 0.043956043956044, 0.010989010989011, 0.021978021978022,
            93, 0.11625, 0, 0.0752688172043011, 0.021505376344086, 0.043010752688172,
            0.021505376344086, 0.032258064516129, 0.021505376344086, 0.032258064516129,
            0.032258064516129, 0.032258064516129, 0.0537634408602151, 0.0537634408602151,
            0.043010752688172, 0.043010752688172, 0.010752688172043, 0.043010752688172,
            0.021505376344086, 0.032258064516129, 0.0537634408602151, 0.0537634408602151,
            0.0537634408602151, 0.010752688172043, 0.0537634408602151, 0.0537634408602151,
            0.021505376344086, 0.021505376344086, 0.0645161290322581, 63,
            0.07875, 0, 0.0158730158730159, 0.0476190476190476, 0, 0.0634920634920635,
            0.0634920634920635, 0.0158730158730159, 0.0476190476190476, 0.0634920634920635,
            0.0793650793650794, 0.0634920634920635, 0.0158730158730159, 0.0317460317460317,
            0, 0.0476190476190476, 0.0793650793650794, 0.0158730158730159,
            0.0317460317460317, 0.0476190476190476, 0.0317460317460317, 0.0158730158730159,
            0.126984126984127, 0.0317460317460317, 0.0158730158730159, 0.0158730158730159,
            0.0158730158730159, 0.0158730158730159, 95, 0.11875, 0.0105263157894737,
            0.0631578947368421, 0.0315789473684211, 0.0210526315789474, 0.0842105263157895,
            0.0631578947368421, 0.0105263157894737, 0.0526315789473684, 0.0315789473684211,
            0, 0.0315789473684211, 0.0526315789473684, 0.0421052631578947,
            0.0526315789473684, 0.0631578947368421, 0.0421052631578947, 0.0210526315789474,
            0, 0.0105263157894737, 0.0315789473684211, 0.0421052631578947,
            0.0210526315789474, 0.0526315789473684, 0.0315789473684211, 0.0315789473684211,
            0.0105263157894737, 0.0947368421052632, 126, 0.1575, 0, 0.0317460317460317,
            0.0238095238095238, 0.0238095238095238, 0.0555555555555556, 0.0158730158730159,
            0.0634920634920635, 0.0476190476190476, 0.0238095238095238, 0.0634920634920635,
            0.0396825396825397, 0.0317460317460317, 0.0476190476190476, 0.00793650793650794,
            0.0714285714285714, 0.0555555555555556, 0.0238095238095238, 0.0317460317460317,
            0.0238095238095238, 0.0634920634920635, 0.00793650793650794,
            0.0317460317460317, 0.0476190476190476, 0.0793650793650794, 0.0396825396825397,
            0.0158730158730159, 0.0317460317460317, 56, 0.07, 0.0178571428571429,
            0.0178571428571429, 0.0357142857142857, 0.0178571428571429, 0.0714285714285714,
            0.0535714285714286, 0.0357142857142857, 0.0357142857142857, 0.0357142857142857,
            0.0357142857142857, 0.0357142857142857, 0, 0.0357142857142857,
            0.0714285714285714, 0.0535714285714286, 0.0714285714285714, 0,
            0, 0.0178571428571429, 0.0714285714285714, 0.0357142857142857,
            0, 0.0714285714285714, 0.0357142857142857, 0.0178571428571429,
            0.0714285714285714, 0.0535714285714286), .Dim = c(29L, 9L), .Dimnames = list(
                c("Age: Sample size", "Age: Percentage", "Interview Date: 19/12/2011-1/01/2012",
                "Interview Date: 2/01/2012-15/01/2012", "Interview Date: 16/01/2012-29/01/2012",
                "Interview Date: 30/01/2012-12/02/2012", "Interview Date: 13/02/2012-26/02/2012",
                "Interview Date: 27/02/2012-11/03/2012", "Interview Date: 12/03/2012-25/03/2012",
                "Interview Date: 26/03/2012-8/04/2012", "Interview Date: 9/04/2012-22/04/2012",
                "Interview Date: 23/04/2012-6/05/2012", "Interview Date: 7/05/2012-20/05/2012",
                "Interview Date: 21/05/2012-3/06/2012", "Interview Date: 4/06/2012-17/06/2012",
                "Interview Date: 18/06/2012-1/07/2012", "Interview Date: 2/07/2012-15/07/2012",
                "Interview Date: 16/07/2012-29/07/2012", "Interview Date: 30/07/2012-12/08/2012",
                "Interview Date: 13/08/2012-26/08/2012", "Interview Date: 27/08/2012-9/09/2012",
                "Interview Date: 10/09/2012-23/09/2012", "Interview Date: 24/09/2012-7/10/2012",
                "Interview Date: 8/10/2012-21/10/2012", "Interview Date: 22/10/2012-4/11/2012",
                "Interview Date: 5/11/2012-18/11/2012", "Interview Date: 19/11/2012-2/12/2012",
                "Interview Date: 3/12/2012-16/12/2012", "Interview Date: 17/12/2012-30/12/2012"
                ), c("18 to 24", "25 to 29", "30 to 34", "35 to 39", "40 to 44",
                "45 to 49", "50 to 54", "55 to 64", "65 or more"))))
})

test_that("Missing values",
{
    expect_error(segcmp.missing <- SegmentComparisonTable(all.var, income), NA)
    expect_equal(attr(segcmp.missing, "p-values"), structure(c(NA, NA, 0.442913092343005, 0.442913092343004, 0.90471315420544,
        NA, 0.359934751904966, NA, 0.632568139146777, 0.480112262935356,
        0.399935099557095, NA, NA, NA, NA, NaN, NaN, 0.532191306572819,
        NaN, 0.222513289389306, 0.990627416462493, NaN, NA, NA, 2.28306627828792e-05,
        2.2830662782879e-05, 0.668841107876397, NA, 0.979492418243493,
        NA, 0.15890242217714, 0.147644613948328, 0.329677394445749, NA,
        NA, NA, NA, NaN, NaN, 0.648976756582583, NaN, 0.107929097193098,
        0.255689622233643, NaN, NA, NA, 0.0351514682974753, 0.0351514682974756,
        0.967404844044838, NA, 0.789615557014612, NA, 0.592478436679724,
        0.209384725779942, 0.533752641707727, NA, NA, NA, NA, NaN, NaN,
        0.125402478279284, NaN, 0.853918232074452, 0.757459141889878,
        NaN, NA, NA, 0.881274082835059, 0.881274082835059, 0.88482768927819,
        NA, 0.109209210897554, NA, 0.337766099067151, 0.539530589706417,
        0.253635896483679, NA, NA, NA, NA, NaN, NaN, 0.989743152505496,
        NaN, 0.28198487325302, 0.179562599368097, NaN, NA, NA, 0.10884350939606,
        0.108843509396062, 0.428603032484554, NA, 0.234236237202806,
        NA, 0.60222590006482, 0.682973119619939, 0.401509950968774, NA,
        NA, NA, NA, NaN, NaN, 0.074501980467638, NaN, 0.158387683902162,
        0.599700558245926, NaN, NA, NA, 0.275202305069102, 0.275202305069103,
        0.520702151419303, NA, 0.610855703687319, NA, 0.665093412416572,
        0.750805101335221, 0.795786052458179, NA, NA, NA, NA, NaN, NaN,
        0.694366026214812, NaN, 0.399616254290769, 0.273052380283977,
        NaN, NA, NA, 0.0240561692086176, 0.0240561692086175, 0.952109067301335,
        NA, 0.455144078089062, NA, 0.10383460966435, 0.00285320153232304,
        0.214367328810334, NA, NA, NA, NA, NaN, NaN, 0.474018639470364,
        NaN, 0.07055588751867, 0.355068877040397, NaN, NA, NA, 0.0210216801333983,
        0.0210216801333983, 0.612022685899217, NA, 0.644506574597027,
        NA, 0.149386132840012, 0.249398658263419, 0.841940431639367,
        NA, NA, NA, NA, NaN, NaN, 0.805832337475599, NaN, 0.97098040987573,
        0.221769689699331, NaN, NA, NA, 0.326003170694519, 0.326003170694519,
        0.405093925744972, NA, 0.197813308827291, NA, 0.441272557817656,
        0.924757011862815, 0.428465230257554, NA, NA, NA, NA, NaN, NaN,
        0.909346875020716, NaN, 0.848109732011707, 0.378259491599583,
        NaN), .Dim = c(22L, 9L), .Dimnames = list(c("Sample size", "Percentage",
        "Male", "Female", "", "don't like to follow rules",
        "try and get a lot done every day", "think a lot about what I eat",
        "care a lot about what others think", "like to take chances",
        "just want to have fun", "spend as much time with my friends as I can",
        "like to work on community projects", "work hard to stay fit and healthy",
        "SUM", "Coca-Cola", "Diet Coke", "Coke Zero", "Pepsi", "Diet Pepsi",
        "Pepsi Max", "NET"), c("Less than $15,000", "$15,001 to $30,000",
        "$30,001 to $45,000", "$45,001 to $60,000", "$60,001 to $90,000",
        "$90,001 to $120,000", "$120,001 to $150,000", "$150,001 to $200,000",
        "$200,001 or more"))))
})

test_that("Weights and filters",
{
    expect_error(segcmp.wf.mean <- SegmentComparisonTable(all.var, income, weights = ww, subset = ff), NA)
    expect_equal(attr(segcmp.wf.mean, "ChartData"), structure(c(4, 0.0569757365684575, 0.665399239543726, 0.334600760456274,
        73.0456273764259, NaN, -0.665399239543726, NaN, 0.163498098859316,
        0.836501901140684, -0.163498098859316, NaN, NaN, NaN, NaN, 1,
        1, 1, 1, 1, 1, 1, 15, 0.17396013864818, 0.313823163138232, 0.686176836861768,
        71.6899128268991, NaN, 0.205479452054795, NaN, -0.0174346201743462,
        -0.285180572851806, 0.161892901618929, NaN, NaN, NaN, NaN, 1,
        1, 0.995018679950187, 1, 0.887920298879203, 0.935242839352428,
        1, 9, 0.117201039861352, 0.865064695009242, 0.134935304990758,
        73.9796672828096, NaN, -0.284658040665434, NaN, 0.229205175600739,
        0.033271719038817, 0.393715341959335, NaN, NaN, NaN, NaN, 1,
        1, 1, 1, 0.519408502772643, 0.881700554528651, 1, 17, 0.16421143847487,
        0.617414248021108, 0.382585751978892, 58.8601583113456, NaN,
        -0.0079155672823219, NaN, 0.0356200527704486, -0.0949868073878628,
        0.134564643799472, NaN, NaN, NaN, NaN, 1, 1, 1, 1, 0.932717678100264,
        0.779683377308707, 1, 16, 0.171360485268631, 0.604298356510746,
        0.395701643489254, 67.1011378002528, NaN, 0.175726927939317,
        NaN, -0.419721871049305, 0.151706700379267, 0.307206068268015,
        NaN, NaN, NaN, NaN, 1, 1, 1, 1, 0.853350189633375, 1, 1, 11,
        0.128682842287695, 0.569023569023569, 0.430976430976431, 69.7037037037037,
        NaN, -0.095959595959596, NaN, 0.222222222222222, -0.069023569023569,
        -0.0572390572390572, NaN, NaN, NaN, NaN, 1, 1, 0.861952861952862,
        1, 0.673400673400673, 1, 1, 9, 0.067157712305026, 0.558064516129032,
        0.441935483870968, 59.9677419354839, NaN, -0.1, NaN, 0.0032258064516129,
        -0.203225806451613, 0.3, NaN, NaN, NaN, NaN, 1, 1, 1, 1, 0.72258064516129,
        0.97741935483871, 1, 5, 0.0686741767764298, 0.725552050473186,
        0.274447949526814, 72.8044164037855, NaN, 0.126182965299685,
        NaN, -0.274447949526814, -0.0694006309148265, 0.277602523659306,
        NaN, NaN, NaN, NaN, 1, 1, 1, 1, 0.725552050473186, 1, 1, 4, 0.0517764298093588,
        0.121338912133891, 0.878661087866109, 66.1380753138075, NaN,
        0.548117154811716, NaN, 0.330543933054393, -0.188284518828452,
        -0.690376569037657, NaN, NaN, NaN, NaN, 1, 1, 1, 1, 0.690376569037657,
        1, 1), .Dim = c(22L, 9L), .Dimnames = list(c("Income: Sample size (unweighted)",
        "Income: Percentage (weighted)", "Gender: Male", "Gender: Female",
        "Unique Identifier", "Max-Diff Experiment: I: don't like to follow rules",
        "Max-Diff Experiment: I: try and get a lot done every day", "Max-Diff Experiment: I: think a lot about what I eat",
        "Max-Diff Experiment: I: care a lot about what others think",
        "Max-Diff Experiment: I: like to take chances", "Max-Diff Experiment: I: just want to have fun",
        "Max-Diff Experiment: I: spend as much time with my friends as I can",
        "Max-Diff Experiment: I: like to work on community projects",
        "Max-Diff Experiment: I: work hard to stay fit and healthy",
        "Max-Diff Experiment: I: SUM", "Awareness: Coca-Cola", "Awareness: Diet Coke",
        "Awareness: Coke Zero", "Awareness: Pepsi", "Awareness: Diet Pepsi",
        "Awareness: Pepsi Max", "Awareness: NET"), c("Less than $15,000",
        "$15,001 to $30,000", "$30,001 to $45,000", "$45,001 to $60,000",
        "$60,001 to $90,000", "$90,001 to $120,000", "$120,001 to $150,000",
        "$150,001 to $200,000", "$200,001 or more"))))

    expect_equal(attr(segcmp.wf.mean, "p-values"), structure(c(NA, NA, 0.664765063069914, 0.664765063069915, 0.669613344147336,
        NA, 0.0618435495638518, NA, 0.424720325580127, 0.00915221418692521,
        0.185582061271539, NA, NA, NA, NA, NA, NA, 0.758275993140942,
        NA, 0.308600567331334, 0.601859790807698, NA, NA, NA, 0.0597930603683412,
        0.0597930603683412, 0.493895624624889, NA, 0.356108916434427,
        NA, 0.948115917939329, 0.173115742853716, 0.87482381448374, NA,
        NA, NA, NA, NA, NA, 0.22619154619377, NA, 0.433220854349966,
        0.973785657133997, NA, NA, NA, 0.0848050936443713, 0.0848050936443712,
        0.348986523211993, NA, 0.244656671908053, NA, 0.400483097756892,
        0.529241280103158, 0.220480178255696, NA, NA, NA, NA, NA, NA,
        0.640699436906283, NA, 0.0569373961493344, 0.488887113897719,
        NA, NA, NA, 0.675757144526789, 0.675757144526789, 0.139646320281706,
        NA, 0.923291509208079, NA, 0.824483040619502, 0.693931503296975,
        0.99419922910054, NA, NA, NA, NA, NA, NA, 0.56385576382888, NA,
        0.158347420138383, 0.0158373898447118, NA, NA, NA, 0.774926393607771,
        0.774926393607771, 0.885377448540318, NA, 0.386106788310728,
        NA, 0.0186427888285372, 0.274190046851244, 0.316609570745341,
        NA, NA, NA, NA, NA, NA, 0.555370009060264, NA, 0.561730107599281,
        0.321521430657317, NA, NA, NA, 0.983065245406176, 0.983065245406176,
        0.824220958181082, NA, 0.619191605125967, NA, 0.407641748685655,
        0.86668485524957, 0.316385626682278, NA, NA, NA, NA, NA, NA,
        9.14823772291129e-14, NA, 0.371932919721531, 0.404696229272393,
        NA, NA, NA, 0.971766516301732, 0.971766516301732, 0.377305103626668,
        NA, 0.440454788547912, NA, 0.98355571428827, 0.52483204011919,
        0.589447508200489, NA, NA, NA, NA, NA, NA, 0.729565936593491,
        NA, 0.712476588357537, 0.33741757632375, NA, NA, NA, 0.512015936609234,
        0.512015936609234, 0.500071641526465, NA, 0.737459426471875,
        NA, 0.331607885086099, 0.894903754682261, 0.760205979930143,
        NA, NA, NA, NA, NA, NA, 0.731947261599598, NA, 0.737133452907676,
        0.56259602781099, NA, NA, NA, 0.0187226741816935, 0.0187226741816935,
        0.839455660095585, NA, 0.155804602219702, NA, 0.326909828416473,
        0.657879241948716, 0.0525794696245795, NA, NA, NA, NA, NA, NA,
        0.767437389488944, NA, 0.637489113859305, 0.611913037571106,
        NA), .Dim = c(22L, 9L), .Dimnames = list(c("Sample size (unweighted)", "Percentage (weighted)",
        "Male", "Female", "", "don't like to follow rules",
        "try and get a lot done every day", "think a lot about what I eat",
        "care a lot about what others think", "like to take chances",
        "just want to have fun", "spend as much time with my friends as I can",
        "like to work on community projects", "work hard to stay fit and healthy",
        "SUM", "Coca-Cola", "Diet Coke", "Coke Zero", "Pepsi", "Diet Pepsi",
        "Pepsi Max", "NET"), c("Less than $15,000", "$15,001 to $30,000",
        "$30,001 to $45,000", "$45,001 to $60,000", "$60,001 to $90,000",
        "$90,001 to $120,000", "$120,001 to $150,000", "$150,001 to $200,000",
        "$200,001 or more"))))


    expect_error(segcmp.wf.index <- SegmentComparisonTable(all.var, income, weights = ww, subset = ff,
        show.index.values = TRUE), NA)
    expect_equal(attr(segcmp.wf.index, "p-values"), structure(c(NA, NA, 0.664765063069914, 0.664765063069915, 0.669613344147336,
        NA, 0.0618435495638518, NA, 0.424720325580127, 0.00915221418692521,
        0.185582061271539, NA, NA, NA, NA, NA, NA, 0.758275993140942,
        NA, 0.308600567331334, 0.601859790807698, NA, NA, NA, 0.0597930603683412,
        0.0597930603683412, 0.493895624624889, NA, 0.356108916434427,
        NA, 0.948115917939329, 0.173115742853716, 0.87482381448374, NA,
        NA, NA, NA, NA, NA, 0.22619154619377, NA, 0.433220854349966,
        0.973785657133997, NA, NA, NA, 0.0848050936443713, 0.0848050936443712,
        0.348986523211993, NA, 0.244656671908053, NA, 0.400483097756892,
        0.529241280103158, 0.220480178255696, NA, NA, NA, NA, NA, NA,
        0.640699436906283, NA, 0.0569373961493344, 0.488887113897719,
        NA, NA, NA, 0.675757144526789, 0.675757144526789, 0.139646320281706,
        NA, 0.923291509208079, NA, 0.824483040619502, 0.693931503296975,
        0.99419922910054, NA, NA, NA, NA, NA, NA, 0.56385576382888, NA,
        0.158347420138383, 0.0158373898447118, NA, NA, NA, 0.774926393607771,
        0.774926393607771, 0.885377448540318, NA, 0.386106788310728,
        NA, 0.0186427888285372, 0.274190046851244, 0.316609570745341,
        NA, NA, NA, NA, NA, NA, 0.555370009060264, NA, 0.561730107599281,
        0.321521430657317, NA, NA, NA, 0.983065245406176, 0.983065245406176,
        0.824220958181082, NA, 0.619191605125967, NA, 0.407641748685655,
        0.86668485524957, 0.316385626682278, NA, NA, NA, NA, NA, NA,
        9.14823772291129e-14, NA, 0.371932919721531, 0.404696229272393,
        NA, NA, NA, 0.971766516301732, 0.971766516301732, 0.377305103626668,
        NA, 0.440454788547912, NA, 0.98355571428827, 0.52483204011919,
        0.589447508200489, NA, NA, NA, NA, NA, NA, 0.729565936593491,
        NA, 0.712476588357537, 0.33741757632375, NA, NA, NA, 0.512015936609234,
        0.512015936609234, 0.500071641526465, NA, 0.737459426471875,
        NA, 0.331607885086099, 0.894903754682261, 0.760205979930143,
        NA, NA, NA, NA, NA, NA, 0.731947261599598, NA, 0.737133452907676,
        0.56259602781099, NA, NA, NA, 0.0187226741816935, 0.0187226741816935,
        0.839455660095585, NA, 0.155804602219702, NA, 0.326909828416473,
        0.657879241948716, 0.0525794696245795, NA, NA, NA, NA, NA, NA,
        0.767437389488944, NA, 0.637489113859305, 0.611913037571106,
        NA), .Dim = c(22L, 9L), .Dimnames = list(c("Sample size (unweighted)", "Percentage (weighted)",
        "Male", "Female", "", "don't like to follow rules",
        "try and get a lot done every day", "think a lot about what I eat",
        "care a lot about what others think", "like to take chances",
        "just want to have fun", "spend as much time with my friends as I can",
        "like to work on community projects", "work hard to stay fit and healthy",
        "SUM", "Coca-Cola", "Diet Coke", "Coke Zero", "Pepsi", "Diet Pepsi",
        "Pepsi Max", "NET"), c("Less than $15,000", "$15,001 to $30,000",
        "$30,001 to $45,000", "$45,001 to $60,000", "$60,001 to $90,000",
        "$90,001 to $120,000", "$120,001 to $150,000", "$150,001 to $200,000",
        "$200,001 or more"))))
})

test_that("Removing rows",
{
    res <- SegmentComparisonTable(pick.any.vset, income, format.conditional.fill = TRUE,
                show.index.values = TRUE, row.names.to.remove = "NET, SUM")
    expect_equal(attr(res, "ChartData"), structure(c(23, 0.0325318246110325, 1, 1, 1.03720992264604, 1,
        1.11875518046869, 0.999259658096648, 77, 0.108910891089109, 1,
        1, 0.985778025655326, 1, 1.08208602489365, 1.03757388122713,
        73, 0.103253182461103, 1, 1, 0.950668123371712, 1, 0.990313620284419,
        0.98948216633445, 129, 0.182461103253182, 1, 1, 1.00029723688591,
        1, 0.959345989010251, 0.967168262653899, 158, 0.223479490806223,
        1, 1, 1.03631474722373, 1, 1.04693635785271, 1.01130618803151,
        103, 0.145685997171146, 1, 1, 0.989606289832629, 1, 0.963588026450842,
        1.03067420120833, 59, 0.0834512022630834, 1, 1, 0.9740823541645,
        1, 0.893017654143289, 0.964579944377394, 44, 0.0622347949080622,
        1, 1, 1.01042247629671, 1, 1.00252087600441, 0.945187165775401,
        41, 0.057991513437058, 1, 1, 1.00501271883884, 1, 0.986219723549055,
        1.04104054972438), .Dim = 8:9, .Dimnames = list(c("Income: Sample size",
        "Income: Percentage", "Awareness: Coca-Cola", "Awareness: Diet Coke",
        "Awareness: Coke Zero", "Awareness: Pepsi", "Awareness: Diet Pepsi",
        "Awareness: Pepsi Max"), c("Less than $15,000", "$15,001 to $30,000",
        "$30,001 to $45,000", "$45,001 to $60,000", "$60,001 to $90,000",
        "$90,001 to $120,000", "$120,001 to $150,000", "$150,001 to $200,000",
        "$200,001 or more"))))

    res <- SegmentComparisonTable(list(income), formSegmentation,
            row.names.to.remove = "\"Less than $15,000\"")
    expect_equal(nrow(attr(res, "ChartData")), 10)

    res <- SegmentComparisonTable(q2, income, row.names.to.remove = "SUM, NET")
    expect_equal(dim(attr(res, "ChartData")), c(14, 9))

    res <- SegmentComparisonTable(list(date), income,
        row.names.to.remove = "19/12/2011-1/01/2012, 2/01/2012-15/01/2012")
    expect_equal(dim(attr(res, "ChartData")), c(27, 9))
})

test_that("PickOneMulti",
{
    expect_error(res <- SegmentComparisonTable(brand.attitute, income,
        show.index.values = TRUE), NA)
    expect_equal(dim(attr(res, "ChartData")), c(32, 9))
})

test_that("NumberMulti",
{
    expect_error(res <- SegmentComparisonTable(q2, income), NA)
    expect_equal(dim(attr(res, "ChartData")), c(23, 9))
})

test_that("Non-factor groups",
{
    expect_error(res <- SegmentComparisonTable(all.var, q.in.pick.any), NA)  # all 1s and no 0s
    expect_equal(attr(res, "ChartData"),
        structure(c(800, 1, 0.49375, 0.50625, 1600.5, NaN, -0.08125,
NaN, 0.17625, 0.025, 0.06875, NaN, NaN, NaN, NaN, 1, 1, 0.92375,
1, 0.81625, 0.91375, 1), .Dim = c(22L, 1L), .Dimnames = list(
    c("Coca-Cola: Sample size", "Coca-Cola: Percentage", "Gender: Male",
    "Gender: Female", "Unique Identifier", "Max-Diff Experiment: I: don't like to follow rules",
    "Max-Diff Experiment: I: try and get a lot done every day",
    "Max-Diff Experiment: I: think a lot about what I eat", "Max-Diff Experiment: I: care a lot about what others think",
    "Max-Diff Experiment: I: like to take chances", "Max-Diff Experiment: I: just want to have fun",
    "Max-Diff Experiment: I: spend as much time with my friends as I can",
    "Max-Diff Experiment: I: like to work on community projects",
    "Max-Diff Experiment: I: work hard to stay fit and healthy",
    "Max-Diff Experiment: I: SUM", "Awareness: Coca-Cola", "Awareness: Diet Coke",
    "Awareness: Coke Zero", "Awareness: Pepsi", "Awareness: Diet Pepsi",
    "Awareness: Pepsi Max", "Awareness: NET"), "Coca-Cola")))

    expect_error(res <- SegmentComparisonTable(all.var, q2.in.pick.any), NA)  # 1s and 0s
    expect_equal(attr(res, "ChartData"),
        structure(c(739, 0.92375, 0.495263870094723, 0.504736129905277,
            1599.73748308525, NaN, -0.0866035182679296, NaN, 0.179972936400541,
            0.0189445196211096, 0.074424898511502, NaN, NaN, NaN, NaN, 1,
            1, 1, 1, 0.821380243572395, 0.909336941813261, 1), .Dim = c(22L,
            1L), .Dimnames = list(c("Coke Zero: Sample size", "Coke Zero: Percentage",
            "Gender: Male", "Gender: Female", "Unique Identifier", "Max-Diff Experiment: I: don't like to follow rules",
            "Max-Diff Experiment: I: try and get a lot done every day", "Max-Diff Experiment: I: think a lot about what I eat",
            "Max-Diff Experiment: I: care a lot about what others think",
            "Max-Diff Experiment: I: like to take chances", "Max-Diff Experiment: I: just want to have fun",
            "Max-Diff Experiment: I: spend as much time with my friends as I can",
            "Max-Diff Experiment: I: like to work on community projects",
            "Max-Diff Experiment: I: work hard to stay fit and healthy",
            "Max-Diff Experiment: I: SUM", "Awareness: Coca-Cola", "Awareness: Diet Coke",
            "Awareness: Coke Zero", "Awareness: Pepsi", "Awareness: Diet Pepsi",
            "Awareness: Pepsi Max", "Awareness: NET"), "Coke Zero")))
})

test_that("Missing values in groups",
{
    expect_error(res <- SegmentComparisonTable(all.var, liv.missing), NA)
    expect_equal(dimnames(attr(res, "ChartData")),
        list(c("Living arrangement with level entirely missing: Sample size",
            "Living arrangement with level entirely missing: Percentage",
            "Gender: Male", "Gender: Female", "Unique Identifier", "Max-Diff Experiment: I: don't like to follow rules",
            "Max-Diff Experiment: I: try and get a lot done every day", "Max-Diff Experiment: I: think a lot about what I eat",
            "Max-Diff Experiment: I: care a lot about what others think",
            "Max-Diff Experiment: I: like to take chances", "Max-Diff Experiment: I: just want to have fun",
            "Max-Diff Experiment: I: spend as much time with my friends as I can",
            "Max-Diff Experiment: I: like to work on community projects",
            "Max-Diff Experiment: I: work hard to stay fit and healthy",
            "Max-Diff Experiment: I: SUM", "Awareness: Coca-Cola", "Awareness: Diet Coke",
            "Awareness: Coke Zero", "Awareness: Pepsi", "Awareness: Diet Pepsi",
            "Awareness: Pepsi Max", "Awareness: NET"), c("Living with your parents/guardian",
            "Living alone", "Living with partner only", "Living with children only",
            "Living with partner and children", "Sharing accommodation",
            "Other (Please type into the box.)")))

})

test_that("Ordered factors",
{
    res <- SegmentComparisonTable(formOrderedCat, group = income)
    expect_equal(attr(res, "ChartData"),
        structure(c(23, 0.0325318246110325, 0.347826086956522, 0, 0,
        0.0869565217391304, 0, 0, 0, 0.478260869565217, 0.0869565217391304,
        77, 0.108910891089109, 0.194805194805195, 0.0649350649350649,
        0, 0.0779220779220779, 0.168831168831169, 0.038961038961039,
        0.0909090909090909, 0.207792207792208, 0.155844155844156, 73,
        0.103253182461103, 0.164383561643836, 0.0547945205479452, 0.0821917808219178,
        0.0958904109589041, 0.0410958904109589, 0.0821917808219178, 0.123287671232877,
        0.26027397260274, 0.0958904109589041, 129, 0.182461103253182,
        0.13953488372093, 0.0697674418604651, 0.162790697674419, 0.10077519379845,
        0.0775193798449612, 0.124031007751938, 0.108527131782946, 0.0465116279069767,
        0.170542635658915, 158, 0.223479490806223, 0.0822784810126582,
        0.113924050632911, 0.0886075949367089, 0.0822784810126582, 0.20253164556962,
        0.0886075949367089, 0.164556962025316, 0.164556962025316, 0.0126582278481013,
        103, 0.145685997171146, 0.0776699029126214, 0.271844660194175,
        0.116504854368932, 0.184466019417476, 0.0679611650485437, 0.029126213592233,
        0.155339805825243, 0.0970873786407767, 0, 59, 0.0834512022630834,
        0.0677966101694915, 0.101694915254237, 0.169491525423729, 0.0677966101694915,
        0.101694915254237, 0.135593220338983, 0.101694915254237, 0.254237288135593,
        0, 44, 0.0622347949080622, 0.0454545454545455, 0.113636363636364,
        0.113636363636364, 0.295454545454545, 0.0681818181818182, 0.0909090909090909,
        0.0454545454545455, 0.227272727272727, 0, 41, 0.057991513437058,
        0.170731707317073, 0.170731707317073, 0.146341463414634, 0.0731707317073171,
        0.146341463414634, 0, 0.146341463414634, 0.146341463414634, 0
        ), .Dim = c(11L, 9L), .Dimnames = list(c("Income: Sample size",
        "Income: Percentage", "Age: 18 to 24", "Age: 25 to 29", "Age: 30 to 34",
        "Age: 35 to 39", "Age: 40 to 44", "Age: 45 to 49", "Age: 50 to 54",
        "Age: 55 to 64", "Age: 65 or more"), c("Less than $15,000", "$15,001 to $30,000",
        "$30,001 to $45,000", "$45,001 to $60,000", "$60,001 to $90,000",
        "$90,001 to $120,000", "$120,001 to $150,000", "$150,001 to $200,000",
        "$200,001 or more"))))

    res2 <- SegmentComparisonTable(list(income), group = formOrderedCat[[1]])
    expect_equal(attr(res2, "ChartData"),
        structure(c(99, 0.12375, 0.0919540229885057, 0.172413793103448,
        0.137931034482759, 0.206896551724138, 0.149425287356322, 0.0919540229885057,
        0.0459770114942529, 0.0229885057471264, 0.0804597701149425, 94,
        0.1175, 0, 0.0609756097560976, 0.0487804878048781, 0.109756097560976,
        0.219512195121951, 0.341463414634146, 0.0731707317073171, 0.0609756097560976,
        0.0853658536585366, 83, 0.10375, 0, 0, 0.0810810810810811, 0.283783783783784,
        0.189189189189189, 0.162162162162162, 0.135135135135135, 0.0675675675675676,
        0.0810810810810811, 91, 0.11375, 0.025, 0.075, 0.0875, 0.1625,
        0.1625, 0.2375, 0.05, 0.1625, 0.0375, 93, 0.11625, 0, 0.1625,
        0.0375, 0.125, 0.4, 0.0875, 0.075, 0.0375, 0.075, 63, 0.07875,
        0, 0.0555555555555556, 0.111111111111111, 0.296296296296296,
        0.259259259259259, 0.0555555555555556, 0.148148148148148, 0.0740740740740741,
        0, 95, 0.11875, 0, 0.0813953488372093, 0.104651162790698, 0.162790697674419,
        0.302325581395349, 0.186046511627907, 0.0697674418604651, 0.0232558139534884,
        0.0697674418604651, 126, 0.1575, 0.092436974789916, 0.134453781512605,
        0.159663865546218, 0.0504201680672269, 0.218487394957983, 0.0840336134453782,
        0.126050420168067, 0.0840336134453782, 0.0504201680672269, 56,
        0.07, 0.0444444444444444, 0.266666666666667, 0.155555555555556,
        0.488888888888889, 0.0444444444444444, 0, 0, 0, 0), .Dim = c(11L,
        9L), .Dimnames = list(c("Age: Sample size", "Age: Percentage",
        "Income: Less than $15,000", "Income: $15,001 to $30,000", "Income: $30,001 to $45,000",
        "Income: $45,001 to $60,000", "Income: $60,001 to $90,000", "Income: $90,001 to $120,000",
        "Income: $120,001 to $150,000", "Income: $150,001 to $200,000",
        "Income: $200,001 or more"), c("18 to 24", "25 to 29", "30 to 34",
        "35 to 39", "40 to 44", "45 to 49", "50 to 54", "55 to 64", "65 or more"
        ))))
})

test_that("Order Multi - Grid",
{
    dat.orderedmultigrid <- list(as.data.frame(lapply(brand.attitute[[1]], as.ordered)))
    res <- SegmentComparisonTable(dat.orderedmultigrid, formSegmentation)
    res0 <- SegmentComparisonTable(brand.attitute, formSegmentation)
    expect_equal(attr(res, "p-value"), attr(res0, "p-value"), check.attributes = FALSE)
})

test_that("Empty Segments",
{
    segVar <- structure(c(1L, 6L, 3L, 4L, 2L, 1L, 4L, 3L, 2L, 2L, 3L, 4L, 5L,
                  2L, 2L, 4L, 3L, 5L, 5L, 2L, 3L, 2L, 1L, 5L, 6L, 2L, 1L, 4L, 2L,
                  2L, 4L, 3L, 6L, 2L, 2L, 3L, 2L, 6L, 4L, 3L, 4L, 1L, 2L, 6L, 5L),
                  class = "factor", levels = c("Segment 1", "Segment 2", "Segment 3",
                    "Segment 4", "Segment 5", "Segment 6"), questiontype = "PickOne",
                  dataset = "FinalNausicaaSegmentation_V2.sav", values = c(`Not in tree` = NA,
                    `Segment 1` = 1, `Segment 2` = 2, `Segment 3` = 3, `Segment 4` = 4,
                    `Segment 5` = 5, `Segment 6` = 6), sourcevalues = c(`Not in tree` = NaN,
                    `Segment 1` = 1, `Segment 2` = 2, `Segment 3` = 3, `Segment 4` = 4,
                    `Segment 5` = 5, `Segment 6` = 6), codeframe = list(`Segment 1` = 1,
                    `Segment 2` = 2, `Segment 3` = 3, `Segment 4` = 4, `Segment 5` = 5,
                    `Segment 6` = 6, NET = c(1, 2, 3, 4, 5, 6)), name = "segmentsGINGV",
                    label = "Segmentation_7th", question = "Segmentation_7th")
    profVar <- list(`G8br22: Others, please specify - Please rank the list of information sources about BCL based on your frequency of visit? 1=Most frequent. You can select up to 5 sources.` = structure(c(NA,
                   NA, 2L, NA, NA, NA, NA, 4L, NA, NA, NA, NA, NA, 1L, 1L, 4L, NA,
                   NA, NA, NA, 5L, NA, NA, NA, NA, NA, 5L, 5L, 2L, NA, NA, NA, NA,
                   NA, 3L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
                   class = c("ordered","factor"), levels = c("Rank 1", "Rank 2", "Rank 3", "Rank 4",
                    "Rank 5"), questiontype = "PickOne", dataset = "FinalNausicaaSegmentation_V2.sav", values = c(`Rank 1` = 1,
                      `Rank 2` = 2, `Rank 3` = 3, `Rank 4` = 4, `Rank 5` = 5), sourcevalues = c(`Rank 1` = 1,
                    `Rank 2` = 2, `Rank 3` = 3, `Rank 4` = 4, `Rank 5` = 5), codeframe = list(
                    `Rank 1` = 1, `Rank 2` = 2, `Rank 3` = 3, `Rank 4` = 4, `Rank 5` = 5,
                    NET = c(1, 2, 3, 4, 5)), name = "G8br22",
                  label = "G8br22: Others, please specify - Please rank the list of information sources about BCL based on your frequency of visit? 1=Most frequent. You can select up to 5 sources.", question = "G8br22: Others, please specify - Please rank the list of information sources about BCL based on your frequency of visit? 1=Most frequent. You can select up to 5 sources."))
    expect_error(SegmentComparisonTable(profVar, group=segVar, font.color.set.if.nonsignificant = TRUE,
                weights = rep(c(0.1, 0.2, 0.3), length=45)), NA)
})
Displayr/flipAnalysisOfVariance documentation built on Feb. 19, 2025, 3:19 a.m.