2016-08-23 38 views
1

我正在進行marascuilio程序,以比較比例之間的差異。 我使用下面的代碼(複製和改編this tutorialMarascuilo程序中R

## 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) 

我需要輸出將同時打印類別的標籤(例如,它們正好被比較的類別)

所以如果這些類別的分離式向量上市,如categories <- c("cat1", "cat2", cat"3),攀比是cat1-cat2cat1-cat3,並cat2-cat3

我怎麼能這些標籤附加到我的輸出?

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

又該輸出數據框的樣子在上面的例子嗎? – thepule

+0

我添加了一個例子,所以基本上應該添加另一列,並攜帶有關類別比較的信息 –

回答

1

試試這個:

## 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) 

輸出:

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 
+1

太好了。我將'df $ significance < - ifelse(df $ value> df $ critical.range,「yes」,「no」)加到'df'中,因此它也會指出比較是否顯着。 –