简体   繁体   English

MASS软件包的“ fitdistr”:处理经处理的随机数据时出错

[英]MASS packages' “fitdistr”: Error when dealing with manipulated random data

Background: 背景:

Below I have generated some random beta data using R and manipulate the shape of the data a bit to arrive at what I call " Final " in my code. 下面,我使用R生成了一些随机的Beta数据,并对数据的形状进行了一些调整,以得出我在代码中称为Final ”的内容 And I histogram " Final " in my code. 我在代码中直方图Final

Question: 题:

I'm wondering why when trying to fit a "beta" distribution to " Final " data using MASS packages' " fitdistr " function, I get the following error (Any suggestion how to avoid this error)? 我想知道为什么当尝试使用MASS软件包的“ fitdistr ”函数将“ beta”分布拟合为Final数据时,出现以下错误(任何建议如何避免此错误)?

Error in stats::optim(x = c(0.461379379270288, 0.0694261016478062, 0.76934266883081, : initial value in 'vmmin' is not finite

Here is my R code: 这是我的R代码:

 require(MASS)

## Generate some data and manipulate it
set.seed(47)

Initial = rbeta(1e5, 2, 3)
d <- density(Initial)

b.5 <- dbeta(seq(0, 1, length.out = length(d$y)), 50, 50)
b.5 <- b.5 / (max(b.5) / max(d$y))    # Scale down to max of original density

 b.6 <- dbeta(seq(0, 1, length.out = length(d$y)), 60, 40)
 b.6 <- b.6 / (max(b.6) / max(d$y))

 # Collect maximum densities at each x to use as sample probability weights
 p <- pmax(d$y, b.5, b.6)


Final <- sample(d$x, 1e4, replace = TRUE, prob = p) ## THIS IS MY FINAL DATA

hist(Final, freq = F, ylim = c(0, 2))               ## HERE IS A HISTOGRAM

 m <- MASS::fitdistr(Final, "beta",          ## RUN THIS TO SEE HOW THE ERROR COMES UP
                start = list(shape1 = 1, shape2 = 1))

Here is the code. 这是代码。

It is the same with your code, I just removed the negative beta values. 您的代码也是如此,只是删除了负的beta值。

library(MASS)

set.seed(47)

Initial = rbeta(1e5, 2, 3)
d <- density(Initial)

b.5 <- dbeta(seq(0, 1, length.out = length(d$y)), 50, 50)


b.5 <- b.5 / (max(b.5) / max(d$y))    # Scale down to max of original 
density

b.6 <- dbeta(seq(0, 1, length.out = length(d$y)), 60, 40)
b.6 <- b.6 / (max(b.6) / max(d$y))

# Collect maximum densities at each x to use as sample probability weights
p <- pmax(d$y, b.5, b.6)


Final <- sample(d$x, 1e4, replace = TRUE, prob = p) ## THIS IS MY FINAL DATA

hist(Final, freq = F, ylim = c(0, 2))               ## HERE IS A HISTOGRAM

这是原始的直方图

# replace negative beta values with smallest value > 0
Final[Final<= 0] <- min(Final[Final>0])

hist(Final, freq = F, ylim = c(0, 2))

去除负值后的直方图

m <- MASS::fitdistr(x = Final, densfun = "beta",          
                start = list(shape1 = 1, shape2 = 1))

Here are the shape parameters: 以下是形状参数:

> m
     shape1       shape2  
  1.99240852   2.90219720 
 (0.02649853) (0.04010168)

Take note that it gives some warnings. 请注意,它会发出一些警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM