R中正态分布随机变量的模拟平均值

问题描述 投票:2回答:1

我正在尝试在R中模拟一些数据,以检查我的手动计算方法,以了解在简单模型中方差如何变化,该模型涉及一系列正态分布的随机变量,取其平均值。但是,我发现得到的结果不仅与我的手动计算不一致,而且彼此不一致。显然我在做错事,但是在隔离问题上遇到了麻烦。

从概念上讲,模型涉及两个步骤:首先,存储变量,其次,使用存储的变量生成输出。然后将输出存储为新变量,以促进将来的输出,依此类推。我假设存储是嘈杂的(即存储的是随机变量而不是常量),但是不会在输出产生中添加更多的噪音,这仅涉及对现有存储变量进行平均。因此,我的模型涉及以下步骤,其中V_i是步骤i中存储的变量,O_i是第i个输出:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9xZVkyWS5wbmcifQ==” alt =“模型方程式的图像”>“>

依此类推。

我已经尝试通过两种方式在R中对此进行模拟:首先,

nSamples <- 100000
o1 <- rnorm(nSamples) # First output
o2 <- rowMeans(cbind(rnorm(nSamples, mean=o1),rnorm(nSamples))) # Second output, averaged from first two stored variables.
o3 <- rowMeans(cbind(rnorm(nSamples, mean=o2),rnorm(nSamples, mean=o1),rnorm(nSamples))) # Third output, averaged from first three stored variables.

这给了我

var(o1) # Approximately 1, as per my manual calculations.
var(o2) # Approximately .75, as per my manual calculations.
var(o3) # Approximately .64, where my manual calculations give 19/36 or approximately .528.

起初,我信任代码,并认为我的计算是错误的。然后,我尝试了以下替代代码,这些代码更明确地遵循了我手动使用的步骤:

nSamples <- 100000
initialValue <- 0
v1 <- rnorm(nSamples, initialValue)
o1 <- v1
v2 <- rnorm(nSamples, o1)
o2 <- rowMeans(cbind(v1, v2))
v3 <- rnorm(nSamples, o2)
o3 <- rowMeans(cbind(v1, v2, v3))

这给了我

var(o1) # Approximately 1, as per my calculations.
var(o2) # Approximately 1.25, where my calculations give .75.
var(o3) # Approximately 1.36, where my calculations give approximately .528.

因此,很明显,在使用这三种方法中的至少两种方法时,我做错了事,但是在找出问题根源时遇到了麻烦。我错过了什么导致我的代码行为与预期不同?两个代码示例之间的差异是什么,导致示例中的方差减小而另一方的方差增大?]

我正在尝试在R中模拟一些数据,以检查我的手动计算方法,以了解在简单模型中方差如何变化,该模型涉及一系列正态分布的随机变量,取其平均值。 ...

r statistics normal-distribution
1个回答
0
投票

您的正确计算是第一个,您在求平均值时正在生成普通随机变量的new

© www.soinside.com 2019 - 2024. All rights reserved.