Sometimes you may want to test the responses from a Nominal-Multi variable set against the previous time period, and to plot this in a stacked column chart. This article describes how to go from a table where the years are nested under the responses in the columns:
To a stacked column chart where arrows are shown on the visualization for that tests against the previous period for each variable+response:
Requirements
- A table with at least two columns and at least one row.
- Where the Rows is a Nominal-Multi variable set and the Columns is a Date/Time variable, (required to do the previous period testing).
Method
Setup stat testing to test against the previous period
Set the statistics testing to compare to previous period, more detail is in How to Conduct Significance Tests by Comparing to Previous Time Periods if needed. To do so:
- Select the table.
- In the object inspector, click the Appearance tab and from the Significance menu, select Advanced > Test Type.
- From the Date: field, select Compare to previous period
- Click Apply to Selection to apply to only this table, or Apply as Default to apply to all tables using the default significance testing settings.
Use R to reshape the table and setup the visualization
There are two ways to do this:
1 - Using the Statistical Assumptions Attributes
- Click Calculation > Custom Code right
- Copy and paste the following R program into the code window (you will need to change the variable names to match the ones in your file).
# Getting table. Note that this is a four dimensional array.
x = table.Brand.attitude.by.Interview.Date.5
dims = dim(x)
n.brands = dims[1]
n.statements = dims[2]
n.periods = dims[3]
brand.names = dimnames(x)[[1]]
# Getting stat testing information. Note that this appears as a data frame.
# The stat testing is stored in a data frame, which is structured
# so that the first rows in the data frame represent the first row of the table
# (that is, it is storing the stat testing information according to the rows
# in the table that contains the percentages)
# By happy coincidence in this case it is already in the same order that we will
# want so that it matches up with them modifications that we will make to the
# table containing the percentages
stat.testing.info = attr(x, "QStatisticsTestingInfo")
# Rearrangin the percentages to a format that is liked by the visualization
# swapping around dimensions
x = aperm(x, c(3, 2, 1))
# Replacing a 3D array with a 2D array, by subscripting and then splicing by row
library(abind)
x = abind(x[,,1], x[,,2], x[,,3], along = 1)
# Updating column names
rownames(x) = paste(rep(brand.names, rep(n.periods, n.brands)),
rownames(x)[1:n.periods])
# Appending the statistic name, so Displayr recognizes the data in the table
attr(x, "statistic") <- "Column %"
# Appending the stat testing
attr(x, "QStatisticsTestingInfo") = stat.testing.info
x
rearranged = x - Click Calculate
- Either click the Visualization button in the object inspector to the left or, while the calculation is selected, from the Visualization menu, select Column > Stacked Column.
The results are as follows:
2 - Using the Z-Statistic and Annotations in the Visualization
- Click Calculation > Custom Code right
- Copy and paste the following R program into the code window (you will need to change the variable names to match the ones in your file).
# Getting table. Note that this is a four dimensional array.
x = table.Brand.attitude.by.Interview.Date.6
dims = dim(x)[1:3]# Ignoring last dimension as unneeded
n.brands = dims[1]
n.periods = dims[3]
brand.names = dimnames(x)[[1]]
# swapping around dimensions
x = aperm(x, c(3, 2, 1, 4))
# Replacing a four dimensional array with a 3 dimensional array by
# subscripting and then splicing together by row
library(abind)
x = abind(x[,,1, ], x[,,2, ], x[,,3, ], along = 1)
# Updating column names
n.months = dim(x)
rep(n.periods, n.brands)
rownames(x) = paste(rep(brand.names, rep(n.periods, n.brands)),
rownames(x)[1:n.periods])
rearranged.with.z = x
4. Either click the Visualization button in the object inspector to the left or, while the calculation is selected, from the Visualization menu, select Column > Stacked Column.
The results are as follows:
Next
How to Test Against the Previous Period without a Date/Time Variable
How to Create Date Variables in Displayr