[英]Simulating a discrete distribution on a different scale in R
如果要使用現有的六面芯片數據,請第二次嘗試。 我使用sn
包將傾斜的正態分布擬合到您現有的數據,然后對其進行縮放以表示一個十面的模具,並使用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))
要模擬112個學生滾動一個10面骰子並將結果繪制成直方圖,請執行以下操作:
n=112
res = sample(1:10, size = n, replace = T)
hist(res)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.