Cara menggunakan q-q plot python

Secara harfiah, statistik parametrik adalah metode atau teknik yang bertujuan untuk menguji asumsi mengenai parameter pada kumpulan data dari distribusi populasi data tersebut. Kebalikan dari statistik parametrik adalah statistik non parametrik. Pada umumnya, uji statistik parametrik mengacu pada tes yang mengasumsikan suatu populasi berdistribusi normal. Selain itu, jika kita menggunakan metode statistik parametrik berarti kita mengasumsikan bahwa data berasal dari skala interval yang sama.

Data berdistribusi normal ditandai dengan kurva distribusi yang membentuk lonceng sempurna dimana kurva tersebut dibagi menjadi dua bagian yang sama besar dan titik tengahnya merupakan ukuran central tendency berupa mean atau rata-rata. Metode statistik parametrik tidak bisa digunakan secara sembarangan karena ada asumsi-asumsi pada data yang harus terpenuhi. Tujuan dari pemenuhan asumsi ini adalah sebagai syarat bahwa data bisa diolah menggunakan suatu metode statistik parametrik sehingga hasilnya lebih optimal. Pertanyaannya, apa saja asumsi-asumsi tersebut? Pada artikel kali ini kita akan membahasnya. Jadi tunggu apa lagi? Let"s get started!

1. Asumsi Normalitas

Statistik parametrik mengasumsikan bahwa setiap kelompok berdistribusi normal. Jika ukuran kumpulan data terlalu kecil (kurang dari 30) maka kita dapat menggunakan uji Shapiro-Wilk untuk menentukan apakah data berdistribusi normal atau tidak. Jika nilai p-value kurang dari tingkat signifikansi tertentu, maka data tersebut kemungkinan tidak berdistribusi normal. Namun, jika ukuran data cukup besar akan lebih baik jika menggunakan Q-Q plot untuk memeriksa distribusi normal data secara visual. Jika titik-titik pada plot berada di sekitar garis diagonal pada Q-Q plot, maka kemungkinan besar dataset mengikuti distribusi normal.

Baca juga : Pengolahan Data Statistik Parametrik dan Non-Parametrik

2. Varian Sama

Statistik parametrik mengasumsikan bahwa varian setiap kumpulan data adalah sama. Secara visual, kita dapat memeriksa apakah asumsi ini terpenuhi atau tidak dengan membuat boxplot berdampingan untuk setiap kelompok data. Boxplot-boxplot yang berjejer ini bisa digunakan untuk mengidentifikasi apakah ukuran setiap kumpulan kelompok sama atau tidak. Cara lain untuk memeriksa asumsi ini adalah jika selisih rasio varians terbesar dan terkecil kurang dari 4, maka kita dapat mengasumsikan bahwa varian kumpulan data tersebut sama.

3. Independensi

Statistik parametrik mengasumsikan bahwa pengamatan di setiap kelompok tidak tergantung pada pengamatan kelompok lainnya. Cara mudah untuk memeriksa asumsi ini adalah dengan memverifikasi bahwa data yang dikumpulkan harus menggunakan metode probability sampling. Probability sampling adalah sebuah metode pengambilan sampel dimana setiap anggota dalam suatu populasi memiliki peluang yang sama untuk terpilih. Beberapa metode yang biasa digunakan adalah simple random sampling, stratified random sampling, cluster random sampling, dan systematic random sampling.

4. Outlier

Statistik parametrik mengasumsikan bahwa tidak ada outlier ekstrim dalam kumpulan data yang dapat mempengaruhi hasil tes. Salah satu cara untuk memeriksa outlier atau pencilan secara visual adalah dengan membuat boxplot untuk setiap kumpulan data dan melihat apakah ada outlier yang jelas jauh dari kumpulan data lainnya. Selain secara visual, ada juga cara lain untuk memeriksa outlier yaitu menggunakan Grubbs" test.

Baca juga : Yuk Pelajari Macam-Macam Metode Analisis Statistika

5. Belajar Metode Analisis Data Beserta Tools-nya Bersama DQLab

Saat ini produksi data meningkat sangat pesat bahkan dalam satu hari ratusan ribu byte data diproduksi dan ratusan byte data digunakan di berbagai bidang industri. Data yang digunakan merupakan data berukuran besar yang tidak bisa diolah menggunakan tools sembarangan. Salah satu tools yang banyak digunakan untuk mengolah big data adalah bahasa pemrograman R dan Python. Bahasa pemrograman multifungsi ini tidak hanya digunakan untuk mengolah data, tetapi juga bisa digunakan untuk tujuan lain seperti apps development, web development, bahkan membuat robot. Yuk belajar R dan Python bersama DQLab! Klik button di bawah ini untuk mengakses berbagai modul dari DQLab dan nikmati belajar mengolah data menggunakan tipe data yang banyak digunakan di dunia industri. 

Engineers and scientists work with data. Without data, they are not able to draw any conclusion. Now is the era of creation of data everyday from every aspects of our lives. Some data are random and some are biased. Some may suffer from bias because of the data collection process. One very important aspect of data is the distribution profile. The collected data may have normal distribution or may be far from normal. It may also be skewed one either side or may follow multimodal pattern. It may be discrete or may be continuous. For continuous data, normal distribution brings a whole lot of advantages compared to its counterparts. Various inferential statistical process assume that the distribution is normal. A bell-shaped curve is easy to describe with mean and standard deviation.

Typical normally distributed data (Image by Author)

Why Q-Q plot?

Since normal distribution is of so much importance, we need to check if the collected data is normal or not. Here, we will demonstrate the Q-Q plot to check the normality of skewness of data. Q stands for quantile and therefore, Q-Q plot represents quantile-quantile plot. To determine the normality, there are also several statistical tests out there such as the Kolmogorov–Smirnov test and the Shapiro–Wilk test. The scope of this article is only Q-Q plot.

Brief explanation

We now know Q-Q plot is quantile-quantile plot but what is quantile at the first place? When the whole data is sorted, 50th quantile means 50% of the data falls below that point and 50% of the data falls above that point. That is the median point. When we say 1st quantile, only 1% of the data falls below that point and 99% is above that. 25th and 75th quantile points are also known as quartiles. There are three quartiles is the dataset.

Q1 = first quartile = 25th quantile

Q2 = second quartile = 50th quantile = median

Q3 = third quartile = 75th quantile

Quantile are sometimes called percentile. A typical Q-Q plot is sown below. Let’s explain this plot which seems pretty much a straight line.

Q-Q plot (Image by Author)

Axes

The x-axis of a Q-Q plot represents the quantiles of standard normal distribution. Let’s say we have a normal data and we want to standardize it. Standardizing means subtracting mean from each data point and dividing it by standard deviation. The resultant is also known as z-score. Let’s sort those z-scores and plot again. The plot below shows that the x-axis is now centered at 0 and extended up to 3 standard deviations on each side. Statistically, 99.7& of the data falls between this range.

Distribution of standardized normal data (Image by Author)

The first step to find the x-axis values of Q-Q plot is to determine the quantiles/percentiles of this normally distributed standard data. This way we can obtain the quantiles which are pretty much standard across all Q-Q plots. When we use these z-scores, the x-axis will roughly stretch from -3 to +3.

Once we obtain the values to plot along x-axis, we then need to apply the same method for our data of interest. Therefore, we will plot the z-scores of out data against the z-scores of the standard normal data. If our data is normal, the plot will be a straight line since we are plotting very close values against each other. If the data is not normally distributed, the line will deviate from the straight track and signal different scenarios.

Skewed data on right side (Image by Author)

For example, if the data is skewed on high side (as shown above), we will obtain a Q-Q plot similar to the one plotted below.

Q-Q plot of skewed data (Image by Author)

If the data exhibits bimodality, the Q-Q plot will signal that too.

Bimodal data (Image by Author)

Q-Q plot of bimodal data (Image by Author)

A tutorial link is here for this article.

Implementation in python

Python has stastsmodels library which has handy qqplot module to use.

Statsmodels Q-Q plot of normal data (Image by Author)

I will also show the breakdown of the code blocks to simply implement it for better understanding. Once we have the data in a dataframe, we need to standardize it.

Next, we need to find the quantiles/percentiles.

Finally we need to sort the output and plot.

That’s it. We can have a quick look at the normality of the data using this Q-Q plot.

Conclusion

Q-Q plot is an extremely useful tool to determine the normality of the data or how much the data is deviated from normality. This plot represents the z-scores of standard normal distribution along x-axis and corresponding z-scores of the obtained data. Sometimes instead of z-score, the sample quantiles can also be plotted along y-axis.