How to Suppress an Output with a Small Sample Size
When creating dashboards it can be useful to prevent visualizations and tables from appearing when the user applies filters that make the sample size too small. This post describes four different strategies for preventing analyses, or parts thereof, from being shown to the user if the sample size is below a specified threshold.
Method 1: Using the Hide columns/rows with small sample size settings in Visualization
When a visualization is created using Insert > Visualization, there are two options in the Object Inspector for hiding rows and columns of the input table based on specified thresholds. You can also use this method to hide the contents of a table.
To use these methods :
- Create a table that contains the data to be visualized.
- Select the table and click on STATISTICS > Cells and choose the sample size statistic that you wish to use. In the example below, I have used Column Sample Size.
- Press Insert > Visualization and selected your desired visualization (if you want to use a table, just choose any one of the visualizations, and then change OUTPUT > Chart type to Table). The visualization needs to be on the same page as the table in order for the visualization to automatically update when filtered.
- Link the table to the visualization by choosing the table in DATA SOURCE > Output in 'pages'.
- If you are wanting to hide columns, select Inputs > COLUMN MANIPULATIONS > Hide columns with small sample sizes. If rows: Inputs > ROW MANIPULATIONS > Hide rows with small sample sizes.
- Specify the Sample size cut-off. It defaults to 30.
The visualization will now automatically hide elements of the visualization with insufficient sample sizes. In the case of the example table above, with the sample size being restricted to 30, the 65 or more column of the table is excluded from the visualization.
When a user filters this page, any data that is below the specified sample size will automatically be excluded. If no data can be shown, the visualization will be replaced with the output shown below. To try for yourself, click here to view the dashboard, click the Filters link at the very top of the screen and choose 65 or more and press OK. As this filter group only has 22 respondents, the entire visualization will be removed. To play around with the original document itself, click here.
Method 2: Making an invisible box visible with R code
- Insert > R Output (Analysis)
- Resize the output to cover whatever you wish to disappear.
- Paste in the code below, modifying the first two lines to suit your needs, and press CALCULATE .
n = sample.size.ranking.table[1, 1] minimum.sample.size = 8 html = if (n >= minimum.sample.size) '' else '<!DOCTYPE html> <html> <head> <style> body { background-color: white; } </style> </head> <body> Sample size is too small to show results </body> </html>' rhtmlMetro::Box(html, text.as.html = TRUE)
Method 3: Creating an error with R code
The stop function in R code prevents an output from being displayed. For example, using the same table as above (which has the name of mytable), the code below the output prevents the visualization from being shown whenever the sample size is less than 100.
data = data.table[-5, -10,"Column %"] sample.size = max(data.table[,,"Column Sample Size"]) if (sample.size == 100) stop("The sample size is too small for valid analysis.") flipChart::CChart("Column", data)
Method 4: Create a Rule using Q
Additional customization is also available by modifying your document using Q. Q is Displayr's sister product, and permits much greater customization of tables (e.g., tables with nested categories and multiple statistics on the table). If you do not have a Q license, you can download a free trial to do what you need to do.
The process for setting this up is:
- Create a table or chart to be exported. (Note that if we're using a visualization, we modify the input table rather than the visualization itself.)
- Export your document using Export and any one of Embed or Web page options. This causes Displayr to add the most recent version of the document the Version history.
- Go to the document settings (see the image shown to the right).
- Expand out the Versions History group and press the Download link next to the most recent version and open it in Q.
- Select the table(s) or chart(s) of interest.
- Automate > Browse Online Library > Modify the Whole table or Plot > Avoid Showing Outputs with Small Sample Sizes or Hide Rows and Columns with Small Sample Sizes and choose your desired settings (alternatively, write a custom rule if you want to do something more exotic).
- Save as a QPack
- Go back to the document settings page in Displayr, click on the + Upload New Version button at the bottom of the Version History.
To try for yourself, click here to view this example. Click the Filters link at the very top of the screen and choose 65 or more. Note that we get a different message to that which was provided in the two previous examples; we can customize the wording using Table JavaScript and Plot JavaScript. To play around with the original document itself, click here.
Check out more of our handy tips on Using Displayr!