以指定的均值采样整数值 [英] sample integer values with specified mean

查看:88
本文介绍了以指定的均值采样整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在R中生成具有指定均值的整数样本. 我使用mu+sd*scale(rnorm(n))生成了一个n值的样本,该样本的平均值完全等于mu 但这会生成浮点值;我想改为生成整数值.例如,我想生成一个均值= 4的样本.我的样本大小n = 5,生成的值的示例为{2,6,4,3,5}. 关于在满足均值特定值约束的情况下如何在R中执行此操作的任何想法?

I want to generate a sample of integer numbers in R with a specified mean. I used mu+sd*scale(rnorm(n)) to generate a sample of n values that has exactly the mean=mu but this generates floating-point values; I would like to generate integer values instead. For example, I would like to generate a sample of mean=4. My sample size n=5, an example of generated values would be {2,6,4,3,5}. Any ideas on how to do this in R while satisfying the constraint of a specific value of the mean?

推荐答案

选择平均值为mn值等效于选择总计为m*nn值. (我假设您将坚持使用正数整数-否则事情会变得更加棘手!)这是一个基于采样分区的解决方案(一组值合计为所需的总数)统一,但是我不确定这是您想要的,因为它不是在上而是在 partitions 上统一采样...也许其他人可以做得更好,或弄清楚如何对样品进行称重.

Picking n values with a mean of m is equivalent to picking n values that sum to m*n. (I'm assuming you're going to stick to positive integers -- otherwise things get much harder!) Here is a solution based on sampling partitions (sets of values that add up to the desired total) uniformly, but I'm not sure it's what you want, since it doesn't sample uniformly over values, but over partitions ... perhaps someone else can do better, or figure out how to reweight the samples.

在比您的示例大得多的情况下,这种强力解决方案也可能会失败(共有627个分区,共20个分区,共5604个分区,共30个分区,共37338个分区,共40个分区...)

This brute-force solution will also probably fail for cases much larger than your example (there are 627 partitions for a total of 20, 5604 for a total of 30, 37338 for a total of 40 ...)

m <- 4
n <- 5
library("partitions")    
pp <- parts(m*n) ## all sets of integers that sum to m*n (=20 here)
## restrict to partitions with exactly n (=5) non-zero values.
pp5 <- pp[1:5,colSums(pp>0)==n]
set.seed(101) ## for reproducibility
## sample uniformly from this set
pp5[,sample(ncol(pp5),size=1)]  ## 9, 5, 4, 1, 1

这篇关于以指定的均值采样整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆