
close all
clear all
disp('Goodness of Fit: Anacapa')
 lw = 2.5; 
 set(0, 'DefaultAxesFontSize', 16);
 fs = 15;
 msize = 10;
%   PCB in Yolks of Pelican Eggs.
% A well known data set for testing agreement with normal distribution comes from
% Risebrough (1972) who explored concentrations of polychlorinated biphenyl (PCB)
% in yolk lipids of pelican eggs. For  n=65   pelicans from Anacapa Island 
% (north-west of LA), the concentrations of PCB were
%   
anacapa =[452   184   115   315   139   177  214   356   166   246  ...
    177   289   175   296   205   324   260   188   208   109   204 ...
     89   320   256   138   198   191   193   316   122   305   203 ...
    396   250   230   214    46   256   204   150   218   261   143 ...
    229   173   132   175   236   220   212   119   144   147   171 ...
    216   232   216   164   185   216   199   236   237   206   87];

hist((anacapa), 20)

prctile(anacapa, [10 20 30 40 50 60 70 80 90])

 %122.0000  148.5000  175.0000  192.0000  205.0000  216.0000
 % 232.0000  256.0000  315.0000

  
prcs = prctile(anacapa, [12.5 25 37.5 50 62.5 75 87.5 ])  
 
%135.7500  169.7500  187.6250  205.0000  216.2500  239.2500  299.3750

%[-inf]  46    87    89   109   115   119   122   132   138   [138.5]     
%       139   143   144   147   150   164   166   171   173   [174]
%       175   175   177   177   184   185   188   191   193   [195.5]
%       198   199   203   204   204   205   206   208   212   [213]
%       214   214   216   216   216   218   220   229   230   [231] 
%       232   236   236   237   246   250   256   256   260   [260.5]
%       261   289   296   305   315   316   320   324   356   
%       396   452 [inf]

  
ni = [9 9 9 9 9 9 11]
ei = 65 * diff(normcdf([-1000 138.5 174 195.5 213 231 260.5 2000], ...
        mean(anacapa), std(anacapa) ))
              
chi2 = sum(    (ni - ei).^2 ./ ei )
1-chi2cdf(chi2, 7-1-2)
% ei = 10.6016  9.5833 7.1860 6.1970 6.3072 9.2636  15.8613
% chi2 =  4.6503
% pval =  0.3251
% p = 0.3251
                         
[h,p,stats] = chi2gof(anacapa,'cdf',...
   @(z)normcdf(z,mean(anacapa),std(anacapa)),...
   'edges',[0 138.5 174 195.5 213 231 260.5 452 1000],'nparams',2)

   stats.E
   
   
% stats =  
%     chi2stat: 4.6503
%           df: 4
%        edges: [0 138.5000 174 195.5000 213 231 260.5000 1000]
%            O: [9 9 9 9 9 9 11]
%            E: [1x7 double]
% 
% ans = 10.6016    9.5833    7.1860    6.1970    6.3072    9.2636    15.8613
[h,p,stats] = chi2gof(anacapa)
   
   
   