 function [rie]= generate_population_gss

% function [rie]= generate_population
%
% Reads in the GSS 2002 population to be used in Internet adoption 
% simulations. 
%
% OUTPUT:   rie  - Node #, Race, Income, Education, Income Group ID,
% Education Group ID Matrix, Number of Contacts, Number of Friends to
% discuss personal or other problems.(Dimensions: i x j where i = pop size, j = 8) 

% Paul DiMaggio and Filiz Garip (c)
% Department of Sociology
% Princeton University, Harvard University
% Created: May 27, 2008
% File Name: generate_population_gss.m

% INPUT DATA
% Data comes from GSS 2002 data containing 'numprobs' and 'numcntct'
% variables. (numprobs = those with whom you discuss personal or important
% problems; numcntct = number of people in contact with at least once a
% year; numprobs is a subset of numcntct.)
% Columns - race, years of education, education degree, family income,
% numprobs, numcntct, income category.

clear;
load gss02;

% Income is recorded categorically in GSS, uniformly distribute individuals
% within their income interval to get a smooth income distribution.

inccat = data(:,7);
fincome = zeros(size(data,1),1);

index = [0,1,3,4,5,6,7,8,10,12.5,15,17.5,20,22.5,25,30,35,40,50,60,75,90,110]';
index = index*1000;

nocat = max(inccat);

for i=1:nocat-1
     [I] = find(inccat==i);
     fincome(I) = rand(size(I,1),1).*(index(i+1)-index(i))+ index(i);       
end


% Use Census 2007 distribution for those with income>100K. 

[I] = find(inccat==nocat);
csum = size(I,1);
c1 = round(0.60*csum);
c2 = round(0.22*csum);
c3 = round(0.08*csum);
c4 = csum-c1-c2-c3;

fincome(I(1:c1))            = rand(c1,1).*(40000)+ 110000;       
fincome(I(c1+1:c1+c2))      = rand(c2,1).*(50000)+ 150000;       
fincome(I(c1+c2+1:c1+c2+c3))= rand(c3,1).*(50000)+ 200000;       
fincome(I(c1+c2+c3+1:csum)) = rand(c4,1).*(400000)+ 250000;       
 

race    = data(:,1);
educ    = data(:,2);
degree  = data(:,3);
numprobs = data(:,5);
numcntct = data(:,6);

% Income Cutpoints (33rd and 67th percentile cutpoint)
hi     = 55000;         % High income cutpoint for family income 
mi     = 25000;         % Medium income cutpoint for family income

% Education Cutpoints (degree: 0 - less than high school, 1 - high school,
% 2 - Any college w/o BA, 3 - BA, 4 - Graduate School)

degree(degree==4) = 3;
ps = size(race,1);
rie = zeros(ps,6);
rie(:,1) = [1:ps]';
rie(:,2) = race;
rie(:,3) = fincome;
rie(:,4) = educ;

% GENERATE SUBGROUPS BY INCOME AND EDUCATION

% Income Groups     - (1) Low (<25K) - (2) Med (25-55K) - (3) High (>55K)
% Education Groups  - (0) Less than HS - (1) HS - (2) Any college, no BA - (3) BA or more

[I]       = find(rie(:,3)<mi);                    % Index low income
rie(I,5)  = 1;                                    % Set group id

[I]       = find(rie(:,3)>=mi & rie(:,3)<hi);     % Index medium income  
rie(I,5)  = 2;                                    % Set group id

[I]       = find(rie(:,3)>=hi);                   % Index high income-low educ
rie(I,5)  = 3;                                    % Set group id

rie(:,6) = degree;
rie(:,7) = numcntct;
rie(:,8) = numprobs;
