Skip to contents

Runs the tests behind plot_mosaic() on the table of states by group and returns their results as a data.frame. By default Pearson's chi-square, which treats every state occurrence as independent, is shown next to four tests that allow for sequences nested in actors (sessions in students, students in teams), so the table shows what the nesting changes.

Usage

mosaic_test(x, ...)

# S3 method for class 'group_tna'
mosaic_test(x, actor = NULL, method = nested_methods, level = "test", ...)

# S3 method for class 'tna_data'
mosaic_test(
  x,
  group,
  actor = NULL,
  method = nested_methods,
  level = "test",
  ...
)

Arguments

x

A group_tna or a tna_data object.

...

Arguments passed to the methods.

actor

An optional actor identifier for sequences nested in actors: a column name of the non-sequence data, or a vector with one value per sequence. The default NULL makes every sequence its own actor.

method

The tests to run, any of "chisq", "rao_scott", "donner_klar", "cmh" and "gee". The default runs all five.

level

"test" (the default) for one row per test, "cells" for one row per cell per test.

group

For a tna_data object: a column name of the (meta) data, or a vector of group indicators with one value per sequence.

Value

A data.frame. With level = "test", one row per test with the columns method, test, statistic (F for Rao-Scott, chi-square otherwise), df, df2 (Rao-Scott's denominator degrees of freedom), p_value, n (state occurrences), actors (the actors the test used), design_effect, icc and note. With level = "cells", one row per cell and test with the columns method, group, state, observed and residual (the residual that test gives the tile; NA where it is undefined).

Details

The tests (ported from mosaic_test() of the Nestimate package):

  • "chisq": Pearson's chi-square of the pooled table.

  • "rao_scott": the second-order Rao-Scott corrected chi-square, an F statistic on fractional degrees of freedom (Rao & Scott, 1984), the test of plot_mosaic(actor = ). design_effect is the mean generalised design effect; 1 means the nesting costs nothing.

  • "donner_klar": Pearson's chi-square divided, group by group, by the inflation factor 1 + (m - 1) rho, with rho the intracluster correlation (icc) of the states (Donner & Klar, 1994). It needs actors nested in the groups (each actor in one group).

  • "cmh": the generalised Cochran-Mantel-Haenszel test with each actor as a stratum (Landis, Heyman & Koch, 1978): is there an association within actors? It needs both variables to vary within actors, and tests conditional rather than marginal association.

  • "gee": the Wald test of a marginal model of the state given the group with a cluster-robust covariance (Liang & Zeger, 1986). It is liberal unless the actors are many relative to its degrees of freedom.

Not every test fits every design. When several methods are asked for, a test that cannot run gives a row of NA with the reason in note; asked for alone, it raises an error of class tna_error_nested.

References

Rao, J. N. K., & Scott, A. J. (1984). On chi-squared tests for multiway contingency tables with cell proportions estimated from survey data. The Annals of Statistics, 12(1), 46–60. doi:10.1214/aos/1176346391

Donner, A., & Klar, N. (1994). Methods for comparing event rates in intervention studies when the unit of allocation is a cluster. American Journal of Epidemiology, 140(3), 279–289. doi:10.1093/oxfordjournals.aje.a117247

Landis, J. R., Heyman, E. R., & Koch, G. G. (1978). Average partial association in three-way contingency tables: A review and discussion of alternative tests. International Statistical Review, 46(3), 237–254. doi:10.2307/1402373

Liang, K.-Y., & Zeger, S. L. (1986). Longitudinal data analysis using generalized linear models. Biometrika, 73(1), 13–22. doi:10.1093/biomet/73.1.13

See also

plot_mosaic(), which draws the Rao-Scott residuals.

Examples

d <- prepare_data(
  group_regulation_long,
  actor = "Actor",
  action = "Action",
  time = "Time"
)
#> ── Preparing Data ──────────────────────────────────────────────────────────────
#> ℹ Input data dimensions: 27533 rows, 6 columns
#> ℹ First few time values: 2025-01-01 08:27:07.712698, 2025-01-01
#>   08:35:20.712698, and 2025-01-01 08:42:18.712698
#> ℹ Number of values to parse: 27533
#> ℹ Sample values: 2025-01-01 08:27:07.712698, 2025-01-01 08:35:20.712698, and
#>   2025-01-01 08:42:18.712698
#> ℹ Sample of parsed times: 2025-01-01 08:27:07.712698, 2025-01-01
#>   08:35:20.712698, and 2025-01-01 08:42:18.712698
#> ℹ Time threshold for new session: 900 seconds
#> ℹ Total number of sessions: 2000
#> ℹ Number of unique users: 2000
#> ℹ Total number of actions: 27533
#> ℹ Maximum sequence length: 26 actions
#> ℹ Time range: 2025-01-01 08:01:16.009382 to 2025-01-01 13:03:20.238288
model <- group_model(d, group = "Course")
# Students nested in teams
mosaic_test(model, actor = "Group")
#>        method                    test statistic       df      df2      p_value
#> 1       chisq                 Pearson 233.56026 16.00000       NA 1.198905e-40
#> 2   rao_scott               Rao-Scott  15.35338 12.44286 2476.128 9.856413e-33
#> 3 donner_klar             Donner-Klar 233.56026 16.00000       NA 1.198905e-40
#> 4         cmh Cochran-Mantel-Haenszel        NA       NA       NA           NA
#> 5         gee                GEE Wald 299.91330 16.00000       NA 2.658268e-54
#>       n actors design_effect           icc
#> 1 27533     NA            NA            NA
#> 2 27533    200     0.9507688            NA
#> 3 27533    200     1.0000000 -5.064256e-05
#> 4 27533     NA            NA            NA
#> 5 27533    200            NA            NA
#>                                                                                                                note
#> 1                                                                                                              <NA>
#> 2                                                                                                              <NA>
#> 3                                                                                                              <NA>
#> 4                            Cochran-Mantel-Haenszel stratifies by actor, so both variables must vary within actors
#> 5 Wald test with a cluster-robust covariance; liberal unless the actors are many relative to the degrees of freedom