如何正确使用fitdist拟合广义正态分布?解决R代码报错问题
解决广义正态分布拟合的R代码错误
错误原因
报错核心问题有两点:
start列表里的参数名和gnorm分布函数的参数不匹配,gnorm(依赖sn包)的参数为location(对应均值)、scale(对应标准差)、shape,而非你使用的mean和sd。- 形状参数
shape初始值不能设为0,因为当shape=0时,广义正态分布会退化为标准正态分布,引发拟合逻辑冲突。
修正后的代码
# 加载依赖包 library(fitdistrplus) library(sn) # gnorm分布的定义来自sn包 data(mtcars) head(mtcars) # 修正参数名与初始值 fit <- fitdist(mtcars$mpg, "gnorm", start=list(location=mean(mtcars$mpg), scale=sd(mtcars$mpg), shape=1)) # 用非0值作为shape初始值 # 查看拟合结果 summary(fit) plot(fit)
说明
- 必须加载
sn包,fitdistrplus调用的gnorm相关分布函数(dgnorm、pgnorm等)都来自该包。 - 把
mean替换为location、sd替换为scale,严格匹配gnorm的参数命名规则。 - 形状参数
shape初始值设为1(也可根据数据分布尝试0.5、2等其他非0值),确保拟合流程正常执行。
内容的提问来源于stack exchange,提问作者Data Science study
相关产品推荐
相关产品推荐

