Implement the z-test for proportions
Use this statistical test to determine whether the difference between the proportions of two samples is significant or not.
-
In the Field Picker, select the Count measure to include it in the
Data panel.
The following step creates a table calculation for use in subsequent table calculations. After you create this table calculation you can hide the fields for these measures in the Data panel.
-
Create a table calculation for
Count:
- In the Field Picker, select Add > Table Calculation.
-
Enter
${ and then select the
Count measure from the drop-down list.
- In the Name text box, enter Count as the field name.
- Click Save.
-
Create a table calculation for
CountA:
- In the Field Picker, select Add > Table Calculation.
-
Copy the table calculation template into the
Expression text box.
# Convert NULLS to 0s to enable proper calculation if(is_null(${sample1_count}),0,${sample1_count})
-
Replace all instances of
${sample1_count}
with the${count}
table calculation. -
In the
Name text box, enter the field name.
For example: CountA
- Click Save.
-
Create a table calculation for
Column total count:
- In the Field Picker, select Add > Table Calculation.
-
Copy the appropriate table calculation template into the
Expression text box.
sum(${sample1_count})
-
Replace
${sample1_count}
with the${counta}
table calculation. -
In the
Name text box, enter the field name.
For example: Column Total Count
- Click Save.
-
Create a table calculation for
Proportion %:
- In the Field Picker, select Add > Table Calculation.
-
Copy the appropriate table calculation template into the
Expression text box.
${counta}/${column_total_count}
-
In the
Name text box, enter the field name.
For example: Proportion %
- Click Save.
-
Create a table calculation for
Period over period change (PoP%):
- In the Field Picker, select Add > Table Calculation.
-
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 ---------------------------------- 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, ((${proportion}-pivot_offset(${proportion}, -1)))) # the -1 arguments above are taking the previous value
-
Replace
${period/sample_group}
in the first line with the period, sample group or wave dimension in your data. -
If using the second pivot, replace all instances of
${second_pivot}
with the second dimension you are pivoting on. For example, Country. Otherwise, remove the second pivot section. - In the Format drop-down list, select Percent.
- In the Decimals text box, enter 0.
-
In the
Name text box, enter the field name.
For example: PoP%
- Click Save.
-
Create a table calculation for
Row total count:
- In the Field Picker, select Add > Table Calculation.
-
Copy the appropriate 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_count}+if(is_null(pivot_offset(${sample2_count},-1)),0,pivot_offset(${sample2_count},-1))) # the -1 arguments above are taking the previous value
-
Replace
${period/sample_group}
in the first line with the period, sample group or wave dimension in your data. -
If using the second pivot, replace all instances of
${second_pivot}
with the second dimension you are pivoting on. For example, Country. Otherwise, remove the second pivot section. -
Replace all instances of
${sample1_count}
and${sample2_count}
with the${count}
table calculation. -
In the
Name text box, enter the field name.
For example: Row Total Count
- Click Save.
-
Create a table calculation for
Minimum sample size:
- In the Field Picker, select Add > Table Calculation.
-
Copy this table calculation template into the
Expression text box:
# Using 95% confidence interval (1.96) and Power of 0.8 (~0.84) 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,( (2* power((1.96+0.84),2) * (${sample_1_proportion}*(1-${sample_1_proportion}) + pivot_offset(${sample_2_proportion},-1)*(1-pivot_offset(${sample_2_proportion},-1))) / power((${sample_1_proportion}-pivot_offset(${sample_2_proportion},-1)),2)))) #sample_1_proportion and sample_2_proportion are the PERCENTAGES of the total count
-
Replace
${period/sample_group}
in the first line with the period, sample group or wave dimension in your data. -
If using the second pivot, replace all instances of
${second_pivot}
with the second dimension you are pivoting on. For example, Country. Otherwise, remove the second pivot section. -
Replace
${sample1_proportion}
and${sample2_proportion}
with the${proportion}
table calculation. - In the Format drop-down list, select Decimals.
- In the Decimals text box, enter 0.
-
In the
Name text box, enter the field name.
For example: Minimum Sample Size Z-Test Proportion
- Click Save.
-
Create a table calculation for
Z-test proportion:
- In the Field Picker, select Add > Table Calculation.
-
Copy this table calculation template into the
Expression text box:
# Using 95% confidence interval (1.96) and Power of 0.8 (~0.84) 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, #p1-p2 (${sample_1_proportion} - pivot_offset(${sample_2_proportion},-1)) / sqrt( #p (${sample_1_proportion_count} + pivot_offset(${sample_2_proportion_count},-1)) / (${sample_1_total_count} + pivot_offset(${sample_2_total_count},-1)) * #1 - p (1 - (${sample_1_proportion_count} + pivot_offset(${sample_2_proportion_count},-1)) / (${sample_1_total_count} + pivot_offset(${sample_2_total_count},-1))) #1/n1 +1/n2 * (1/${sample_1_total_count} + 1/pivot_offset(${sample_2_total_count},-1)) ) )
-
Replace
${period/sample_group}
in the first line with the period, sample group or wave dimension in your data. -
If using the second pivot, replace all instances of
${second_pivot}
with the second dimension you are pivoting on. For example, Country. Otherwise, remove the second pivot section. -
Replace
${sample1_proportion}
and${sample2_proportion}
with the${proportion}
table calculation. -
Replace
${sample1_proportion_count}
and${sample2_proportion_count}
with the${counta}
table calculation. -
Replace
${sample1_total_count}
and${sample1_total_count}
with the${column_total_count}
table calculation. -
In the
Name text box, enter the field name.
For example: Z-test Proportion
- Click Save.
-
Create a table calculation for
Final z-test proportion:
- In the Field Picker, select Add > Table Calculation.
-
Copy this table calculation template into the
Expression text box:
# The following if statement allows only Wave over Wave comparison between 2 different waves 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, # 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(${sample_1_proportion_count} < ${minimum_sample_size_z_test_proportion} OR pivot_offset(${sample_2_proportion_count}, -1) < ${minimum_sample_size_z_test_proportion},null, # Calculate t-test score if the above if statements are true ${z_test_proportion}))
-
Replace
${period/sample_group}
in the first line with the period, sample group or wave dimension in your data. -
If using the second pivot, replace all instances of
${second_pivot}
with the second dimension you are pivoting on. For example, Country. Otherwise, remove the second pivot section. -
Replace
${sample1_proportion_count}
and${sample2_proportion_count}
with the${counta}
table calculation. -
In the
Name text box, enter the field name.
For example: Final Z-Test Proportion
- Click Save.
-
Create a table calculation for
Z-test proportion significant:
- In the Field Picker, select Add > Table Calculation.
-
Copy this table calculation template into the
Expression text box:
if(is_null(${final_z_test_proportion}), null, if(abs(${final_z_test_proportion})>=1.96, 1,0))
- Uses 95% confidence interval (1.96)
- For the two-tailed hypothesis
-
In the
Name text box, enter the field name.
For example: Z-test Proportion Significant
- Click Save.