2012-08-16 31 views
0

我正在比較各組之間的一組基線和結束學習差異。對於例子,我可能有以下數據集:如何在r中爲組比較創建表?

> baseline.comp 
          cluster 1970_pred 2008_pred ratio diff 
9 Many Transitions, Middle Income 0.1156 0.0248 4.6613 0.0908 
10  Many Transitions, Low Income 0.1779 0.0389 4.5733 0.1390 
4  Dictatorships, High Income 0.1403 0.0307 4.5700 0.1096 
7 One Transition, Middle Income 0.0801 0.0219 3.6575 0.0582 
1   Democracies, High Income 0.0396 0.0116 3.4138 0.0280 
5  Dictatorships, Middle Income 0.1252 0.0399 3.1378 0.0853 
2  Democracies, Middle Income 0.0811 0.0291 2.7869 0.0520 
8  One Transition, Low Income 0.1912 0.0775 2.4671 0.1137 
3   Democracies, Low Income 0.1612 0.0698 2.3095 0.0914 
6  Dictatorships, Low Income 0.1854 0.0821 2.2582 0.1033 

在這個例子中,我想列pred_1970與自身比較,這樣我可以有一個表,告訴我在這些集羣在基準條件下的差異。這將是一個10乘10的表格,但只有波紋對角線的細胞纔會有實際的數字,反映了這些羣體初始條件的差異。我想知道如果R已經有一些功能實現。

謝謝

安東尼佩德羅

回答

1

outer是你在找什麼。

baseline_diff <- outer(baseline.comp[['1970_pred']],baseline.comp[['1970_pred']], '-') 
## if you want to set the dimension names (but they will be very long!) 
# dimnames(baseline_diff) <- list(baseline.comp[['cluster']], 
#         baseline.comp[['cluster']]) 
baseline_diff 
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] 
[1,] 0.0000 -0.0623 -0.0247 0.0355 0.0760 -0.0096 0.0345 -0.0756 -0.0456 -0.0698 
[2,] 0.0623 0.0000 0.0376 0.0978 0.1383 0.0527 0.0968 -0.0133 0.0167 -0.0075 
[3,] 0.0247 -0.0376 0.0000 0.0602 0.1007 0.0151 0.0592 -0.0509 -0.0209 -0.0451 
[4,] -0.0355 -0.0978 -0.0602 0.0000 0.0405 -0.0451 -0.0010 -0.1111 -0.0811 -0.1053 
[5,] -0.0760 -0.1383 -0.1007 -0.0405 0.0000 -0.0856 -0.0415 -0.1516 -0.1216 -0.1458 
[6,] 0.0096 -0.0527 -0.0151 0.0451 0.0856 0.0000 0.0441 -0.0660 -0.0360 -0.0602 
[7,] -0.0345 -0.0968 -0.0592 0.0010 0.0415 -0.0441 0.0000 -0.1101 -0.0801 -0.1043 
[8,] 0.0756 0.0133 0.0509 0.1111 0.1516 0.0660 0.1101 0.0000 0.0300 0.0058 
[9,] 0.0456 -0.0167 0.0209 0.0811 0.1216 0.0360 0.0801 -0.0300 0.0000 -0.0242 
[10,] 0.0698 0.0075 0.0451 0.1053 0.1458 0.0602 0.1043 -0.0058 0.0242 0.0000 

要僅顯示在Matrix包的下(或上)的三角形使用triltriu

library(Matrix) 

tril(baseline_diff) 

10 x 10 Matrix of class "dtrMatrix" 
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] 
[1,] 0.0000  .  .  .  .  .  .  .  .  . 
[2,] 0.0623 0.0000  .  .  .  .  .  .  .  . 
[3,] 0.0247 -0.0376 0.0000  .  .  .  .  .  .  . 
[4,] -0.0355 -0.0978 -0.0602 0.0000  .  .  .  .  .  . 
[5,] -0.0760 -0.1383 -0.1007 -0.0405 0.0000  .  .  .  .  . 
[6,] 0.0096 -0.0527 -0.0151 0.0451 0.0856 0.0000  .  .  .  . 
[7,] -0.0345 -0.0968 -0.0592 0.0010 0.0415 -0.0441 0.0000  .  .  . 
[8,] 0.0756 0.0133 0.0509 0.1111 0.1516 0.0660 0.1101 0.0000  .  . 
[9,] 0.0456 -0.0167 0.0209 0.0811 0.1216 0.0360 0.0801 -0.0300 0.0000  . 
[10,] 0.0698 0.0075 0.0451 0.1053 0.1458 0.0602 0.1043 -0.0058 0.0242 0.0000 
+0

謝謝!這真的很簡潔! – Tom 2012-08-16 07:08:34

2

嘗試以下方法:

# This part is just to create your data: 

baseline.comp <- read.table(text=" 
          cluster 1970_pred 2008_pred ratio diff 
9 'Many Transitions, Middle Income' 0.1156 0.0248 4.6613 0.0908 
10  'Many Transitions, Low Income' 0.1779 0.0389 4.5733 0.1390 
4  'Dictatorships, High Income' 0.1403 0.0307 4.5700 0.1096 
7 'One Transition, Middle Income' 0.0801 0.0219 3.6575 0.0582 
1   'Democracies, High Income' 0.0396 0.0116 3.4138 0.0280 
5  'Dictatorships, Middle Income' 0.1252 0.0399 3.1378 0.0853 
2  'Democracies, Middle Income' 0.0811 0.0291 2.7869 0.0520 
8  'One Transition, Low Income' 0.1912 0.0775 2.4671 0.1137 
3   'Democracies, Low Income' 0.1612 0.0698 2.3095 0.0914 
6  'Dictatorships, Low Income' 0.1854 0.0821 2.2582 0.1033") 

colnames(baseline.comp) <- c("cluster", "1970_pred", "2008_pred", "ratio", "diff") 

# Now, we use outer 

diff.1970 <- outer(baseline.comp$`1970_pred`, baseline.comp$`1970_pred`, "-") 

# Just renaming the output matrix. I've used A through J to make 
# the output more readable. 

#colnames(diff.1970) <- baseline.comp$cluster 
colnames(diff.1970) <- LETTERS[1:10] 
#rownames(diff.1970) <- baseline.comp$cluster 
rownames(diff.1970) <- LETTERS[1:10] 

# Make sure only the lower half of the result contains non-zero values 

> diff.1970 * lower.tri(diff.1970) 
     A  B  C  D  E  F  G  H  I J 
A 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0 
B 0.0623 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0 
C 0.0247 -0.0376 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0 
D -0.0355 -0.0978 -0.0602 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0 
E -0.0760 -0.1383 -0.1007 -0.0405 0.0000 0.0000 0.0000 0.0000 0.0000 0 
F 0.0096 -0.0527 -0.0151 0.0451 0.0856 0.0000 0.0000 0.0000 0.0000 0 
G -0.0345 -0.0968 -0.0592 0.0010 0.0415 -0.0441 0.0000 0.0000 0.0000 0 
H 0.0756 0.0133 0.0509 0.1111 0.1516 0.0660 0.1101 0.0000 0.0000 0 
I 0.0456 -0.0167 0.0209 0.0811 0.1216 0.0360 0.0801 -0.0300 0.0000 0 
J 0.0698 0.0075 0.0451 0.1053 0.1458 0.0602 0.1043 -0.0058 0.0242 0 

一些注意事項有關此:

一般來說,這不是一個好主意具有以數字開頭的變量(或列名稱)。這就是爲什麼我們在使用read.table時必須重命名列:R會自動在數字之前放置一個'X'。請注意,在引用outer函數中的這些列名稱時,我不得不使用刻度。完全避免這種情況會更好。

至於outer功能,我用了一個小小的變化。通常的調用看起來像x %o% y,這與outer(x, y, "*")相同。然而,在這種情況下,我們感興趣的是差異而不是乘法。

最後一步是乘以lower.tri,它返回一個TRUE/FALSE矩陣,其中對角線以下的所有東西都是TRUE,其他的都是FALSE。如果您使用diag = TRUE作爲參數,則對角線也將爲TRUE,但在此處並不重要,因爲對角線始終爲零。由於R將TRUE設爲1且FALSE設爲零,因此我們可以將原始矩陣乘以lower.tri,以便爲除我們感興趣的值(對角線以下的值)之外的所有內容都返回零值。

+0

我認爲你也可以使用'diff.1970 [upper.tri(diff.1970)] = 0'或類似的東西使上面的三角形成爲「0」。還沒有測試過它。 +1來解釋你在做什麼。 – A5C1D2H2I1M1N2O1R2T1 2012-08-16 06:57:10

+0

謝謝。我非常感謝你的時間。 – Tom 2012-08-16 07:08:01

+0

也感謝您提醒我不要在變量名稱的開頭使用數字。 – Tom 2012-08-16 19:13:34