[英]Transform variable to gamma distribution in R
I have a variable that I want to transform to a gamma distribution with known shape and rate parameters. 我有一个要转换为具有已知形状和速率参数的伽马分布的变量。 How can I transform the variable to a gamma distribution in R? 如何在R中将变量转换为伽马分布? I've looked into the dgamma, pgamma, and qgamma functions, but I can't tell if any will do what I want. 我已经研究了dgamma,pgamma和qgamma函数,但无法确定是否有任何功能可以满足我的要求。
Here's a small example: 这是一个小例子:
variable <- rnorm(100)
shape <- .83
rate <- .01
Note: I realize this example uses normally distributed data (which doesn't fit a gamma distribution), but I need to rescale the variable to the original gamma distribution. 注意:我意识到本示例使用正态分布的数据(不适合伽玛分布),但是我需要将变量重新缩放为原始伽玛分布。
Use the distribution and quantile functions to translate: 使用分布和分位数功能来翻译:
qgamma(pnorm(variable), shape=.83, rate=.01)
This assumes that variable
has mean 0, sd 1 (as it does for your example). 假设variable
平均值为0,标准差为1(如您的示例所示)。 Otherwise you can pass the mean and sd into pnorm
. 否则,您可以传递均值并将sd转换为pnorm
。
To see the transformation: 要查看转换:
plot(density(variable))
plot(density(qgamma(pnorm(variable), shape=.83, rate=.01)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.