首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用R fit gamma {MASS}拟合伽马分布时出现错误

尝试使用R fit gamma {MASS}拟合伽马分布时出现错误
EN

Stack Overflow用户
提问于 2013-04-12 23:03:53
回答 2查看 10.4K关注 0票数 10

我有一个问题,在R中的fitdistr{MASS}函数。我有这个向量:

代码语言:javascript
复制
a <- c(26,73,84,115,123,132,159,207,240,241,254,268,272,282,300,302,329,346,359,367,375,378, 384,452,475,495,503,531,543,563,594,609,671,687,691,716,757,821,829,885,893,968,1053,1081,1083,1150,1205,1262,1270,1351,1385,1498,1546,1565,1635,1671,1706,1820,1829,1855,1873,1914,2030,2066,2240,2413,2421,2521,2586,2727,2797,2850,2989,3110,3166,3383,3443,3512,3515,3531,4068,4527,5006,5065,5481,6046,7003,7245,7477,8738,9197,16370,17605,25318,58524)

我想用一个命令来拟合伽马分布到数据中:

代码语言:javascript
复制
fitted.gamma <- fitdistr(a, "gamma")

但我有这样的错误:

代码语言:javascript
复制
Error in optim(x = c(26, 73, 84, 115, 123, 132, 159, 207, 240, 241, 254,  : 
non-finite finite-difference value [1]
In addition: Warning messages:
1: In densfun(x, parm[1], parm[2], ...) : NaNs produced
2: In densfun(x, parm[1], parm[2], ...) : NaNs produced
3: In densfun(x, parm[1], parm[2], ...) : NaNs produced
4: In densfun(x, parm[1], parm[2], ...) : NaNs produced

所以我试着初始化参数:

代码语言:javascript
复制
(fitted.gamma <- fitdistr(a, "gamma", start=list(1,1)))

对象fitted.gamma已创建,但在打印时会创建一个错误:

代码语言:javascript
复制
Error in dn[[2L]] : subscript out of bounds

你知道发生了什么吗,或者知道一些其他的R函数来拟合单变量分布吗?

提前感谢您的帮助或回复。

Kuba

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-12 23:14:33

总是先绘制你的东西,你的缩放是很麻烦的。

代码语言:javascript
复制
library(MASS)
a <- c(26,73,84,115,123,132,159,207,240,241,254,268,272,282,300,302,329,346,359,367,375,378, 384,452,475,495,503,531,543,563,594,609,671,687,691,716,757,821,829,885,893,968,1053,1081,1083,1150,1205,1262,1270,1351,1385,1498,1546,1565,1635,1671,1706,1820,1829,1855,1873,1914,2030,2066,2240,2413,2421,2521,2586,2727,2797,2850,2989,3110,3166,3383,3443,3512,3515,3531,4068,4527,5006,5065,5481,6046,7003,7245,7477,8738,9197,16370,17605,25318,58524)
## Ooops, rater wide
plot(hist(a))
fitdistr(a/10000,"gamma") # gives warnings
# No warnings
fitted.gamma <- fitdistr(a/10000, dgamma,  start=list(shape = 1, rate = 0.1),lower=0.001)

现在,您可以决定如何进行缩放

票数 11
EN

Stack Overflow用户

发布于 2015-11-05 01:50:04

对于明显符合伽马分布的数据,但在错误的尺度上(即,就好像它被乘以/除以一个大数字),这里有一种拟合伽马分布的替代方法:

代码语言:javascript
复制
fitgamma <- function(x) {
  # Equivalent to `MASS::fitdistr(x, densfun = "gamma")`, where x are first rescaled to 
  # the appropriate scale for a gamma distribution. Useful for fitting the gamma distribution to 
  # data which, when multiplied by a constant, follows this distribution
  if (!requireNamespace("MASS")) stop("Requires MASS package.")

  fit <- glm(formula = x ~ 1, family = Gamma)
  out <- MASS::fitdistr(x * coef(fit), "gamma")
  out$scaling_multiplier <- unname(coef(fit))
  out
}

用法:

代码语言:javascript
复制
set.seed(40)
test <- rgamma(n = 100, shape = 2, rate = 2)*50000
fitdistr(test, "gamma") # fails
dens_fit <- fitgamma(test) # successs
curve(dgamma(x, 2, 2), to = 5) # true distribution
curve(dgamma(x, dens_fit$estimate['shape'], dens_fit$estimate['rate']), add=TRUE, col=2) # best guess
lines(density(test * dens_fit$scaling_multiplier), col = 3)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15974773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档