【问题标题】:Use vector to make probability table使用向量制作概率表
【发布时间】:2016-07-17 17:31:32
【问题描述】:

以概率表的形式,我想说明一个可被 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

【问题讨论】:

    标签: r statistics probability


    【解决方案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
    

    【讨论】:

    • 感谢您的快速回复!另外,给定db7=="TRUE",我将如何获得db5=="TRUE" 的条件概率
    • sum(db5 &amp; db7) / sum(db7)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 2019-06-07
    • 2014-05-02
    相关资源
    最近更新 更多