使用向量来制作概率表

时间:2016-07-17 09:12:45

标签: r statistics probability

在概率表的形式中,我想说明一个可被7和5整除的分位数矢量,用于边际概率分布,5个给定7,用于条件概率。

我们假设这是我的数据:

>prob.table(table(x)) # discrete number and its probability
      20       22       23       24       25       26       27       28       29       30       31 
0.000152 0.000625 0.000796 0.001224 0.003138 0.003043 0.004549 0.006444 0.005938 0.009301 0.009456 
      32       33       34       35       36       37       38       39       40       41       42 
0.013448 0.019839 0.018596 0.026613 0.028902 0.027377 0.035156 0.041379 0.041092 0.047733 0.055827 
      43       44       45       46       47       48       49       50       51       52       53 
0.046099 0.051624 0.055131 0.049779 0.056992 0.049801 0.052912 0.031924 0.049114 0.022880 0.042279 
      54       55       56       57       58       59       61       63       65 
0.013946 0.032340 0.003466 0.021240 0.001227 0.011734 0.005115 0.001491 0.000278

如何将其转换为双向概率表,显示哪些数字可以被7和/或5整除以获得边际和条件概率?

这是我希望表格看起来像

       Yes      NO  # Probability of numbers divisible by 7
Yes 0.02754 0.02886 
No  0.02656 0.02831 
# Probability of numbers divisible by 5

1 个答案:

答案 0 :(得分:1)

x <- sample(1:100, 100, replace = TRUE)

# %% is the mod operator, which gives the remainder after the division of the left-hand side by the right-hand side. x %% y == 0 therefore returns TRUE if x is divisible by y
db5 <- x %% 5 == 0
db7 <- x %% 7 == 0

table(db5, db7) / length(x)

#        db7
# db5     FALSE TRUE
#   FALSE  0.62 0.13
#   TRUE   0.24 0.01