%Weldon
close all
clear all


 disp('Goodness of Fit: Weldon Data')
 lw = 2.5; 
 set(0, 'DefaultAxesFontSize', 16);
 fs = 15;
 msize = 10;




obsfreq = [185  1149   3265   5475   6114  5194 ...
                         3067  1331   403   105      14   4   0 ]
 n = sum(obsfreq)                    
expected = n * binopdf(0:12, 12, 1/3)
ing = (obsfreq - expected).^2./expected

chi2 = sum(ing)
pval=1 - chi2cdf(chi2, 13-1)

crit = chi2inv(0.95, 13-1)

%merging cells with counts < 5.
obsfreq1 = [185  1149   3265   5475   6114  5194 ...
                         3067  1331   403   105    18 ]
 n1 = sum(obsfreq1)                    
expected1 = n1 * binopdf(0:9, 12, 1/3); expected1(11) = n1 - sum(expected(1:10));
ing1=(obsfreq1 - expected1).^2./expected1

chi21 = sum(ing1)

pval=1 - chi2cdf(chi21, 11-1)




figure(1)
numdice = (0:12)';
bar(numdice', obsfreq','r')
hold on
plot(numdice', expected','o', ...
        'MarkerSize', 5,...
        'MarkerFaceColor','k')
    axis tight
        xlabel('Number of 5s, 6s out of 12 rolls')
        ylabel('Frequency')
    % print -depsc 'C:\Springer\Goodness\Goodnesseps\weldon0.eps'
 

    figure(2)
    bar([obsfreq' expected'], 2.0) %'group', 1.5)
    colormap summer
    legend('Observed Frequencies','Theoretical Frequencies')
    xlabel('Number of 5s, 6s out of 12 rolls')
    ylabel('Frequency')
   % print -depsc 'C:\Springer\Goodness\Goodnesseps\weldon.eps'
    
    
    
    [h,p,st] = chi2gof(0:12,'ctrs',0:12,...
                        'frequency',obsfreq, ...
                        'expected',expected)
                    
                    
%            1.0279e-004                
%            chi2stat: 35.4943
%            df: 10
%            edges: [1x12 double]
%            O: [185 1149 3265 5475 6114 5194 3067 1331 403 105 18]
%            E: [1x11 double]

    