R:如何为 `rnorm` 的 `mean` 参数提供向量? [英] R: How to provide a vector to the `mean` argument of `rnorm`?

查看:26
本文介绍了R:如何为 `rnorm` 的 `mean` 参数提供向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为 rnormmean 参数提供向量?

How do I provide a vector to the means argument of rnorm?

around_int1_mean <- seq(1.5, 3.5, 0.1)

我想做一些类似 rnorm(n=25, mean=around_int1_mean, sd=0.2) 的事情,避免 for 循环.

I would like to do something like rnorm(n=25, mean=around_int1_mean, sd=0.2), avoiding a for-loop.

我想获得 length(around_int1_mean) 样本集,n=25 的平均值(在第一组中)为 1.5,然后是 1.6,依此类推,直到最后一组的平均值为 3.5.所以最后我会得到 21 组大小为 25 的样本.

I want to get length(around_int1_mean) sets of samples with n=25 with mean (in the first set) of 1.5, then 1.6 and so on until the last set has mean 3.5. So in the end I'd get 21 sets of samples of size 25.

推荐答案

我想获得 length(around_int1_mean) 样本集,n=25 的平均值(在第一组中)为 1.5,然后是 1.6,依此类推,直到最后一组的平均值为 3.5.所以最后我会得到 21 组大小为 25 的样本.

I want to get length(around_int1_mean) sets of samples with n=25 with mean (in the first set) of 1.5, then 1.6 and so on until the last set has mean 3.5. So in the end I'd get 21 sets of samples of size 25.

你需要

rnorm(n = length(around_int1_mean) * 25,
      mean = rep(around_int1_mean, each = 25), sd = 0.2)

rnorm 中的 meansd 参数被向量化.它们将首先被回收为长度n.然后,对于 i = 1, 2, ..., n,从 N(mean[i], sd[i]]).

The mean and sd argument in rnorm are vectorized. They would first be recycled to have length n. Then, for i = 1, 2, ..., n, the i-th sample is drawn from N(mean[i], sd[i]).

再举一个例子,如果你想要每个均值有一个样本,请执行以下操作:

As another example, if you want a single sample for each mean, do:

rnorm(n = length(around_int1_mean), mean = around_int1_mean, sd = 0.2)

<小时>

由于@TMOTTM 坚称我错了并否决了我的答案,我必须出示证据为自己辩护.

around_int1_mean <- seq(1.5, 3.5, by = 0.1)

我会设置 sd = 0 来消除随机性,因此随机样本将取概率为 1 的 mean 值.这使我们能够证明 rnorm 正在为正确的 mean 生成正确的样本集.

I would set sd = 0 to eliminate randomness, so random samples will just take mean value with probability of 1. This enables us to prove that rnorm is generating correct set of samples for correct mean.

x <- rnorm(n = length(around_int1_mean) * 25,
           mean = rep(around_int1_mean, each = 25), sd = 0)

另外,我会用一个矩阵来证明它:

Also, I would use a matrix to demonstrate it:

matrix(x, nrow = 25)

#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
# [1,]  1.5  1.6  1.7  1.8  1.9    2  2.1  2.2  2.3   2.4   2.5   2.6   2.7
# [2,]  1.5  1.6  1.7  1.8  1.9    2  2.1  2.2  2.3   2.4   2.5   2.6   2.7
# [3,]  1.5  1.6  1.7  1.8  1.9    2  2.1  2.2  2.3   2.4   2.5   2.6   2.7
# [4,]  1.5  1.6  1.7  1.8  1.9    2  2.1  2.2  2.3   2.4   2.5   2.6   2.7
# [5,]  1.5  1.6  1.7  1.8  1.9    2  2.1  2.2  2.3   2.4   2.5   2.6   2.7
# [6,]  1.5  1.6  1.7  1.8  1.9    2  2.1  2.2  2.3   2.4   2.5   2.6   2.7
# [7,]  1.5  1.6  1.7  1.8  1.9    2  2.1  2.2  2.3   2.4   2.5   2.6   2.7
# [8,]  1.5  1.6  1.7  1.8  1.9    2  2.1  2.2  2.3   2.4   2.5   2.6   2.7
# [9,]  1.5  1.6  1.7  1.8  1.9    2  2.1  2.2  2.3   2.4   2.5   2.6   2.7
#      [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21]
# [1,]   2.8   2.9     3   3.1   3.2   3.3   3.4   3.5
# [2,]   2.8   2.9     3   3.1   3.2   3.3   3.4   3.5
# [3,]   2.8   2.9     3   3.1   3.2   3.3   3.4   3.5
# [4,]   2.8   2.9     3   3.1   3.2   3.3   3.4   3.5
# [5,]   2.8   2.9     3   3.1   3.2   3.3   3.4   3.5
# [6,]   2.8   2.9     3   3.1   3.2   3.3   3.4   3.5
# [7,]   2.8   2.9     3   3.1   3.2   3.3   3.4   3.5
# [8,]   2.8   2.9     3   3.1   3.2   3.3   3.4   3.5
# [9,]   2.8   2.9     3   3.1   3.2   3.3   3.4   3.5
# [ reached getOption("max.print") -- omitted 16 rows ]

显然我的回答是正确的.每列有 25 个均值相同的样本.

Obviously my answer is correct. Each column has 25 samples with the same mean.

这篇关于R:如何为 `rnorm` 的 `mean` 参数提供向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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