gpt4 book ai didi

r - 使用 ggplot2 绘制 fitdist 图

转载 作者:行者123 更新时间:2023-12-04 04:34:38 24 4
gpt4 key购买 nike

我用 fitdist 拟合了正态分布函数来自 fitdistrplus包裹。使用 denscomp , qqcomp , cdfcompppcomp我们可以绘制 histogram against fitted density functions , theoretical quantiles against empirical ones , the empirical cumulative distribution against fitted distribution functions , 和 theoretical probabilities against empirical ones分别如下。

set.seed(12345)
df <- rnorm(n=10, mean = 0, sd =1)
library(fitdistrplus)
fm1 <-fitdist(data = df, distr = "norm")
summary(fm1)

denscomp(ft = fm1, legendtext = "Normal")

enter image description here
qqcomp(ft = fm1, legendtext = "Normal")

enter image description here
cdfcomp(ft = fm1, legendtext = "Normal")

enter image description here
ppcomp(ft = fm1, legendtext = "Normal")

enter image description here

我非常有兴趣制作这些 fitdistggplot2 一起绘制. MWE如下:
qplot(df, geom = 'blank') +
geom_line(aes(y = ..density.., colour = 'Empirical'), stat = 'density') +
geom_histogram(aes(y = ..density..), fill = 'gray90', colour = 'gray40') +
geom_line(stat = 'function', fun = dnorm,
args = as.list(fm1$estimate), aes(colour = 'Normal')) +
scale_colour_manual(name = 'Density', values = c('red', 'blue'))

enter image description here
ggplot(data=df, aes(sample = df)) + stat_qq(dist = "norm", dparam = fm1$estimate)

我怎样才能开始制作这些 fitdistggplot2 一起绘制?

最佳答案

你可以使用类似的东西:

library(ggplot2)

ggplot(dataset, aes(x=variable)) +
geom_histogram(aes(y=..density..),binwidth=.5, colour="black", fill="white") +
stat_function(fun=dnorm, args=list(mean=mean(z), sd=sd(z)), aes(colour =
"gaussian", linetype = "gaussian")) +
stat_function(fun=dfun, aes(colour = "laplace", linetype = "laplace")) +
scale_colour_manual('',values=c("gaussian"="red", "laplace"="blue"))+
scale_linetype_manual('',values=c("gaussian"=1,"laplace"=1))

你只需要定义 dfun在运行图形之前。在这个例子中,它是一个拉普拉斯分布,但你可以选择任何你想要的并添加更多 stat_function如果你想。

关于r - 使用 ggplot2 绘制 fitdist 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31653112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com