Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quantile-Quantile Plot using python statsmodels api

I am trying to see whether a normal distribution with specific parameters fits to a data set. However it seems qqplot does not work as it is expected to. The following small example shows this:

import numpy as np
import statsmodels.api as sm
import pylab

test = np.random.normal(20,5, 1000)

sm.qqplot(test, loc = 20, scale = 5 ,  line='45')
pylab.show()

As one can see I expect the points to be around the line with slope = 1 but it gives the following figure:

q-q plot

Can anyone explain me why this happens?

like image 380
user2107109 Avatar asked Aug 01 '26 16:08

user2107109


1 Answers

You can use line = '45' and it will work well if you have z-normalized data, meaning your distribution will have mean = 0 and sd = 1. In other cases you have several options, e.g. line = 's' or line = 'q' in case you want to see a fit against standardized line (the expected order statistics are scaled by the standard deviation of the given sample and have the mean added to them) or against line fit through the quartiles, which in my opinion is the one really meaning full and let's observe well the deviation of your data distribution from the normal one. Also, you can use line = 'r' for to see the fit to regression line. By default line is set to "None"

simply use code like this

import numpy as np
import statsmodels.api as sm
import pylab

test = np.random.normal(20, 5, 1000)

sm.qqplot(test, line='q')
pylab.show()
like image 75
DariaS Avatar answered Aug 03 '26 07:08

DariaS