![](/img/trans.png)
[英]Generate data from truncated normal distribution with exact mean and sd in R
[英]Generate samples from data following normal distribution but with new mean
我有一個數字向量
set.seed(1)
x <- rnorm(8334, 1.456977, 0.3552899)
mean(x)
[1] 1.454307
本質上,我想從x
隨機抽取2000個數字,以使該樣本的均值較低。
關鍵是我不想生成新的隨機數,而只是從x
采樣而無需替換,因此我得到了一個均值不同的子集。
誰能幫我?
謝謝!
該方法並不是真正的“隨機”方法,因為它僅從小於mean(x)
值中進行選擇。 讓我知道這是否對您足夠好-
set.seed(1)
x <- rnorm(8334, 1.456977, 0.3552899)
mean(x)
[1] 1.454307
y <- sample(x, 2000, prob = x <= mean(x)) # x > mean(x) has 0 chance of getting sampled
all(y %in% x)
[1] TRUE
mean(y)
[1] 1.170856
這實際上與-
z <- sample(x[x <= mean(x)], 2000)
all(z %in% x)
[1] TRUE
mean(z)
[1] 1.172033
另外,對於2000個值,可能的最低mean
是-
mean(sort(x)[1:2000])
[1] 0.9847526
更新-
這是一種從mean(x)
兩側獲取隨機樣本的方法,盡管它是任意的,我不知道這是否可以保證均值小於mean(x)
。 -
z <- sample(x, 2000, prob = (x <= mean(x)) + 0.1)
mean(z)
[1] 1.225991
table(z <= mean(x))
FALSE TRUE
202 1798
如何進行剔除采樣,即從向量中采樣2000個數字,直到找到滿足所需屬性的一個采樣?
set.seed(1)
x <- rnorm(8334, 1.456977, 0.3552899)
m_x <-mean(x)
y <- sample(x, 2000)
while(mean(y) >= m_x)
y <- sample(x, 2000)
mean(y)
#> [1] 1.4477
由reprex軟件包 (v0.3.0)創建於2019-06-18
這應該相當快,因為新均值可能(大約)比舊均值更大或更小。
為示例隨機化正態分布
x= rnorm(8334,1.45,0.355)
選擇一個2000 nums的樣本
y= sample(x,2000)
y均值降低0.5
y=y-05
將y的標准差提高1.5
y= y*1.5
現在,標准差和Y的均值將約為
mean(y)# ~0.9325603
sd(y)# ~0.5348885
希望這是您正在尋找的答案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.