简体   繁体   中英

Simulating a discrete distribution on a different scale in R

I'm new to R and have this question. As mentioned in the title, I have a distribution of reported dice number from students. In this task, they are given a dice with 6 faces (from 1-6) and are asked to throw it in private. The data are plotted as in the picture.

However, I wonder if it's possible that I can use this data to simulate the situation where they are given a dice with 10 faces instead (from 1-10)? How can I achieve this in R?

在此处输入图片说明

Ok second attempt if you want to use your existing six-sided die data. I use the sn package to fit a skewed normal distribution to your existing data and then scale it to represent a ten-sided die and make it discrete using round .

First I will simulate your data

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))

在此处输入图片说明

Just set dat as your existing data if you want.

Now to parametise the distribution using the sn package. (You can try to fit other distributions if you prefer)

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)

在此处输入图片说明

Now scale the distribution to represent a ten-sided die.

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))

在此处输入图片说明

To simulate 112 students rolling a ten-sided die and plotting the results in histogram:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM