我正在使用以下代码来拟合正态分布。 “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))
绘制直方图后,似乎正态分布应该很好地拟合。
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)
使用
fitdistrplus::fitdist
library(fitdistrplus)
FIT <- fitdist(x, "norm") ## note: it is "norm" not "normal"
class(FIT)
# [1] "fitdist"
plot(FIT) ## use method `plot.fitdist`
关于r - 在 R 中拟合正态分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39961964/