r - 在 R 中拟合正态分布

标签 r statistics distribution normal-distribution fitdistrplus

我正在使用以下代码来拟合正态分布。 “b”(太大而无法直接发布)数据集的链接是:
link for b

setwd("xxxxxx")
library(fitdistrplus)

require(MASS)
tazur <-read.csv("b", header= TRUE, sep=",")
claims<-tazur$b
a<-log(claims)
plot(hist(a))
enter image description here
绘制直方图后,似乎正态分布应该很好地拟合。
f1n <- fitdistr(claims,"normal")
summary(f1n)

#Length Class  Mode   
#estimate 2      -none- numeric
#sd       2      -none- numeric
#vcov     4      -none- numeric
#n        1      -none- numeric
#loglik   1      -none- numeric

plot(f1n)

Error in xy.coords(x, y, xlabel, ylabel, log) :

'x' is a list, but does not have components 'x' and 'y'


当我尝试绘制拟合分布时出现上述错误,甚至 f1n 的汇总统计数据也关闭。
将不胜感激任何帮助。

最佳答案

看起来你在 MASS::fitdistr 之间混淆了和 fitdistrplus::fitdist .

  • MASS::fitdistr返回类“fitdistr”的对象,并且没有用于此的绘图方法。所以需要提取估计的参数,自己绘制估计的密度曲线。
  • 我不知道你为什么加载包fitdistrplus ,因为您的函数调用清楚地表明您正在使用 MASS .不管怎样,fitdistrplus有功能fitdist它返回类“fitdist”的对象。这个类有 plot 方法,但它不适用于 MASS 返回的“fitdistr” .

  • 我将向您展示如何使用这两个包。
    ## reproducible example
    set.seed(0); x <- rnorm(500)
    

    使用 MASS::fitdistr

    没有可用的绘图方法,所以自己做。
    library(MASS)
    fit <- fitdistr(x, "normal")
    class(fit)
    # [1] "fitdistr"
    
    para <- fit$estimate
    #         mean            sd 
    #-0.0002000485  0.9886248515 
    
    hist(x, prob = TRUE)
    curve(dnorm(x, para[1], para[2]), col = 2, add = TRUE)
    

    enter image description here

    使用 fitdistrplus::fitdist
    library(fitdistrplus)
    FIT <- fitdist(x, "norm")    ## note: it is "norm" not "normal"
    class(FIT)
    # [1] "fitdist"
    
    plot(FIT)    ## use method `plot.fitdist`
    

    enter image description here

    关于r - 在 R 中拟合正态分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39961964/

    相关文章:

    r - 过滤大于 Dplyr R 中数字的比例

    R:将条形图与填充有匹配条形颜色的线条连接起来

    statistics - 寻找模式和统计分析的最佳语言或程序?

    algorithm - 我如何计算准确度?

    MySql:在不均匀分布的数据上均匀分布样本量

    python - 对两个字典执行两个样本 Kolmogorov-Smirnov 检验

    r - 插值年中平均值

    r - 在 R 中将日期转换为 as.POSIXct 并减去两个日期

    python - Python 中 R 的 "phyper"函数有哪些等价物?

    iphone - 无需 AppStore 即可向客户分发免费的 iPhone 应用程序和硬件