如何从 R 中向左移动的卡方分布生成随机数? [英] how to generate random numbers from a shifted to the left chi square distribution in R?
问题描述
我想从具有 3 个自由度但向左移动的卡方分布生成随机数.我的意思是移位分布函数 f(x-a) a 是移位量.
I want to generate random numbers from chi-square distribution with 3 degrees of freedom but shifted to the left . I mean the shifted distribution function f(x-a) a is the amount of shifting.
在 r 中表示非中心性参数必须为非负.
in r it is said the non centrality parameter must be non negative.
推荐答案
让我们看一下具有 3 个自由度的卡方分布:
Let's look at the Chi-square distribution with 3 degrees of freedom:
x_vals <- seq(0, 10, 0.1)
plot(x_vals, dchisq(x_vals, 3), type = "l",
main = "Chi Squared distribution of x with 3 DOF")
现在让我们将它向左移动一个常量 a
.我们将在 x = 0 处绘制一条垂直线以强调偏移:
Now let's shift it to the left by a constant a
. We'll plot a vertical line at x = 0 to emphasize the shift:
a <- 2
plot(x_vals - a, dchisq(x_vals, 3), type = "l",
main = "Chi Squared distribution of x - 2 with 3 DOF")
abline(v = 0, lty = 2)
这是您希望从中抽样的分布.既然如此,我们只需要从卡方分布中采样,然后从每个绘制的元素中减去 a
.在 R 中,这就像执行 rchisq(n, 3) - a
一样简单,其中 n
是所需的样本大小.
This is the distribution from which you wish to sample. That being the case, we need only sample from the Chi-square distribution and subtract a
from each element drawn. In R this is as easy as doing rchisq(n, 3) - a
where n
is the desired sample size.
为了演示,以下是从该分布中抽取的 10,000 个样本的直方图:
To demonstrate, here is a histogram of 10,000 samples drawn from this distribution:
hist(rchisq(10000, 3) - a, breaks = 100, xlim = c(-2, 8),
main = "10,000 samples from Chi Square distribution of (x - 2) with 3 DOF")
这篇关于如何从 R 中向左移动的卡方分布生成随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!