Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Data documentation at the end.
- clear all
- v8_assumption = 100;
- v9_assumption = 5;
- age_range = [18 30];
- years_v = [1991, 1993, 1994, 1996, 1998, 2000, 2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018];
- GSS = importdata('GSS.dat');
- GSS = GSS(GSS(:,3) >= age_range(1) & GSS(:,3) <= age_range(2) ,:); % only people in the desired age range
- GSS = GSS(:,[1 4 5 6]); % keep only year(1), sex(2), partners last year(3) and partners last 5 years(4) as the variables
- GSS = GSS(GSS(:,3)~=-1 & GSS(:,3)~=98 & GSS(:,3)~=99 & GSS(:,3)~=95,:); % remove bad answers (last 1 years)
- GSS = GSS(GSS(:,4)~=-1 & GSS(:,4)~=98 & GSS(:,4)~=99 & GSS(:,3)~=95,:); % remove bad answers (last 5 years)
- m1_data = []; m5_data = [];
- f1_data = []; f5_data = [];
- m1_gini = []; m5_gini = [];
- f1_gini = []; f5_gini = [];
- for y = years_v
- G_y = GSS(GSS(:,1) == y ,:); % choose year
- G_m = G_y(G_y(:,2) == 1 ,:); G_f = G_y(G_y(:,2) == 2 ,:); % choose sex
- G_m1 = G_m(:,3); G_m5 = G_m(:,4);
- G_f1 = G_f(:,3); G_f5 = G_f(:,4); % data for male/female 1/5 year partners.
- % This is [year, total respondents, v0-v9 as given above]
- m1_data = [m1_data; y, length(G_m1), grab_vars(G_m1)]; m5_data = [m5_data; y, length(G_m5) grab_vars(G_m5)];
- f1_data = [f1_data; y, length(G_f1), grab_vars(G_f1)]; f5_data = [f5_data; y, length(G_f5) grab_vars(G_f5)];
- % Convert to a population of people and the number of times they've had
- % sex, then calculate a gini coefficient based off that.
- m1_gini = [m1_gini; y, gini_coefficient(convert_to_population(y,m1_data,v8_assumption,v9_assumption))];
- m5_gini = [m5_gini; y, gini_coefficient(convert_to_population(y,m5_data,v8_assumption,v9_assumption))];
- f1_gini = [f1_gini; y, gini_coefficient(convert_to_population(y,f1_data,v8_assumption,v9_assumption))];
- f5_gini = [f5_gini; y, gini_coefficient(convert_to_population(y,f5_data,v8_assumption,v9_assumption))];
- end
- figure(1)
- hold on
- plot(m5_data(:,1) , 100 * m5_data(:,3)./m5_data(:,2) ,'color',[0.47 0.81 0.94],'LineWidth',4)
- plot(f5_data(:,1) , 100 * f5_data(:,3)./f5_data(:,2) ,'color',[0.96 0.76 0.76],'LineWidth',4)
- set(gca,'FontSize', 15)
- title('Share of 18-30 year olds sexless in the last 5 years')
- legend('Males', 'Females','FontSize',20)
- figure (2)
- hold on
- plot(m1_data(:,1) , 100 * m1_data(:,3)./m1_data(:,2) ,'color',[0.47 0.81 0.94],'LineWidth',4)
- plot(f1_data(:,1) , 100 * f1_data(:,3)./f1_data(:,2) ,'color',[0.96 0.76 0.76],'LineWidth',4)
- set(gca,'FontSize', 15)
- title('Share of 18-30 year olds sexless in the last 1 year')
- legend('Males', 'Females','FontSize',20)
- figure(3)
- hold on
- plot(m1_gini(:,1) , m1_gini(:,2) ,'color',[0.47 0.81 0.94],'LineWidth',3)
- plot(f1_gini(:,1) , f1_gini(:,2),'color',[0.96 0.76 0.76],'LineWidth',3)
- set(gca,'FontSize', 15)
- title('Gini Coefficient facing males and females 18-30 in the last 1 year of sex','FontSize',13)
- legend('Males', 'Females','FontSize',20)
- figure(4)
- hold on
- plot(m5_gini(:,1) , m5_gini(:,2) ,'color',[0.47 0.81 0.94],'LineWidth',3)
- plot(f5_gini(:,1) , f5_gini(:,2),'color',[0.96 0.76 0.76],'LineWidth',3)
- set(gca,'FontSize', 15)
- title('Gini Coefficient facing males and females 18-30 in the last 5 years of sex','FontSize',13)
- legend('Males', 'Females','FontSize',20)
- function v_args = grab_vars(G)
- v_args = [];
- for i=0:9
- v_args = [v_args length(G(G==i))];
- end
- end
- function vec = convert_to_population(year,data,v8_assumption,v9_assumption)
- n = [0 1 2 3 4 sqrt(5*10) sqrt(11*20) sqrt(21*100) v8_assumption v9_assumption];
- x = data(data(:,1)==year ,:);
- vec = [];
- for i = 0:9
- vec = [vec, zeros(1,x(i+3))+n(i+1)];
- end
- end
- function g = gini_coefficient(population)
- s = 0;
- for i = 1:length(population)
- for j = 1:length(population)
- s = s + abs(population(i) - population(j));
- end
- end
- g = s/(2 * length(population).^2 * mean(population));
- end
- %{
- 1 label variable year "Gss year for this respondent ";
- 2 label variable id_ "Respondent id number";
- 3 label variable age "Age of respondent";
- 4 label variable sex "Respondents sex";
- 5 label variable partners "How many sex partners r had in last year";
- 6 label variable partnrs5 "How many sex partners r had in last 5 years";
- 7 label variable ballot "Ballot used for interview";
- field 1: YEAR
- field 2: SEX
- 2 "Female"
- 1 "Male"
- field 3: AGE
- 99 "No answer"
- 98 "Don't know"
- 89 "89 or older"
- field 3: SEX PARTNERS IN LAST 1 YEAR
- 99 "No answer"
- 98 "Don't know"
- 95 "Several" <-- IGNORE, NO SUCH RESPONSE
- 9 "1 or more, # unknown"
- 8 "More than 100 partners"
- 7 "21-100 partners"
- 6 "11-20 partners"
- 5 "5-10 partners"
- 4 "4 partners"
- 3 "3 partners"
- 2 "2 partners"
- 1 "1 partner"
- 0 "No partners"
- -1 "Not applicable"
- field 4: SEX PARTNERS IN LAST 5 YEARS
- same as above.
- %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement