Wednesday, March 9, 2011

Ecological Detective (Chapter 6)

Alright, here is the Matlab code for chapter 6 of the Ecological Detective.

% Here is the data from table 6.1, page 120 (Ecological Detective)

M=[0 0 0 0 0; 0 0 0 0 0; 0 0 0 0 0; 4 0 2 1 0; 5 0 5 1 0; 6 1 11 3 0; 7 1 5 1 0; 8 0 2 1 0; 9 0 1 0 1; 10 0 4 3 0; 11 0 3 4 0; 12 0 1 6 0; 13 1 2 4 0;14 0 0 3 1; 15 0 3 4 0; 16 0 2 6 0; 17 0 2 4 0; 18 0 0 2 0; 19 0 0 6 0; 20 0 0 2 0; 21 0 1 1 0; 22 0 0 0 0;23 0 0 1 0]


% Pseudocode 6.1

% Fixed clutch, c1

SSQ1=0;
for i=4:23
for j=1:4
SSQ1=SSQ1+(j-1)^2*M(i,j+1);
end
end


% Fixed clutch, c2

SSQ2=0;
for i=4:23
for j=1:4
SSQ2=SSQ2+(j-2)^2*M(i,j+1);
end
end

% Fixed clutch, c3

SSQ3=0;
for i=4:23
for j=1:4
SSQ3=SSQ3+(j-3)^2*M(i,j+1);
end
end

% Fixed clutch, c4

SSQ4=0;
for i=4:23
for j=1:4
SSQ4=SSQ4+(j-4)^2*M(i,j+1);
end
end


% Now to get the table (pg 124) results we scale the above values by Nc=102 (total number of observations)

Nc=102
Result=[1 SSQ1/Nc;2 SSQ2/Nc; 3 SSQ3/Nc; 4 SSQ4/Nc]






% Pseudocode 6.2

% first we get the place holders ready

SSQ=10^6;
SSQss=0;
parameters=[0 0 0];

% First we loop over all possible combinations of c1, c2, e1
% Then we generate the SSQ (sum of squares).
% Last ish we hold the smallest

for c1=1:4
for c2=1:4
for e1=4:23


for i=4:23
for j=1:4
if i>e1
SSQss=SSQss+(j-c2)^2*M(i,j+1);
else
SSQss=SSQss+(j-c1)^2*M(i,j+1);
end
end
end

if SSQssparameters=[c1 c2 e1];
SSQ=SSQss;
end

SSQss=0;

end
end
end




% Pseudocode 6.3

Just look at the statistical package in Matlab, there is a bootstrap command AND a sample with replacement command.

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...