【问题标题】:Simulating a discrete distribution on a different scale in R在 R 中模拟不同尺度的离散分布
【发布时间】:2017-11-25 06:06:10
【问题描述】:

我是 R 新手,有这个问题。如标题所述,我有学生报告的骰子数量分布。在这个任务中,他们得到一个有 6 个面的骰子(从 1 到 6),并被要求私下掷骰子。数据如图所示。

但是,我想知道是否可以使用这些数据来模拟给他们一个有 10 个面的骰子的情况(从 1 到 10)?如何在 R 中实现这一点?

【问题讨论】:

    标签: r distribution discrete-mathematics


    【解决方案1】:

    如果您想使用现有的六面模具数据,请进行第二次尝试。我使用snpackage 将偏斜的正态分布拟合到您现有的数据中,然后将其缩放以表示一个十面芯片并使用round 使其离散。

    首先我会模拟你的数据

    set.seed(9999)
    n=112
    a = rnorm( 42, 3, 1 )
    b = rnorm( 70, 5, 0.5 )
    dat = round(c( a, b))
    dat[!(dat %in% 1:6)] = NA
    dat=dat[complete.cases(dat)]
    
    hist(dat,breaks = seq(0.5, 6.5,1), col = rgb(0,0,1,0.25))
    

    如果需要,只需将 dat 设置为现有数据即可。

    现在使用sn 包对分发进行参数化。 (如果您愿意,可以尝试适应其他分布)

    require(sn)
    cp.est = sn.mple(y=dat,opt.method = "nlminb")$cp 
    dp.est = cp2dp(cp.est,family="SN")
    
    ##example to sample from the distribution and compare to existing
    sim = rsn(n, xi=dp.est[1], omega=dp.est[2], alpha=dp.est[3])
    sim = round(sim)
    sim[!(sim %in% 1:6)] = NA
    hist(sim,breaks = seq(0.5, 6.5,1), col = rgb(1,0,0,0.25), add=T)
    

    现在缩放分布以表示一个十面骰子。

    sim = rsn(n, xi=dp.est[1], omega=dp.est[2], alpha=dp.est[3])/6*10
    sim <- round(sim)
    sim[!(sim %in% 1:10)] = NA
    hist(sim,breaks = seq(0.5, 10.5,1), col = rgb(0,1,0,0.25))
    

    【讨论】:

    • 太棒了!正是我要找的,非常感谢!
    • 我很高兴@GAM780。如果有效,请勾选接受答案:)
    【解决方案2】:

    模拟 112 名学生掷出一个十面骰子并在直方图中绘制结果:

    n=112
    res = sample(1:10, size = n, replace = T)
    hist(res)
    

    【讨论】:

    • 感谢您的回答,但我认为这段代码不会重用我为 6 面骰子收集的数据?!?
    猜你喜欢
    • 2012-07-15
    • 1970-01-01
    • 2022-01-17
    • 2020-05-20
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 2015-09-18
    相关资源
    最近更新 更多