Advertisement
Guest User

code.m

a guest
Feb 22nd, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. % Data documentation at the end.
  2.  
  3. clear all
  4. v8_assumption = 100;
  5. v9_assumption = 5;
  6. age_range = [18 30];
  7.  
  8. years_v = [1991, 1993, 1994, 1996, 1998, 2000, 2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018];
  9. GSS = importdata('GSS.dat');
  10. GSS = GSS(GSS(:,3) >= age_range(1) & GSS(:,3) <= age_range(2) ,:); % only people in the desired age range
  11. 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
  12. GSS = GSS(GSS(:,3)~=-1 & GSS(:,3)~=98 & GSS(:,3)~=99 & GSS(:,3)~=95,:); % remove bad answers (last 1 years)
  13. GSS = GSS(GSS(:,4)~=-1 & GSS(:,4)~=98 & GSS(:,4)~=99 & GSS(:,3)~=95,:); % remove bad answers (last 5 years)
  14.  
  15. m1_data = []; m5_data = [];
  16. f1_data = []; f5_data = [];
  17. m1_gini = []; m5_gini = [];
  18. f1_gini = []; f5_gini = [];
  19.  
  20. for y = years_v
  21. G_y = GSS(GSS(:,1) == y ,:); % choose year
  22. G_m = G_y(G_y(:,2) == 1 ,:); G_f = G_y(G_y(:,2) == 2 ,:); % choose sex
  23. G_m1 = G_m(:,3); G_m5 = G_m(:,4);
  24. G_f1 = G_f(:,3); G_f5 = G_f(:,4); % data for male/female 1/5 year partners.
  25.  
  26. % This is [year, total respondents, v0-v9 as given above]
  27. m1_data = [m1_data; y, length(G_m1), grab_vars(G_m1)]; m5_data = [m5_data; y, length(G_m5) grab_vars(G_m5)];
  28. f1_data = [f1_data; y, length(G_f1), grab_vars(G_f1)]; f5_data = [f5_data; y, length(G_f5) grab_vars(G_f5)];
  29.  
  30. % Convert to a population of people and the number of times they've had
  31. % sex, then calculate a gini coefficient based off that.
  32. m1_gini = [m1_gini; y, gini_coefficient(convert_to_population(y,m1_data,v8_assumption,v9_assumption))];
  33. m5_gini = [m5_gini; y, gini_coefficient(convert_to_population(y,m5_data,v8_assumption,v9_assumption))];
  34. f1_gini = [f1_gini; y, gini_coefficient(convert_to_population(y,f1_data,v8_assumption,v9_assumption))];
  35. f5_gini = [f5_gini; y, gini_coefficient(convert_to_population(y,f5_data,v8_assumption,v9_assumption))];
  36. end
  37.  
  38. figure(1)
  39. hold on
  40. plot(m5_data(:,1) , 100 * m5_data(:,3)./m5_data(:,2) ,'color',[0.47 0.81 0.94],'LineWidth',4)
  41. plot(f5_data(:,1) , 100 * f5_data(:,3)./f5_data(:,2) ,'color',[0.96 0.76 0.76],'LineWidth',4)
  42. set(gca,'FontSize', 15)
  43. title('Share of 18-30 year olds sexless in the last 5 years')
  44. legend('Males', 'Females','FontSize',20)
  45.  
  46. figure (2)
  47. hold on
  48. plot(m1_data(:,1) , 100 * m1_data(:,3)./m1_data(:,2) ,'color',[0.47 0.81 0.94],'LineWidth',4)
  49. plot(f1_data(:,1) , 100 * f1_data(:,3)./f1_data(:,2) ,'color',[0.96 0.76 0.76],'LineWidth',4)
  50. set(gca,'FontSize', 15)
  51. title('Share of 18-30 year olds sexless in the last 1 year')
  52. legend('Males', 'Females','FontSize',20)
  53.  
  54. figure(3)
  55. hold on
  56. plot(m1_gini(:,1) , m1_gini(:,2) ,'color',[0.47 0.81 0.94],'LineWidth',3)
  57. plot(f1_gini(:,1) , f1_gini(:,2),'color',[0.96 0.76 0.76],'LineWidth',3)
  58. set(gca,'FontSize', 15)
  59. title('Gini Coefficient facing males and females 18-30 in the last 1 year of sex','FontSize',13)
  60. legend('Males', 'Females','FontSize',20)
  61.  
  62. figure(4)
  63. hold on
  64. plot(m5_gini(:,1) , m5_gini(:,2) ,'color',[0.47 0.81 0.94],'LineWidth',3)
  65. plot(f5_gini(:,1) , f5_gini(:,2),'color',[0.96 0.76 0.76],'LineWidth',3)
  66. set(gca,'FontSize', 15)
  67. title('Gini Coefficient facing males and females 18-30 in the last 5 years of sex','FontSize',13)
  68. legend('Males', 'Females','FontSize',20)
  69.  
  70.  
  71.  
  72. function v_args = grab_vars(G)
  73. v_args = [];
  74. for i=0:9
  75. v_args = [v_args length(G(G==i))];
  76. end
  77. end
  78.  
  79. function vec = convert_to_population(year,data,v8_assumption,v9_assumption)
  80. n = [0 1 2 3 4 sqrt(5*10) sqrt(11*20) sqrt(21*100) v8_assumption v9_assumption];
  81.  
  82. x = data(data(:,1)==year ,:);
  83. vec = [];
  84. for i = 0:9
  85. vec = [vec, zeros(1,x(i+3))+n(i+1)];
  86. end
  87. end
  88.  
  89. function g = gini_coefficient(population)
  90. s = 0;
  91. for i = 1:length(population)
  92. for j = 1:length(population)
  93. s = s + abs(population(i) - population(j));
  94. end
  95. end
  96. g = s/(2 * length(population).^2 * mean(population));
  97. end
  98.  
  99. %{
  100. 1 label variable year "Gss year for this respondent ";
  101. 2 label variable id_ "Respondent id number";
  102. 3 label variable age "Age of respondent";
  103. 4 label variable sex "Respondents sex";
  104. 5 label variable partners "How many sex partners r had in last year";
  105. 6 label variable partnrs5 "How many sex partners r had in last 5 years";
  106. 7 label variable ballot "Ballot used for interview";
  107.  
  108. field 1: YEAR
  109. field 2: SEX
  110. 2 "Female"
  111. 1 "Male"
  112. field 3: AGE
  113. 99 "No answer"
  114. 98 "Don't know"
  115. 89 "89 or older"
  116. field 3: SEX PARTNERS IN LAST 1 YEAR
  117. 99 "No answer"
  118. 98 "Don't know"
  119. 95 "Several" <-- IGNORE, NO SUCH RESPONSE
  120. 9 "1 or more, # unknown"
  121. 8 "More than 100 partners"
  122. 7 "21-100 partners"
  123. 6 "11-20 partners"
  124. 5 "5-10 partners"
  125. 4 "4 partners"
  126. 3 "3 partners"
  127. 2 "2 partners"
  128. 1 "1 partner"
  129. 0 "No partners"
  130. -1 "Not applicable"
  131. field 4: SEX PARTNERS IN LAST 5 YEARS
  132. same as above.
  133. %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement