Dmitry Leykin Dmitry Leykin - 3 months ago 32
R Question

Marascuilo procedure in R

I'm conducting the marascuilio procedure in order to compare differences between proportions.
I'm using the following code (copied and adapted from this tutorial:

## Set the proportions of interest.
p = c(0.3481, 0.1730, 0.4788)
N = length(p)
value = critical.range = c()

## Compute critical values.
for (i in 1:(N-1))
{ for (j in (i+1):N)
{
value = c(value,(abs(p[i]-p[j])))
critical.range = c(critical.range,
sqrt(qchisq(.95,3))*sqrt(p[i]*(1-p[i])/12000 + p[j]*(1-p[j])/12000))
}
}
round(cbind(value,critical.range),3)


I need that the output will print also the labels of the categories (e.g. which categories are exactly being compared).

So if the categories are listed in a seperated vector, e.g.
categories <- c("cat1", "cat2", cat"3)
, the comparisons are
cat1-cat2
,
cat1-cat3
, and
cat2-cat3
.

How can i append these labels to my output?

value critical.range
[1,] 0.175 0.016
[2,] 0.131 0.018
[3,] 0.306 0.016

Answer

Try this:

## Set the proportions of interest.
p = c(0.3481, 0.1730, 0.4788)
N = length(p)
value = critical.range = tag = c()
categories <- c("cat1", "cat2", "cat3")

## Compute critical values.
for (i in 1:(N-1)){ 
    for (j in (i+1):N){

    value <- c(value,(abs(p[i]-p[j])))
    critical.range = c(critical.range,
                       sqrt(qchisq(.95,N-1))*sqrt(p[i]*(1-p[i])/12000 + p[j]*(1-p[j])/12000))
    tag = c(tag, paste(categories[i], categories[j], sep = "-"))

    }
}
df <- as.data.frame(cbind(value,critical.range, tag), stringsAsFactors = F)
df$value <- round(as.numeric(df$value),3)
df$critical.range <- round(as.numeric(df$critical.range),3)

Output:

 value critical.range       tag
1 0.175          0.016 cat1-cat2
2 0.131          0.018 cat1-cat3
3 0.306          0.016 cat2-cat3