RUNIF和R中的样本之间的区别? [英] Difference between runif and sample in R?

查看:192
本文介绍了RUNIF和R中的样本之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就他们使用的概率分布而言?我知道runif给出分数,样本给出整数,但是我感兴趣的是样本是否也使用均匀概率分布"?

In terms of probability distribution they use? I know that runif gives fractional numbers and sample gives whole numbers, but what I am interested in is if sample also use the 'uniform probability distribution'?

推荐答案

请考虑以下代码和输出:

Consider the following code and output:

> set.seed(1)
> round(runif(10,1,100))
 [1] 27 38 58 91 21 90 95 66 63  7
> set.seed(1)
> sample(1:100, 10, replace=TRUE)
 [1] 27 38 58 91 21 90 95 67 63  7

这强烈表明,当要求执行相同的操作时,这两个函数给出的输出几乎相同(有趣的是,round给出的输出相同,而不是floorceiling).主要区别在于默认值,如果不更改这些默认值,则两者都将提供一个称为统一的名称(尽管sample被认为是离散的统一名称,默认情况下不替换).

This strongly suggests that when asked to do the same thing, the 2 functions give pretty much the same output (though interestingly it is round that gives the same output rather than floor or ceiling). The main differences are in the defaults and if you don't change those defaults then both would give something called a uniform (though sample would be considered a discrete uniform and by default without replacement).

编辑

Edit

更正确的比较是:

> ceiling(runif(10,0,100))
 [1] 27 38 58 91 21 90 95 67 63  7

代替使用round.

我们甚至可以提高一个级别:

We can even step that up a notch:

> set.seed(1)
> tmp1 <- sample(1:100, 1000, replace=TRUE)
> set.seed(1)
> tmp2 <- ceiling(runif(1000,0,100))
> all.equal(tmp1,tmp2)
[1] TRUE

当然,如果使用sampleprobs参数(并非所有值都相等),那么它将不再是统一的.

Of course if the probs argument to sample is used (with not all values equal), then it will no longer be uniform.

这篇关于RUNIF和R中的样本之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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