 % Upton Fingleton Square
clear all
close all
disp('Poisson Regression:Upton-Fingleton Square')

ufs = [0 0 0 1 0; 3 3 5 2 0; 2 3 6 2 5; 1 5 4 6 7; 6 2 4 3 4];

uf = [0 0 0 1 0      3 3 5 2 0    2 3 6 2 5    1 5 4 6 7     6 2 4 3 4];
x =  [1 2 3 4 5      1 2 3 4 5    1 2 3 4 5    1 2 3 4 5     1 2 3 4 5 ];
y =  [1 1 1 1 1      2 2 2 2 2    3 3 3 3 3    4 4 4 4 4     5 5 5 5 5 ];
   
[b1, dev1, stats1] = glmfit( zeros(size(uf)),uf, 'poisson');
[b2, dev2, stats2] = glmfit(x', uf', 'poisson');
[b3, dev3, stats3] = glmfit(y', uf', 'poisson');
[b4, dev4, stats4] = glmfit([x' y'], uf', 'poisson');

dev1
dev2
dev3
dev4
stats1.dfe
stats2.dfe
stats3.dfe
stats4.dfe

resids = reshape(stats4.residd,5,5)
figure(1)
subplot(1,2,1)
  imagesc(ufs), colormap gray, colorbar
  subplot(1,2,2)
imagesc(resids), colormap gray, colorbar
ts=stats4.beta./diag(stats4.covb);
[ts tcdf(-abs(ts), stats4.dfe)]
[stats4.beta - tinv(0.975, stats4.dfe)*diag(stats4.covb)  ...
stats4.beta + tinv(0.975, stats4.dfe)*diag(stats4.covb)]

