[英]How to create a Q-Q plot with Poisson as theoretical distribution
我需要创建一个QQ图,以检查观察到的数据是否符合泊松分布。
这是我的data.frame:
df = read.table(text = 'Var1 Freq
1975 10
1976 12
1977 9
1978 14
1979 14
1980 11
1981 8
1982 7
1983 10
1984 8
1985 12
1986 9
1987 10
1988 9
1989 10
1990 9
1991 11
1992 12
1993 9
1994 10', header = TRUE)
df$Freq
列使我感兴趣,因为观察值表示每年的事件计数。
我知道我必须使用qqplot
功能,也是qpois
一个创造理论位数,但如何?
ggplot2
有一个不错的接口可以做到这一点。 这是带有红色协议步骤线的QQ图。 使用stat_qq
并更改distribution
参数来制作QQ图。 您需要在dparams
参数中提供lambda
。
ggplot(data = df,
mapping = aes(sample = Freq)) +
stat_qq(distribution = stats::qpois,
dparams = list(lambda = mean(df$Freq))) +
geom_step(data = data.frame(x = 6:16,
Freq = 6:16),
mapping = aes(x = x,
y = Freq),
colour = "red",
alpha = 0.5)
而且, fitdistrplus
软件包可以用更少的代码来完成此任务。 比较经验密度和理论密度以及CDF。
library('fitdistrplus')
plot(fitdist(df$Freq,"pois"))
您可以获取lambda等,并对照其他发行版进行检查。 不如ggplot
方法灵活,但可用于快速检查。
这是我可能的答案:
#calculate Frequencies
tbl = as.data.frame(table(df$Freq))
#create theoretical poisson distr
dist = dpois(1:7, lambda = mean(tbl$Freq))
dist = dist * 20 #make values in the same scale as tbl$Freq (20 = sum(tbl$Freq))
dist = as.data.frame(dist)
dist$Var1 = tbl$Var1
#qqplot
qqplot(dist$dist, tbl$Freq, xlab = 'Theoretical Quantiles', ylab = 'Empirical Quantiles',
main = 'Q-Q plot Poisson', xlim = c(0,5), ylim = c(0,5))
abline(0,1) #create 45° line
如果您发现任何错误,请告诉我。 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.