Implement the t-test for means

You can add the t-test to your visualization to compare the means of two samples.

Prerequisites:
  • Add necessary dimensions. For example, add a Country dimension and Period or Wave dimension, and pivot the fields.
  • Add Average, Standard Deviation, and Count measures as table calculations.
  1. In the Field Picker, select the following measures to include in the Data panel:
    • Count
    • Average
    • Standard Deviation

    The following three steps create table calculations for each of these measures, for use in subsequent table calculations and so that visualizations can easily be duplicated. After you create these table calculations you can hide the fields for these measures in the Data panel.

  2. Create a table calculation for Count:
    1. In the Field Picker, select Add > Table Calculation.
    2. Enter ${ and then select the Count measure from the drop-down list.
    3. In the Name text box, enter Count as the field name.
    4. Click Save.
  3. Create a table calculation for Average:
    1. In the Field Picker, select Add > Table Calculation.
    2. Enter ${ and then select the Field Name? measure from the drop-down list.
    3. In the Name text box, enter Average as the field name.
    4. Click Save.
  4. Create a table calculation for Standard Deviation:
    1. In the Field Picker, select Add > Table Calculation.
    2. Enter ${ and then select the Field name? measure from the drop-down list.
    3. In the Name text box, enter STD as the field name.
    4. Click Save.
  5. Create a table calculation for Period over period change (PoP%):
    1. In the Field Picker, select Add > Table Calculation.
    2. Copy the table calculation template into the Expression text box.
      if(${period/sample_group}="1" # 1 is equal to the period number
      # This can alternatively be equal to a sample_group name
      # ----- Begin second pivot section ----------------------------------
      # Remove this section if you aren't including a second pivoted field
      OR ${second_pivot}!=pivot_offset(${second_pivot}, -1)
      # what the previous line of code is doing is that given the current column 
      # like "Canada", return true if the column before's value is not "Canada"
      # ----- End second pivot section   ----------------------------------
      ,null,
      ((${sample1_average}-pivot_offset(${sample2_average}, -1))
      /
      abs(pivot_offset(${sample1_average}, -1))))
      # the -1 arguments above are taking the previous value
    3. Replace ${period/sample_group} in the first line with the period, sample group or wave dimension in your data.
    4. Replace all instances of ${sample1_average} and ${sample2_average} with the ${average} table calculation.
    5. In the Format drop-down list, select Percent.
    6. In the Decimals text box, enter 0.
    7. In the Name text box, enter the field name.
      For example: PoP%
    8. Click Save.
  6. Create a table calculation for the Minimum sample size t-test:
    1. In the Field Picker, select Add > Table Calculation.
    2. Copy the table calculation template into the Expression text box.
      # Using 95% confidence interaval (1.96) and Power of 0.8 (~0.84)
      (
      (2* power((1.96+0.84),2) * (power(${std_1},2)/${count_1} 
      + 
      power(pivot_offset(${std_2},-1),2)/pivot_offset(${count_2},-1) ))
      /
      (power((${sample1_average} - pivot_offset(${sample2_average},-1)),2))
      )
    3. Replace all instances of ${std_1} and ${std_2} with the ${std} table calculation, ${count_1} and ${count_2} with the ${count} table calculation, and ${sample1_average} and ${sample2_average} with the ${average} table calculation.
    4. In the Name text box, enter the field name.
      For example: Minimum Sample Size T-test
    5. Click Save.
    6. In the Data table, click the gear icon for the Minimum Sample Size T-test column and select Hide this field from visualization.
  7. Create a table calculation for the T-test:
    1. In the Field Picker, select Add > Table Calculation.
    2. Copy the table calculation template into the Expression text box.
      # As the the sample sizes cause the degrees of freedom to far exceed 100, 
      # we can assume +/- 1.96 for the Critical T-Value for a 2-tail hypothesis.
      
      (${sample1_average} - pivot_offset(${sample2_average},-1)) 
      /
      (sqrt((power(${sample1_std},2)/${sample1_count}) + (power(pivot_offset(${sample2_std},-1),2)/(pivot_offset(${sample2_count},-1)))))
    3. Replace all instances of ${sample1_average} and ${sample2_average} with the ${average} table calculation, ${sample1_std} and ${sample2_std} with the ${std} table calculation, and ${sample1_count} and ${sample1_count} with the ${count} table calculation.
    4. In the Name text box, enter the field name.
      For example: T-test
    5. Click Save.
  8. Create a table calculation for the Final T-test:
    1. In the Field Picker, select Add > Table Calculation.
    2. Copy the table calculation template into the Expression text box.
      # The following if statement allows only Wave over Wave comparisons
      # between 2 different waves not the ${second_pivot} column.
      # ${Wave_Number} and ${second_pivot} can be replaced when assessing other comparisons.
      if(${Wave_Number}=1
      # ----- Begin second pivot section ----------------------------------
      # Remove this section if you aren't including a second pivoted field
      OR ${second_pivot}!=pivot_offset(${second_pivot}, -1)
      # what the previous line of code is doing is that given the current column 
      # like "Canada", return true if the column before's value is not "Canada"
      # ----- End second pivot section   ----------------------------------
      ,null,
      # The following if statement requires the sample size for each sample
      # to meet a minimum sample size in order to calculate the t-test score
      if(${sample1_count} < ${minimum_sample_size_t_test} 
        OR
        pivot_offset(${sample2_count}, -1) < ${minimum_sample_size_t_test},null,
      # Calculate t-test score if the above if statements are passed
        ${t_test}))
    3. Replace all instances of ${Wave_Number} with the Period or Wave dimension, and ${sample1_count} and ${sample1_count} with the ${count} table calculation.
    4. In the Format drop-down list, select Decimals.
    5. In the Decimals text box, enter 2.
    6. In the Name text box, enter the field name.
      For example: Final T-test
    7. Click Save.
  9. Optional: Create a table calculation for T-test Significant:
    1. In the Field Picker, select Add > Table Calculation.
    2. Copy the table calculation template into the Expression text box.
      if(is_null(${final_t_test}),
        null,
        if(abs(${final_t_test})>=1.96,
        1,0))