11

fitdist我用包中的函数拟合了正态分布fitdistrplus。使用denscomp, qqcomp,我们可以分别绘制, , , 和cdfcomp,如下所示。ppcomphistogram against fitted density functionstheoretical quantiles against empirical onesthe empirical cumulative distribution against fitted distribution functionstheoretical 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")

在此处输入图像描述

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

在此处输入图像描述

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

在此处输入图像描述

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

在此处输入图像描述

我对制作这些fitdist情节非常感兴趣ggplot2。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'))

在此处输入图像描述

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

我怎样才能开始制作这些fitdist情节ggplot2

4

1 回答 1

4

你可以使用类似的东西:

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

于 2015-07-30T20:07:56.117 回答