how to generate random numbers from a shifted to the left chi square distribution in R?

谁说我不能喝 提交于 2021-02-17 07:07:15

问题


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.

in r it is said the non centrality parameter must be non negative.


回答1:


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")

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)

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.

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")



来源:https://stackoverflow.com/questions/63729468/how-to-generate-random-numbers-from-a-shifted-to-the-left-chi-square-distributio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!