# Checking Whether Margins are (Stochastically) Ordered

    library(ordinalTables)

# Checking Whether the Margins are Ordered

For this example, we will use the vision_data.

    print(vision_data)
    #>      [,1] [,2] [,3] [,4]
    #> [1,] 1520  266  124   66
    #> [2,]  234 1512  432   78
    #> [3,]  117  362 1772  205
    #> [4,]   36   82  179  492

Row Sums = 1976, 2256, 2456, 789 Column Sums = 1907, 2222, 2507, 841

## Testing Marginal Homogeneity

A first test is whether the margins can be considered identical. There are two tests of marginal homogeneity, Stuart’s and Bhapkar’s. These are available as Stuart_marginal_homogeneity() and Bhapkar_marginal_homoogeneity(), and for a r X r table there are r - 1 degrees. There is also a third test using the Minimum Discriminannt Information statistic, MDIS, Ireland_marginal_homogeneity()

    stuart_result <- Stuart_marginal_homogeneity(vision_data)
    bhapkar_result <- Bhapkar_marginal_homogeneity(vision_data)
    ireland_result <- Ireland_marginal_homogeneity(vision_data)

For this data, the first two yield very similar values. Suart is `11.9565696` and Bhapkar yields 11.9757202.

Using the MDIS criterion yields another similar result, 11.8365927, as the MDIS can be interpreted similarly to a chi-square with r - 1 (3) degrees of freedom.

In all 3 cases we reject the hypothesis that the margins are the same.

## Using Cliff’s d-test

First, we examine the independent d-test, ignoring the pairing infomation, using Cliff_independent_from_table(vision_data).

    cliff_independent <- Cliff_independent_from_table(vision_data)

Somewhat surprisingly, there is a non-significant difference, d = -0.0169229, with an associated z-score of -1.8709709.

Next, we do a dependent test. We can test whether the columns are greated than the rows using Cliff_dependent_compute_from_table()

    cliff_result <- Cliff_dependent_compute_from_table(vision_data)

It is important to use the Cliff_dependent_compute_from_table() version of the test. The similarly named Cliff_compute_from matrix() expects an N X N d-matrix as input. Calling it with a table yields nonsense results. If for some reason we do want to compute a d-matrix (7477 X 7477 here), use Cliff_dependent_compute_paired_d(). There is a noticeable lag for the computation, although the results are identical. In cases of tables with 7 or more categories, Cliff_dependent_compute_paired_d() has to be used due to limitations in preparing the library (devtools::install() throws an error that the stack has been exceeded).

Here there are 3 tests. The first, z_dw -3.449963, asks whether the within-person change is 0. Here it is significant, indicating that the columns (right eye) are higher than the rows (left eye). The measure z_dw indicates that slightly more than 2% of the time the right eye will score above, the left, versus the pair having the opposite order. With only 16 cells, the table contains a large number of tied comparisons, which contribute to the denominator but not the numerator of the statistic. The second, z_db test is a comparison of the groups after removing the diagonal. That also is significant -3.4533948 indicating that right eyes tend to have more acute vision than left eyes. Third, there is an omnibus test of db + dw, which has a z-score of -3.521673.

## Clayton’s Marginal Location Test

Another test is Clayton’s test for marginal ordering: Clayton_marginal_location(). It operates only on the margins of the table, so we call rowSums() and colSums().

    clayton_result <- Clayton_marginal_location(rowSums(vision_data), colSums(vision_data))

The value of the odds-ratios (1.0568816 and 1.0568808are slightly greater than 1, indicating that columns are higher than rows. However the associated chi-square statistics are both about 3.5 (1.0568816 and 1.0568808for the test, with a non-significant p-value of . Note that, like the independent Cliff test, the Clayton test does not make use of the pairing of the observations in the table.

## Agresti’s Mann-Whitney Test

We can also check using Agresti’s weighted version of the Mann-Whitney test: Agresti_weighted_tau()

    result <- Agresti_weighted_tau(vision_data)

Note that the z-score of `result$z_diff` is similar in size to the Cliff statistic, but the sign is in the opposite direction.

## Agresti’s weighted difference: Agresti_w_diff()

    w <- c(-3, -1, 1, 3)
    agresti_result <- Agresti_w_diff(w, vision_data)

The result is again negative and near -3.5, z = `agresti_result$z_diff`.

We can also compare “spread” of the marginal scores:

    w <- c(3, 1, 1, 3)
    agresti_result_spread <- Agresti_w_diff(w, vision_data)

This time the z-statistic of 0.4742462 indicates no significant difference.

## McCullagh’s Logistic Model: McCullagh_logistic_model()

The final tests to examine are McCullagh’s (1977) logistic model.

    mccullagh_result <- McCullagh_logistic_model(vision_data)

There are two estimators, delta_tilde (0.1608821) and delta_star (0.1609659), which uses weights analogous to those used in the Mantel-Haenszel common odds ratio. Both are compared to the square root of the variance 0.0021748, which is 0.0466353, giving z-scores of 3.4497949 and 3.4515916.

## Conclusions

From these analyses we can: 1) reject the hypothesis of marginal homoogeneity. 2) conclude that columns (vision acuity of right eye) are soomoewhat higher than rows (accuity of left eye) 3) determine that there is important information in the pairing in the data. The statistics that ignored it were not significant, while analyses that included the information were significant.