简体   繁体   中英

QQ Plot for Poisson Distribution in Python

I've been trying to make a QQ plot in python for a poisson distribution. Here is what I have so far:

import numpy as np
import statsmodels.api as sm
import scipy.stats as stats
pois = np.random.poisson(2.5, 100)  #creates random Poisson distribution with mean = 2.5
fig =sm.qqplot(pois, stats.poisson, line = 's')
plt.show()

Whenever I do this, I get "AttributeError: 'poisson_gen' object has no attribute 'fit'"

When googling that error, I found a lot of people saying that there is no Poisson.fit available. I'm pretty sure that the qqplot function is calling Poisson.fit. Does this mean that the qqplot function will not work with the Poisson distribution? If the qqplot function does not work with Poisson distributions, how would you recommend generating this plot? Any recommendations would be appreciated.

I had the same error. The following seemed to work for me:

import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
data=np.random.poisson(2.5, 100)
stats.probplot(data, dist='poisson', sparams=(2.5,), plot=plt)
plt.show()

It is the end of 2022, and this is still a thing. I noticed that the statsmodels qqplots can accept frozen scipy distributions, which are not fit and thus do not throw the error for discrete distributions.

from scipy import stats
import statsmodels.api as sm
import numpy as np
import matplotlib.pyplot as plt

mu = 10
test_array = stats.poisson.rvs(mu=mu, size=10000)
fig, ax = plt.subplots(figsize=(7, 5))
ax.set_title("Poisson vs Poisson Example Q-Q Plot", fontsize=14)
test_mu = np.mean(test_array)
qdist = stats.poisson(test_mu)
sm.qqplot(test_array, dist=qdist, line="45", ax=ax)

fig.set_tight_layout(True)
plt.savefig('poisson_qq_ex.png')
plt.close()

Example QQ plot using StatsModels with discrete Poisson distribution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM