我需要做的是@interstellar在这里问Fit poisson distribution to data,但在R环境中(而不是matlab)。
所以,我用我的观察值创建了一个barplot,我只需要在它上面拟合一个泊松分布。
在这里我的数据:
df = read.table(text = 'Var1 Freq
6 1
7 2
8 5
9 7
10 9
11 6
12 4
13 3
14 2
15 1', header = TRUE)创建的barplot如下所示:
t = barplot(df$Freq, ylim = c(0,10))
axis(1, at=t, labels=df$Var1)我对R还是个新手,所以我如何使用fitdist函数或其他方法在我的barplot上方创建一条线?
任何帮助将非常感激。
UPDATE
我已经制定了一些东西,但如果它是正确的,我不确定100%:
#create barplot
t = barplot(df$Freq, ylim = c(0,10))
axis(1, at=t, labels=df$Var1)
#find lambda value from my data
pois = fitdist(df$Freq, 'pois', method = 'mle')
print(pois)
#result
Fitting of the distribution ' pois ' by maximum likelihood
Parameters:
estimate Std. Error
lambda 4 0.6324555
#create 10 values from a real poisson distribution
dist = dpois(1:10, lambda = 4)
#multiply them by `sum(df$Freq)` in order to scale them to the barplot
dist = dist * sum(df$Freq)
#add the line plot to the original barplot
lines(dist, lwd = 2)result
但曲线并不平坦..