gpt4 book ai didi

R:如何为 `mean` 的 `rnorm` 参数提供向量?

转载 作者:行者123 更新时间:2023-12-02 09:24:29 26 4
gpt4 key购买 nike

如何为 rnormmean 参数提供向量?

around_int1_mean <- seq(1.5, 3.5, 0.1)

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

我想获得 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, ..., ni-th 样本是从 N(mean[i], sd[i ]).

再举一个例子,如果你想为每个均值取一个样本,那么:

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 来消除随机性,因此随机样本只会取 mean 概率为 1 的值。这使我们能够证明 rnorm 正在为正确的 mean 生成正确的样本集。

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

另外,我会用一个矩阵来演示它:

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 个均值相同的样本。

关于R:如何为 `mean` 的 `rnorm` 参数提供向量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38946042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com