简体   繁体   English

在R中以不同比例模拟离散分布

[英]Simulating a discrete distribution on a different scale in R

I'm new to R and have this question. 我是R新手,有这个问题。 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. 在此任务中,将给他们分配6个面孔(从1-6开始)的骰子,并要求将其私下投掷。 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)? 但是,我想知道是否可以使用此数据来模拟给他们分配10个面(从1-10)的骰子的情况? How can I achieve this in R? 如何在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 . 我使用sn包将倾斜的正态分布拟合到您现有的数据,然后对其进行缩放以表示一个十面的模具,并使用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. 如果需要,只需将dat设置为现有数据。

Now to parametise the distribution using the sn package. 现在使用sn软件包对分布进行参数化。 (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: 要模拟112个学生滚动一个10面骰子并将结果绘制成直方图,请执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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