(A very silly question) From the chi2gof documentation I see that chi2gof takes 1 argument
h = chi2gof(x)
Since the chi-square test is just a comparison between observed (O) and expected (E) values, throught the formula (\sum_i (O_i - E_i)^2 / E_i), I guess that the argument "x" would refer to the observed data (O), but.... where should I insert/add the expected (E) values there?
Can you show any example of comparison among observed (O) and expected (E) data, like when I would need to compare two distributions?
(for example the expected (E) values might refer to any theoretical distribution that I want /need to compare to my observed (O) data)

 Respuesta aceptada

Aman
Aman el 21 de Jun. de 2023
Editada: Aman el 21 de Jun. de 2023

2 votos

Hi Sim,
You can add expected value as a parameter as given in chi2gof documentation.
Here is an example for the same,
observed_data = exprnd(2, 100, 1);
num_bins = 10;
bin_edges = linspace(min(observed_data), max(observed_data), num_bins+1);
expected_counts = numel(observed_data) * diff(chi2cdf(bin_edges, 1));
[h, p] = chi2gof(observed_data, 'Expected', expected_counts)
You can add other parameter according to your needs. For further help, check the documentation.
Hope this helps!

3 comentarios

Sim
Sim el 21 de Jun. de 2023
Editada: Sim el 21 de Jun. de 2023
Thanks a lot@Aman! Just a question.... what if my data come from a fitting distribution?? How to derive the "expected_counts" from the fitting distribution?
observed_data = exprnd(2, 100, 1);
xgrid = linspace(0,100,1000)';
pd = fitdist(observed_data,'Exponential'); % <-- fitting distribution
hold on
line(xgrid,pdf(pd,xgrid),'Linewidth',2,'color','b')
histogram(observed_data,100,'Normalization','pdf','facecolor','blue')
hold off
% how to get the "expected_counts" from "pd =
% fitdist(observed_data,'Exponential')"??
% [h, p] = chi2gof(observed_data, 'Expected', expected_counts)
Aman
Aman el 21 de Jun. de 2023
We can use cdf, like this:
expected_counts = numel(observed_data) * diff(cdf(pd,bin_edges));
Sim
Sim el 21 de Jun. de 2023
thanks a lot @Aman!!

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

Sim
el 21 de Jun. de 2023

Comentada:

Sim
el 21 de Jun. de 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by