diff --git a/prescriptiondrugs/OR_plot_Rxdrugs.m b/prescriptiondrugs/OR_plot_Rxdrugs.m new file mode 100644 index 0000000..058227e --- /dev/null +++ b/prescriptiondrugs/OR_plot_Rxdrugs.m @@ -0,0 +1,344 @@ +%make heatmaps out of the relative risk matrix + +cd .. +cd .. +cd matrices +load OR_2013 +load qlabel_033014 + +drugs={'Q25','Q27','Q29','Q31','Q32'}; +violence={'Q01', 'Q02', 'Q03', 'Q04', 'Q05', 'Q06','Q10', 'Q74', 'Q75', 'Q50', 'Q51'}; +suicide={'Q11','Q12','Q13','Q14','Q16'}; +alcohol_marijuana={'Q17','Q18','Q19','Q21','Q23','Q53', 'Q55'}; % 'Q50','Q51'}; +disordered_eat={'Q38','Q39','Q40'}; +cigarette={'Q53','Q55','Q57','Q58','Q59'}; + +ques='Q79'; +OR=oddsratio2013; +[r,c]=size(OR); +OR=OR(2:r,:); +indx=find(strcmp(OR(:,1),'Q79')==1); %put in the number for Rx drugs +OR=OR(indx,:); + +purple_grey=[99/255 86/255 136/255]; +light_purple=[106/255 90/255 205/255]; +green_blue=[67/255 205/255 128/255]; +green=[0/255 205/255 102/255]; +orange=[255/255 127/255 0/255]; +blue=[24/255 116/255 205/255]; +salmon=[205/255 51/255 51/255]; +mauve=[205/255 96/255 144/255]; +magenta=[205/255 41/255 144/255]; +grey1=[192/255 192/255 192/255]; +grey2=[97/255 97/255 97/255]; + +q1='Q79'; +xlab=OR(:,2); +plot_mat=cell2mat(OR(:,7:9)); +[indx_r, indx_c]=find(plot_mat<100000); +indx_ru=unique(indx_r); +plot_mat2=plot_mat(indx_ru,:); +xlab2=xlab(indx_ru,:); +indx=find(plot_mat2==0); +plot_mat2(indx)=NaN; + +[r,c]=size(plot_mat2); +%x={'2009', '2011', '2013'}; +x=1:r; +for j=1:3 + if j==1 + color=[24/255 116/255 205/255]; %blue + elseif j==2 + color=[0/255 205/255 102/255]; %green + elseif j==3 + color=[255/255 127/255 0/255]; %orange + %elseif + % color=[205/255 0/255 0/255]; %red + end + y=plot_mat2(:,j); + scatter(x, y, 5, color, 'filled'); + hold on +end +avg=nanmean(plot_mat2,2); +scatter(x, avg, 5, color, 'filled'); +hold off +set(gca,'XTickLabel', xlab); +cd .. +cd programs +cd prescriptiondrugs +cd results + +saveas (gcf,'PO_OR.fig'); +close + + +%drugs----------- +count=1; +druglab=cell.empty; +for i=1:numel(drugs) + indx=find(strcmp(xlab2,drugs{i})==1); + if numel(indx)>0 + plot_drugs(count,:)=plot_mat2(indx,:); + druglab{count,1}=drugs{i}; + count=count+1; + end +end + +for i=1:numel(alcohol_marijuana) + indx=find(strcmp(xlab2,alcohol_marijuana{i})==1); + if numel(indx)>0 + plot_drugs(count,:)=plot_mat2(indx,:); + druglab{count,1}=alcohol_marijuana{i}; + count=count+1; + end +end + +%f1=subplot(2,3,1); +[r,c]=size(plot_drugs); +%x={'2009', '2011', '2013'}; +x=1:r; +for j=1:3 + if j==1 + color=[24/255 116/255 205/255]; %blue + elseif j==2 + color=[0/255 205/255 102/255]; %green + %elseif + % color=[255/255 127/255 0/255]; %orange + elseif j==3 + color=[205/255 0/255 0/255]; %red + end + y=plot_drugs(:,j); + scatter(x, y,20,'b','o','filled'); + hold on +end +avg=nanmean(plot_drugs,2); +for i=1:numel(x) + x1=x(i)-0.25; + x2=x(i)+0.25; + x_=[x1,x2]; + y_=[avg(i),avg(i)]; + plot(x_,y_, 'k'); +end +%hold off +set(gca,'Xtick',1:r); +label_new={'Ever used cocaine', 'Ever used heroin', 'Ever used methamphetamine', 'Ever injected Drugs', 'Ever used Ecstasy', 'Ever drink alcohol','Drank alcohol*','Had 5+ alcoholic drinks*', 'Ever used marijuana', 'Used marijuana*', 'Ever smoked cigarettes', 'Smoked cigarettes*'}; +set(gca,'XTickLabel', label_new); +xlim([0,r+1]); +ylim([0 25]); +title('Association between Rx Drug use and Drug, Alcohol and Cigarette Use', 'FontSize', 12, 'FontWeight', 'bold'); +cd .. +rotateXLabels(gca,45) +cd results +ylabel('Odds Ratio with Rx Drug Use', 'FontSize', 12); +set (gca, 'FontSize', 12); +saveas (gcf,'Drug_OR.fig'); +print (gcf, '-dpdf', 'Fig3A.pdf'); +close + +%suicide and disordered eating----------- +count=1; +suicide_lab=cell.empty; +plot_suicide=double.empty; +for i=1:numel(suicide) + indx=find(strcmp(xlab2,suicide{i})==1); + if numel(indx)>0 + plot_suicide(count,:)=plot_mat2(indx,:); + suicide_lab{count,1}=suicide{i}; + count=count+1; + end +end + +for i=1:numel(disordered_eat) + indx=find(strcmp(xlab2,disordered_eat{i})==1); + if numel(indx)>0 + plot_suicide(count,:)=plot_mat2(indx,:); + suicide_lab{count,1}=disordered_eat{i}; + count=count+1; + end +end + +%f1=subplot(2,3,1); +[r,c]=size(plot_suicide); +%x={'2009', '2011', '2013'}; +x=1:r; +for j=1:3 + if j==1 + color=[24/255 116/255 205/255]; %blue + elseif j==2 + color=[0/255 205/255 102/255]; %green + %elseif + % color=[255/255 127/255 0/255]; %orange + elseif j==3 + color=[205/255 0/255 0/255]; %red + end + y=plot_suicide(:,j); + scatter(x, y, 20,'b','o','filled'); + hold on +end +avg=nanmean(plot_suicide,2); +for i=1:numel(x) + x1=x(i)-0.25; + x2=x(i)+0.25; + x_=[x1,x2]; + y_=[avg(i),avg(i)]; + plot(x_,y_, 'k'); +end +%hold off +set(gca,'Xtick',1:r); +label_new={'Felt sad or hopeless^', 'Considered suicide^', 'Planned suicide^', 'Was injured during suicide attempt^', 'Attempted Suicide^', 'Fasted to lose weight*', 'Took pill to lose weight*','Vomited to lose weight*'}; +set(gca,'XTickLabel', label_new); +xlim([0,r+1]); +title('Association between Rx Drug use and Suicide, Disordered Eating', 'FontSize', 12, 'FontWeight', 'bold'); +cd .. +rotateXLabels(gca,45) +cd results +ylabel('Odds Ratio with Rx Drug Use', 'FontSize', 12); +ylim([0 10]); +set (gca, 'FontSize', 12); +saveas (gcf,'suicide_eating_OR.fig'); +print (gcf, '-dpdf', 'Fig3B.pdf'); +close + +%violence/safety----------- +count=1; +safe_lab=cell.empty; +plot_safe=double.empty; +for i=1:numel(violence) + indx=find(strcmp(xlab2,violence{i})==1); + if numel(indx)>0 + plot_safe(count,:)=plot_mat2(indx,:); + safe_lab{count,1}=violence{i}; + count=count+1; + end +end + + +%f1=subplot(2,3,1); +[r,c]=size(plot_safe); +%x={'2009', '2011', '2013'}; +x=1:r; +for j=1:3 + if j==1 + color=[24/255 116/255 205/255]; %blue + elseif j==2 + color=[0/255 205/255 102/255]; %green + %elseif + % color=[255/255 127/255 0/255]; %orange + elseif j==3 + color=[205/255 0/255 0/255]; %red + end + y=plot_safe(:,j); + scatter(x, y, 20,'b','o','filled'); + hold on +end +avg=nanmean(plot_safe,2); +for i=1:numel(x) + x1=x(i)-0.25; + x2=x(i)+0.25; + x_=[x1,x2]; + y_=[avg(i),avg(i)]; + plot(x_,y_, 'k'); +end +%hold off +set(gca,'Xtick',1:r); +label_new={'Carried a weapon*', 'Carried a gun*', 'Carried a weapon at school*', 'Felt unsafe at school*', 'Been threatened at school*', 'Been in a fight*','Ever been forced to have sex', 'Been bullied at school^', 'Been electronically bullied^', 'Ridden with a driver who was drinking*', 'Driven while drinking*'}; +set(gca,'XTickLabel', label_new); +xlim([0,r+1]); +title('Association between Rx Drug use and Physical Violence, School Safety', 'FontSize', 12, 'FontWeight', 'bold'); +cd .. +rotateXLabels(gca,45) +cd results +ylabel('Odds Ratio with Rx Drug Use', 'FontSize', 12); +ylim([0 10]); +set (gca, 'FontSize', 12); +saveas (gcf,'violence_OR.fig'); +print (gcf, '-dpdf', 'Fig3C.pdf'); +%close + +% +% %drugs----------- +% count=1; +% druglab=cell.empty; +% for i=1:numel(drugs) +% indx=find(strcmp(xlab2,drugs{i})==1); +% if numel(indx)>0 +% plot_drugs(count,:)=plot_mat2(indx,:); +% druglab{count,1}=drugs{i}; +% count=count+1; +% end +% end +% +% f1=subplot(2,3,1); +% [r,c]=size(plot_drugs); +% %x={'2009', '2011', '2013'}; +% x=1:r; +% for j=1:3 +% if j==1 +% color=[24/255 116/255 205/255]; %blue +% elseif j==2 +% color=[0/255 205/255 102/255]; %green +% %elseif +% % color=[255/255 127/255 0/255]; %orange +% elseif j==3 +% color=[205/255 0/255 0/255]; %red +% end +% y=plot_drugs(:,j); +% scatter(x, y, 15, 'b','o', 'filled'); +% hold on +% end +% avg=mean(plot_drugs,2); +% for i=1:numel(x) +% x1=x(i)-0.25; +% x2=x(i)+0.25; +% x_=[x1,x2]; +% y_=[avg(i),avg(i)]; +% plot(x_,y_); +% end +% %hold off +% set(gca,'Xtick',1:r); +% set(gca,'XTickLabel', druglab); +% xlim([0,r+1]); +% +% %alcohol_marijuana------ +% %drugs----------- +% count=1; +% alclab=cell.empty; +% for i=1:numel(alcohol_marijuana) +% indx=find(strcmp(xlab2,alcohol_marijuana{i})==1); +% if numel(indx)>0 +% plot_alc(count,:)=plot_mat2(indx,:); +% alclab{count,1}=alcohol_marijuana{i}; +% count=count+1; +% end +% end +% +% f2=subplot(2,3,2:3); +% [r,c]=size(plot_alc); +% %x={'2009', '2011', '2013'}; +% x=1:r; +% for j=1:3 +% if j==1 +% color=[24/255 116/255 205/255]; %blue +% elseif j==2 +% color=[0/255 205/255 102/255]; %green +% %elseif +% % color=[255/255 127/255 0/255]; %orange +% elseif j==3 +% color=[205/255 0/255 0/255]; %red +% end +% y=plot_alc(:,j); +% scatter(x, y, 15, 'b','o', 'filled'); +% hold on +% end +% avg=mean(plot_alc,2); +% for i=1:numel(x) +% x1=x(i)-0.25; +% x2=x(i)+0.25; +% x_=[x1,x2]; +% y_=[avg(i),avg(i)]; +% plot(x_,y_); +% end +% hold off +% set(gca,'Xtick',1:r); +% set(gca,'XTickLabel', alclab); +% xlim([0,r+1]); \ No newline at end of file diff --git a/prescriptiondrugs/PO_freqv2.m b/prescriptiondrugs/PO_freqv2.m new file mode 100644 index 0000000..74d7ff2 --- /dev/null +++ b/prescriptiondrugs/PO_freqv2.m @@ -0,0 +1,195 @@ + +cd .. +cd .. +cd data +cd Controls_061514 +sex=importdata('sex-NaN.txt', '\t'); +race=importdata('race-NaN.txt', '\t'); +weight=importdata('weights-NaN.txt','\t'); +grade=importdata('grade-NaN.txt','\t'); +weight=weight(5:7,:); +race=race(5:7,:); +sex=sex(5:7,:); +grade=grade(5:7,:); +cd .. +cd results_061514 +cd NaN +question_mat=importdata('Q79--NaN.txt', '\t'); +[r,c]=size(question_mat); +%question_mat=question_mat(:,2:c); +question_mat(:,1)=9; +cd .. +cd .. +cd .. + +cd programs +cd prescriptiondrugs +cd results + + +for i=1:r +% total(i)=TOTAL(i,1); + index_yes{i}=find(question_mat(i,:)==1); + index_girls{i}=find(sex(i,:)==1); + index_boys{i}=find(sex(i,:)==2); + index_W{i}=find(race(i,:)== 1 ); + index_B{i}=find(race(i,:)== 2 ); + index_H{i}=find(race(i,:)== 3 ); + index_O{i}=find(race(i,:)== 4 ); + index_9{i}=find(grade(i,:)== 1 ); + index_10{i}=find(grade(i,:)== 2 ); + index_11{i}=find(grade(i,:)== 3 ); + index_12{i}=find(grade(i,:)== 4 ); +end +% ^for each desired variable we created an array of the indexes (ie. students) who are a "yes" in that variable + + +for i=1:r + index_missQ{i}=find(question_mat(i,:)==9); %students who didn't answer the Q + index_nomiss{i}=find(question_mat(i,:)==0 | question_mat(i,:)==1); %answers that were NOT missing (ie. 0's and 1's / no's and yes's) +end + +for i=1:r + missQ(i)=length(index_missQ{i}); %number of students who answered the question each year +end + + +for i=1:r + index_total_b{i}=intersect(index_nomiss{i},index_boys{i}); %index of all boys who answered + index_total_g{i}=intersect(index_nomiss{i},index_girls{i}); %index of all girls who answered +end + +% totals of each race/sex/combo: (for percentages- to compare those who said yes to total questioned) +for i=1:r + w=weight(i,:)'; + total_ans(i)=nansum(w(index_nomiss{i})); + total_girls(i)=nansum(w(index_total_g{i})); %total # of girls who answered + total_boys(i)=nansum(w(index_total_b{i})); %total number of boys who answered + total_W{i}=nansum(w(intersect(index_nomiss{i}, index_W{i}))); %total # of white students who answered + total_B{i}=nansum(w(intersect(index_nomiss{i}, index_B{i}))); %total # of black students who answered + total_H{i}=nansum(w(intersect(index_nomiss{i}, index_H{i}))); %total # of hispanic students who answered + total_O{i}=nansum(w(intersect(index_nomiss{i}, index_O{i}))); %total # of "other" students who answered + total_Wb(i)=nansum(w(intersect(index_total_b{i},index_W{i}))); + total_Wg(i)=nansum(w(intersect(index_total_g{i},index_W{i}))); + total_Bb(i)=nansum(w(intersect(index_total_b{i},index_B{i}))); + total_Bg(i)=nansum(w(intersect(index_total_g{i},index_B{i}))); + total_Hb(i)=nansum(w(intersect(index_total_b{i},index_H{i}))); + total_Hg(i)=nansum(w(intersect(index_total_g{i},index_H{i}))); + total_Ob(i)=nansum(w(intersect(index_total_b{i},index_O{i}))); + total_Og(i)=nansum(w(intersect(index_total_g{i},index_O{i}))); + total_9(i)=nansum(w(intersect((index_9{i}),index_nomiss{i}))); + total_10(i)=nansum(w(intersect((index_10{i}),index_nomiss{i}))); + total_11(i)=nansum(w(intersect((index_11{i}),index_nomiss{i}))); + total_12(i)=nansum(w(intersect((index_12{i}),index_nomiss{i}))); + total_9G(i)=nansum(w(intersect(index_9{i},index_total_g{i}))); + total_10G(i)=nansum(w(intersect(index_10{i},index_total_g{i}))); + total_11G(i)=nansum(w(intersect(index_11{i},index_total_g{i}))); + total_12G(i)=nansum(w(intersect(index_12{i},index_total_g{i}))); + total_9B(i)=nansum(w(intersect(index_9{i},index_total_b{i}))); + total_10B(i)=nansum(w(intersect(index_10{i},index_total_b{i}))); + total_11B(i)=nansum(w(intersect(index_11{i},index_total_b{i}))); + total_12B(i)=nansum(w(intersect(index_12{i},index_total_b{i}))); +end + +% %yes-es: +for i=1:r + w=weight(i,:)'; + index_yesgirls{i}=intersect(index_yes{i},index_girls{i}); + index_yesboys{i}=intersect(index_yes{i},index_boys{i}); + yes_girls(i)=nansum(w(index_yesgirls{i})); + yes_boys(i)=nansum(w(index_yesboys{i})); + yes_W(i)=nansum(w(intersect(index_yes{i}, index_W{i}))); + yes_B(i)=nansum(w(intersect(index_yes{i}, index_B{i}))); + yes_H(i)=nansum(w(intersect(index_yes{i}, index_H{i}))); + yes_O(i)=nansum(w(intersect(index_yes{i}, index_O{i}))); + yes_WG(i)=nansum(w(intersect(index_yesgirls{i},index_W{i}))); + yes_BG(i)=nansum(w(intersect(index_yesgirls{i},index_B{i}))); + yes_HG(i)=nansum(w(intersect(index_yesgirls{i},index_H{i}))); + yes_OG(i)=nansum(w(intersect(index_yesgirls{i},index_O{i}))); + yes_WB(i)=nansum(w(intersect(index_yesboys{i},index_W{i}))); + yes_BB(i)=nansum(w(intersect(index_yesboys{i},index_B{i}))); + yes_HB(i)=nansum(w(intersect(index_yesboys{i},index_H{i}))); + yes_OB(i)=nansum(w(intersect(index_yesboys{i},index_O{i}))); + total_yes(i)=nansum(w(index_yes{i})); + yes_9(i)=nansum(w(intersect(index_yes{i},index_9{i}))); + yes_10(i)=nansum(w(intersect(index_yes{i},index_10{i}))); + yes_11(i)=nansum(w(intersect(index_yes{i},index_11{i}))); + yes_12(i)=nansum(w(intersect(index_yes{i},index_12{i}))); + yes_9B(i)=nansum(w(intersect(index_yesboys{i},index_9{i}))); + yes_10B(i)=nansum(w(intersect(index_yesboys{i},index_10{i}))); + yes_11B(i)=nansum(w(intersect(index_yesboys{i},index_11{i}))); + yes_12B(i)=nansum(w(intersect(index_yesboys{i},index_12{i}))); + yes_9G(i)=nansum(w(intersect(index_yesgirls{i},index_9{i}))); + yes_10G(i)=nansum(w(intersect(index_yesgirls{i},index_10{i}))); + yes_11G(i)=nansum(w(intersect(index_yesgirls{i},index_11{i}))); + yes_12G(i)=nansum(w(intersect(index_yesgirls{i},index_12{i}))); + total_w(i)=total_W{i}; + total_b(i)=total_B{i}; + total_h(i)=total_H{i}; + total_o(i)=total_O{i}; +end + +%put them all into a matrix as percent values: +per_mat = zeros(16,r); +for i=1:r + per_mat(1,i)=total_yes(i)/total_ans(i)*100; %total + per_mat(2,i)=yes_boys(i)/total_boys(i)*100; %boys + per_mat(3,i)=yes_girls(i)/total_girls(i)*100; %girls + per_mat(4,i)=yes_W(i)/total_w(i)*100; %whites + per_mat(5,i)=yes_B(i)/total_b(i)*100; %blacks + per_mat(6,i)=yes_H(i)/total_h(i)*100; %hispanics + per_mat(7,i)=yes_O(i)/total_o(i)*100; %other + per_mat(8, i)=yes_WB(i)/total_Wb(i)*100; %WB + per_mat(9, i)=yes_WG(i)/total_Wg(i)*100; %WG + per_mat(10, i)=yes_BB(i)/total_Bb(i)*100; %BB + per_mat(11, i)=yes_BG(i)/total_Bg(i)*100; %BG + per_mat(12, i)=yes_HB(i)/total_Hb(i)*100; %HB + per_mat(13, i)=yes_HG(i)/total_Hg(i)*100; %HG + per_mat(14, i)=yes_OB(i)/total_Ob(i)*100; %OB + per_mat(15, i)=yes_OG(i)/total_Og(i)*100; %OG + per_mat(16, i)=yes_9(i)/total_9(i)*100; + per_mat(17, i)=yes_9B(i)/total_9B(i)*100; + per_mat(18, i)=yes_9G(i)/total_9G(i)*100; + per_mat(19, i)=yes_10(i)/total_10(i)*100; + per_mat(20, i)=yes_10B(i)/total_10B(i)*100; + per_mat(21, i)=yes_10G(i)/total_10G(i)*100; + per_mat(22, i)=yes_11(i)/total_11(i)*100; + per_mat(23, i)=yes_11B(i)/total_11B(i)*100; + per_mat(24, i)=yes_11G(i)/total_11G(i)*100; + per_mat(25, i)=yes_12(i)/total_12(i)*100; + per_mat(26, i)=yes_12B(i)/total_12B(i)*100; + per_mat(27, i)=yes_12G(i)/total_12G(i)*100; +end + +%per_mat=flipud(per_mat); + + label_year={'2009','2011', '2013'}; + label_cell2={'Total', 'Boys', 'Girls', 'W', 'B', 'H', 'O', 'W Boys', 'W Girls', 'B Boys', 'B Girls', 'H Boys', 'H Girls', 'O Boys', 'O Girls', '9th', '9th-Boys', '9th-Girls', '10th','10th-Boys', '10th-Girls', '11th', '11th-Boys', '11th-Girls', '12th', '12th-Boys', '12th-Boys'}; + %per_mat_map(1:15,1:r)=per_mat(1:15,1:r); + %per_mat_map=flipdim(per_mat_map,1); + max_mat=max(max(per_mat)); + if max_mat>75 + M=100; + elseif max_mat>50 + M=75; + elseif max_mat>25 + M=50; + else + M=25; + end + M=50 + %get rid of deimals + per_mat=per_mat*10; + per_mat=round(per_mat); + per_mat=per_mat/10; + h=figure; + title1={'During your life, how many times have you taken a prescription drug'; '(such as OxyContin, Percocet, Vicodin, codeine, Adderall, Ritalin, or Xanax)'; 'without a doctor’s prescription?'}; + [hImage]=heatmap_rb(per_mat, label_year, label_cell2, 1, M, 0, 'Colormap','money', 'UseLogColormap', false, 'ShowAllTicks',true, 'Colorbar',true,'TextColor','k', 'FontSize', 12); + title (title1, 'FontSize', 12, 'fontWeight','bold'); + set (gca, 'FontSize',12); + + + saveas (gcf, 'PO_heatmap.fig'); %can make pdf, jnp, or jpg + print (gcf, '-dpdf', 'PO_heatmap.pdf'); + + close all diff --git a/prescriptiondrugs/PO_heroin_vennsv2.m b/prescriptiondrugs/PO_heroin_vennsv2.m new file mode 100644 index 0000000..e9d8aa6 --- /dev/null +++ b/prescriptiondrugs/PO_heroin_vennsv2.m @@ -0,0 +1,338 @@ +%files1=dir(fullfile('C:','Users','kruggles7','Documents','MATLAB', 'CDC', 'data','cat_results_010314', '*.txt')); + +cd .. +cd .. +cd data +cd results_061514 +cd cat +index_yes=double.empty; + + +questions={'Q79', 'Q27', 'Q31'}; +labels={'Prescription Drugs', 'Heroin', 'Injected Drugs'}; + +quest_1=importdata('Q79-cat-NaN.txt', '\t'); +[r1,c1]=size(quest_1); +q1_2013=quest_1(r1,2:c1); +q1_2011=quest_1(r1-1,2:c1); +q1_2009=quest_1(r1-2,2:c1); +q1_all=cat(2,q1_2013, q1_2011); +q1_all=cat(2,q1_all, q1_2009); + +max_=max(q1_all); +indx_nomiss=find(q1_all>0); +q1_total=numel(indx_nomiss); + +quest_2=importdata('Q27-cat-NaN.txt', '\t'); +[r2,c2]=size(quest_2); +q2_2013=quest_2(r2,2:c2); +q2_2011=quest_2(r2-1,2:c2); +q2_2009=quest_2(r2-2,2:c2); +q2_all=cat(2,q2_2013, q2_2011); +q2_all=cat(2,q2_all, q2_2009); + +indx_nomiss=find(q2_all>0); +q2_total=numel(indx_nomiss); + +quest_3=importdata('Q31-cat-NaN.txt','\t'); +[r3,c3]=size(quest_3); +q3_2013=quest_3(r3,2:c3); +q3_2011=quest_3(r3-1,2:c3); +q3_2009=quest_3(r3-2,2:c3); +q3_all=cat(2,q3_2013, q3_2011); +q3_all=cat(2,q3_all, q3_2009); + +indx_nomiss=find(q3_all>0); +q3_total=numel(indx_nomiss); + +cd .. +cd .. +cd .. +cd programs +cd prescriptiondrugs +cd results + +indx_heroin=find(q2_all>1); +indx_injection=find(q3_all>1); + +table_PO=cell.empty; +table_heroin=cell.empty; +table_overlap=cell(6,8); +table_overlap_heroin=cell(6,5); +table_PO{1,1}='PO lifetime'; +table_PO{2,1}='PO > 3 times'; +table_PO{3,1}='PO > 10 times'; +table_PO{4,1}='PO > 20 times'; +table_PO{5,1}='PO > 40 times'; +table_PO{6,1}='heroin lifetime'; +table_PO{7,1}='injected lifetime'; + +table_heroin{1,1}='Heroin lifetime'; +table_heroin{2,1}='Heroin > 3 times'; +table_heroin{3,1}='Heroin > 10 times'; +table_heroin{4,1}='Heroin > 20 times'; +table_heroin{5,1}='Heroin > 40 times'; + + +%remove missing values +indx=find(q1_all>0 & q2_all>0 & q3_all>0 ); +q1_all=q1_all(indx); +q2_all=q2_all(indx); +q3_all=q3_all(indx); + +indx2=find(q2_all>1); +indx_no2=find(q2_all==1); +table_PO{6,2}=numel(indx2); +indx3=find(q3_all>1); +indx_no3=find(q3_all==1); +table_PO{7,2}=numel(indx3); +indx_no1=find(q1_all==1); +indx1_=find(q1_all>1); + +table_overlap{1,2}='PO only (C)'; +table_overlap{1,3}='heroin+injected+PO (E)'; +table_overlap{1,4}='PO+heroin-injection(B)'; +table_overlap{1,5}='injected+heroin-PO(D)'; +table_overlap{1,6}='PO+injected-heroin (F)'; +table_overlap{1,7}='heroin only (A)'; +table_overlap{1,8}='injection only (G)'; + +table_overlap_heroin{1,2}='Heroin only'; +table_overlap_heroin{1,3}='heroin+injected+po'; +table_overlap_heroin{1,4}='heroin+PO-injections'; +table_overlap_heroin{1,5}='heroin+injections-PO'; + +table_overlap{2,1}='PO lifetime'; +table_overlap{3,1}='PO > 3 times'; +table_overlap{4,1}='PO > 10 times'; +table_overlap{5,1}='PO > 20 times'; +table_overlap{6,1}='PO > 40 times'; + +table_overlap_heroin{2,1}='heroin lifetime'; +table_overlap_heroin{3,1}='heroin > 3 times'; +table_overlap_heroin{4,1}='heroin > 10 times'; +table_overlap_heroin{5,1}='heroin > 20 times'; +table_overlap_heroin{6,1}='heroin > 40 times'; + +for i=1:max_-1 + indx1=find(q1_all>i); + indx2_=find(q2_all>i); + table_PO{i,2}=numel(indx1); + table_heroin{i,2}=numel(indx2_); + %heroin only + indx_=intersect(indx2_, indx_no1); + indx=intersect(indx_, indx_no3); + table_overlap_heroin{i+1,2}=numel(indx); + %heroin+injected+po + indx_=intersect(indx2_, indx1_); + indx=intersect(indx_, indx3); + table_overlap_heroin{i+1,3}=numel(indx); + %heroin+PO-injections + indx_=intersect(indx2_,indx1_); + indx=intersect(indx_,indx_no3); + table_overlap_heroin{i+1,4}=numel(indx); + %heroin+injections-PO + indx_=intersect(indx2_,indx3); + indx=intersect(indx_,indx_no1); + table_overlap_heroin{i+1,5}=numel(indx); + + %heroin only (A) + indx_=intersect(indx2, indx_no1); + indx=intersect(indx_, indx_no3); + table_overlap{i+1,7}=numel(indx); + + %PO+heroin-injection(B) + indx_=intersect(indx1,indx2); + indx=intersect(indx_,indx_no3); + table_overlap{i+1,4}=numel(indx); + %PO only (C) + indx_=intersect(indx1, indx_no2); + indx=intersect(indx_, indx_no3); + table_overlap{i+1,2}=numel(indx); + %injected+heroin-PO(D) + indx_=intersect(indx2,indx3); + indx=intersect(indx_,indx_no1); + table_overlap{i+1,5}=numel(indx); + %heroin+injected+PO(E) + indx_=intersect(indx1,indx2); + indx=intersect(indx_,indx3); + table_overlap{i+1,3}=numel(indx); + %PO+injected-heroin(F) + indx_=intersect(indx1,indx3); + indx=intersect(indx_,indx_no2); + table_overlap{i+1,6}=numel(indx); + %injection only (G) + indx_=intersect(indx3, indx_no1); + indx=intersect(indx_,indx_no2); + table_overlap{i+1,8}=numel(indx); +end + +%injected drugs --------------------------------- + +table_injected=cell.empty; +table_injected{2,1}='injected 1 time'; +table_injected{3,1}='injected 2 or more times'; + +table_overlap_injected=cell(3,5); +table_overlap_injected{1,2}='injected only'; +table_overlap_injected{1,3}='heroin+injected+po'; +table_overlap_injected{1,4}='injected+PO-heroin'; +table_overlap_injected{1,5}='injections+heroin-PO'; +table_overlap_injected{2,1}='injected 1 time'; +table_overlap_injected{3,1}='injected 2 or more times'; + +for i=1:2 + indx3=find(q3_all>i); + table_injected{i+1,2}=numel(indx3); + %injected only + indx_=intersect(indx3, indx_no1); + indx=intersect(indx_, indx_no2); + table_overlap_injected{i+1,2}=numel(indx); + %heroin+injected+po + indx_=intersect(indx3, indx1_); + indx=intersect(indx_, indx2); + table_overlap_injected{i+1,3}=numel(indx); + %injected+PO-heroin + indx_=intersect(indx3,indx1_); + indx=intersect(indx_,indx_no2); + table_overlap_injected{i+1,4}=numel(indx); + %injections+heroin-PO + indx_=intersect(indx2,indx3); + indx=intersect(indx_,indx_no1); + table_overlap_injected{i+1,5}=numel(indx); +end + +inj=cell2mat(table_overlap_injected(2:3,2:5)); +overlap_i(:,1)=inj(:,1); +overlap_i(:,2)=inj(:,3); +overlap_i(:,3)=inj(:,4); +overlap_i(:,4)=inj(:,2); +oi=sum(overlap_i,2); +overlap_i(1,:)=overlap_i(1,:)/oi(1)*100; +overlap_i(2,:)=overlap_i(2,:)/oi(2)*100; + +f1=subplot(3,1,1); +x=1:2; +oi_=oi/q3_total*100; +plot(x,oi_, '-ok', 'MarkerFaceColor','k'); +set(gca,'xtick',[0 1 2]); +set(gca, 'xticklabel', []); +xlim([0.5 2.5]); +ylabel('Total Injection Users'); +f2=subplot(3,1,2:3); +bar(overlap_i,'stacked'); +ylim([0 100]); +labels={'1 time', '2 or more times'}; +set(gca, 'xticklabel', labels); +xlim([0.5 2.5]); +ylabel('% of Total Injection Users'); +legend('Injected Only','Injected Drugs & Prescription Drugs', 'Injected Drugs & Heroin', 'Injected Drugs, Prescription Drugs, & Heroin ', 'Location','SouthOutside' ); +%linkaxes([f1,f2], 'x'); +%find the current position [x,y,width, height] +pos1=get(f1,'Position'); +pos2=get(f2,'Position'); +%set widths equal +pos2(3)=pos1(3); +set (f2, 'Position', pos2); +%set x of first equal to second +pos1(1)=pos2(1); +set(f1, 'Position', pos1); +%move the second to touch first +pos2(2)= pos1(2) - pos2(4); %was pos1(3) +set (f2, 'Position', pos2); + +saveas (gca, 'Injection_overlaps.fig'); +close + +%make the stacked bars---------------------------------------------------- +t=cell2mat(table_overlap(2:6,2:8)); +overlap(:,1)=t(:,1); +overlap(:,2)=t(:,3); +overlap(:,3)=t(:,5); +overlap(:,4)=t(:,2); +o=sum(overlap,2); +overlap(1,:)=overlap(1,:)/o(1)*100; +overlap(2,:)=overlap(2,:)/o(2)*100; +overlap(3,:)=overlap(3,:)/o(3)*100; +overlap(4,:)=overlap(4,:)/o(4)*100; +overlap(5,:)=overlap(5,:)/o(5)*100; + +f1=subplot(3,1,1); +x=1:1:5; +o_=o/q1_total*100; +plot(x,o_, '-ok', 'MarkerFaceColor','k'); +%set(gcf,'xticks', []); +set(gca,'xtick',[0 1 2 3 4 5]); +set(gca, 'xticklabel', []); +xlim([0.5 5.5]); +ylabel('Frequency of PO Users'); +f2=subplot(3,1,2:3); +bar(overlap,'stacked'); +ylim([0 100]); +labels={'>0 times', '>2 times', '>9 times', '>19 times', '>39 times'}; +set(gca, 'xticklabel', labels); +ylabel('% of Total PO Users'); +legend('Prescription Drugs Only','Prescription Drugs & Heroin', 'Prescription Drugs & Injected Drugs', 'Prescription Drugs, Heroin & Injected Drugs', 'Location','SouthOutside' ); +%linkaxes([f1,f2], 'x'); +%find the current position [x,y,width, height] +pos1=get(f1,'Position'); +pos2=get(f2,'Position'); +%set widths equal +pos2(3)=pos1(3); +set (f2, 'Position', pos2); +%set x of first equal to second +pos1(1)=pos2(1); +set(f1, 'Position', pos1); +%move the second to touch first +pos2(2)= pos1(2) - pos2(4); %was pos1(3) +set (f2, 'Position', pos2); + +saveas (gca, 'PO_overlaps.fig'); +close + +%make the stacked bars for heroin +t2=cell2mat(table_overlap_heroin(2:6,2:5)); +overlap2(:,1)=t2(:,1); +overlap2(:,2)=t2(:,3); +overlap2(:,3)=t2(:,4); +overlap2(:,4)=t2(:,2); +o2=sum(overlap2,2); +overlap2(1,:)=overlap2(1,:)/o2(1)*100; +overlap2(2,:)=overlap2(2,:)/o2(2)*100; +overlap2(3,:)=overlap2(3,:)/o2(3)*100; +overlap2(4,:)=overlap2(4,:)/o2(4)*100; +overlap2(5,:)=overlap2(5,:)/o2(5)*100; +f1=subplot(3,1,1); +x=1:1:5; +o2_=o2/q2_total*100; +plot(x,o2_, '-ok', 'MarkerFaceColor','k'); +%set(gcf,'xticks', []); +set(gca,'xtick',[0 1 2 3 4 5]); +set(gca, 'xticklabel', []); +xlim([0.5 5.5]); +ylabel('Frequency of Heroin Users'); +f2=subplot(3,1,2:3); +bar(overlap2,'stacked'); +ylim([0 100]); +labels={'>0 times', '>2 times', '>9 times', '>19 times', '>39 times'}; +set(gca, 'xticklabel', labels); +ylabel('% of Total Heroin Users'); +legend('Heroin Only','Heroin & Prescription Drugs', 'Heroin & Injected Drugs', 'Heroin, Prescription Drugs, & Injected Drugs', 'Location','SouthOutside' ); +%linkaxes([f1,f2], 'x'); +%find the current position [x,y,width, height] +pos1=get(f1,'Position'); +pos2=get(f2,'Position'); +%set widths equal +pos2(3)=pos1(3); +set (f2, 'Position', pos2); +%set x of first equal to second +pos1(1)=pos2(1); +set(f1, 'Position', pos1); +%move the second to touch first +pos2(2)= pos1(2) - pos2(4); %was pos1(3) +set (f2, 'Position', pos2); + +saveas (gca, 'Heroin_overlaps.fig'); +close + + diff --git a/prescriptiondrugs/anova_ttest_demographics.m b/prescriptiondrugs/anova_ttest_demographics.m new file mode 100644 index 0000000..044cef7 --- /dev/null +++ b/prescriptiondrugs/anova_ttest_demographics.m @@ -0,0 +1,683 @@ +files=dir(fullfile('C:','Users','kruggles7','Documents','MATLAB', 'Rajan', 'results_020314', 'NaN', '*.txt')); +% reads all the text files in the folder 'binary_NaN_files' and saves them in an array called files +% make sure that folder contains only the NaN files for the questions you want to run create_hm_graph for +N=length(files); + +cd .. +cd .. +cd data +cd Controls_061514 +sex=importdata('sex-NaN.txt', '\t'); +race=importdata('race-NaN.txt', '\t'); +weight=importdata('weights-NaN.txt','\t'); +grade=importdata('grade-NaN.txt','\t'); +weight=weight(5:7,:); +race=race(5:7,:); +sex=sex(5:7,:); +grade=grade(5:7,:); +cd .. +cd results_061514 +cd NaN +%question_mat=importdata('Q79--NaN.txt', '\t');%PO +question_mat=importdata('Q27--NaN.txt', '\t'); %heroin Q27, injected Q31 +question_mat=question_mat(5:7,:); %heroin and injection +[r,c]=size(question_mat); +%question_mat=question_mat(:,2:c); +question_mat(:,1)=9; +cd .. +cd .. +cd .. + +cd programs +cd prescriptiondrugs +cd results + +p_mat=cell(28,4); +t_mat=cell(28,4); +p_anova_mat=cell(28,4); +f_mat=cell(28,4); + +p_mat{2,1}='total'; +p_mat{3,1}='girls'; +p_mat{4,1}='boys'; +p_mat{5,1}='white'; +p_mat{6,1}='black'; +p_mat{7,1}='hispanic'; +p_mat{8,1}='other'; +p_mat{9,1}='white-girls'; +p_mat{10,1}='black-girls'; +p_mat{11,1}='hispanic-girls'; +p_mat{12,1}='other-girls'; +p_mat{13,1}='white-boys'; +p_mat{14,1}='black-boys'; +p_mat{15,1}='hispanic-boys'; +p_mat{16,1}='other-boys'; +p_mat{17,1}='9th'; +p_mat{18,1}='9th-boys'; +p_mat{19,1}='9th-girls'; +p_mat{20,1}='10th'; +p_mat{21,1}='10th-boys'; +p_mat{22,1}='10th-girls'; +p_mat{23,1}='11th'; +p_mat{24,1}='11th-boys'; +p_mat{25,1}='11th-girls'; +p_mat{26,1}='12th'; +p_mat{27,1}='12th-boys'; +p_mat{28,1}='12th-girls'; +t_mat(:,1)=p_mat(:,1); +p_anova_mat(:,1)=p_mat(:,1); +f_mat(:,1)=p_mat(:,1); + + +[r,c]=size(question_mat); +indx=find(question_mat==9); +question_mat(indx)=NaN; + +%ANOVA------------------------------------------------------------------ +[r,c]=size(question_mat); +c_new=c-1; +a_mat=nan(c_new,r); +s_mat=nan(c_new,r); +r_mat=nan(c_new,r); +g_mat=nan(c_new,r); +for j=1:c_new + for k=1:r + a_mat(j,k)=question_mat(k,j+1)*weight(r,j+1); + s_mat(1:c_new,k)=sex(k,2:c); + r_mat(1:c_new,k)=race(k,2:c); + g_mat(1:c_new,k)=grade(k,2:c); + end +end + +i=1; +[p, table,stats]=anova1(a_mat); +p_anova_mat{2, i+1}=p; +f_mat{2, i+1}=table{2,5}; + +index_girls=find(s_mat==1); +temp_mat=nan(c_new,r); +temp_mat(index_girls)=a_mat(index_girls); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{3, i+1}=p; +f_mat{3, i+1}=table{2,5}; + +index_boys=find(s_mat==2); +temp_mat=nan(c_new,r); +temp_mat(index_boys)=a_mat(index_boys); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{4, i+1}=p; +f_mat{4, i+1}=table{2,5}; + +index_W=find(r_mat==1); +temp_mat=nan(c_new,r); +temp_mat(index_W)=a_mat(index_W); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{5, i+1}=p; +f_mat{5, i+1}=table{2,5}; + +index_B=find(r_mat==2); +temp_mat=nan(c_new,r); +temp_mat(index_B)=a_mat(index_B); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{6, i+1}=p; +f_mat{6, i+1}=table{2,5}; + +index_H=find(r_mat==3); +temp_mat=nan(c_new,r); +temp_mat(index_H)=a_mat(index_H); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{7, i+1}=p; +f_mat{7, i+1}=table{2,5}; + +index_O=find(r_mat==4); +temp_mat=nan(c_new,r); +temp_mat(index_O)=a_mat(index_O); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{8, i+1}=p; +f_mat{8, i+1}=table{2,5}; + +index_WG=intersect(index_W, index_girls); +temp_mat=nan(c_new,6); +temp_mat(index_WG)=a_mat(index_WG); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{9, i+1}=p; +f_mat{9, i+1}=table{2,5}; + +index_BG=intersect(index_B, index_girls); +temp_mat=nan(c_new,r); +temp_mat(index_BG)=a_mat(index_BG); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{10, i+1}=p; +f_mat{10, i+1}=table{2,5}; + +index_HG=intersect(index_H, index_girls); +temp_mat=nan(c_new,r); +temp_mat(index_HG)=a_mat(index_HG); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{11, i+1}=p; +f_mat{11, i+1}=table{2,5}; + +index_OG=intersect(index_O, index_girls); +temp_mat=nan(c_new,r); +temp_mat(index_OG)=a_mat(index_OG); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{12, i+1}=p; +f_mat{12, i+1}=table{2,5}; + +index_WB=intersect(index_W, index_boys); +temp_mat=nan(c_new,r); +temp_mat(index_WB)=a_mat(index_WB); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{13, i+1}=p; +f_mat{13, i+1}=table{2,5}; + +index_BB=intersect(index_B, index_boys); +temp_mat=nan(c_new,r); +temp_mat(index_BB)=a_mat(index_BB); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{14, i+1}=p; +f_mat{14, i+1}=table{2,5}; + +index_HB=intersect(index_H, index_boys); +temp_mat=nan(c_new,r); +temp_mat(index_HB)=a_mat(index_HB); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{15, i+1}=p; +f_mat{15, i+1}=table{2,5}; + +index_OB=intersect(index_O, index_boys); +temp_mat=nan(c_new,r); +temp_mat(index_OB)=a_mat(index_OB); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{16, i+1}=p; +f_mat{16, i+1}=table{2,5}; + +index_9=find(g_mat==1); +temp_mat=nan(c_new,r); +temp_mat(index_9)=a_mat(index_9); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{17, i+1}=p; +f_mat{17, i+1}=table{2,5}; + +index_10=find(g_mat==2); +temp_mat=nan(c_new,r); +temp_mat(index_10)=a_mat(index_10); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{20, i+1}=p; +f_mat{20, i+1}=table{2,5}; + +index_11=find(g_mat==3); +temp_mat=nan(c_new,r); +temp_mat(index_11)=a_mat(index_11); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{23, i+1}=p; +f_mat{23, i+1}=table{2,5}; + +index_12=find(g_mat==4); +temp_mat=nan(c_new,r); +temp_mat(index_12)=a_mat(index_12); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{26, i+1}=p; +f_mat{26, i+1}=table{2,5}; + +index_9G=intersect(index_9, index_girls); +temp_mat=nan(c_new,r); +temp_mat(index_9G)=a_mat(index_9G); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{19, i+1}=p; +f_mat{19, i+1}=table{2,5}; + +index_10G=intersect(index_10, index_girls); +temp_mat=nan(c_new,r); +temp_mat(index_10G)=a_mat(index_10G); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{22, i+1}=p; +f_mat{22, i+1}=table{2,5}; + +index_11G=intersect(index_11, index_girls); +temp_mat=nan(c_new,r); +temp_mat(index_11G)=a_mat(index_11G); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{25, i+1}=p; +f_mat{25, i+1}=table{2,5}; + +index_12G=intersect(index_12, index_girls); +temp_mat=nan(c_new,r); +temp_mat(index_12G)=a_mat(index_12G); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{28, i+1}=p; +f_mat{28, i+1}=table{2,5}; + + +index_9B=intersect(index_9, index_boys); +temp_mat=nan(c_new,r); +temp_mat(index_9B)=a_mat(index_9B); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{18, i+1}=p; +f_mat{18, i+1}=table{2,5}; + +index_10B=intersect(index_10, index_boys); +temp_mat=nan(c_new,r); +temp_mat(index_10B)=a_mat(index_10B); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{21, i+1}=p; +f_mat{21, i+1}=table{2,5}; + +index_11B=intersect(index_11, index_boys); +temp_mat=nan(c_new,r); +temp_mat(index_11B)=a_mat(index_11B); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{24, i+1}=p; +f_mat{24, i+1}=table{2,5}; + +index_12B=intersect(index_12, index_boys); +temp_mat=nan(c_new,r); +temp_mat(index_12B)=a_mat(index_12B); +[p, table,stats]=anova1(temp_mat); +p_anova_mat{27, i+1}=p; +f_mat{27, i+1}=table{2,5}; +close all + +%TTEST--------------------------------------------------------------- + +[r,c]=size(question_mat); +c_new=c-1; +stat_mat=nan(c_new*3,7); +stat_mat(1:c_new,1)=question_mat(1,2:c); +stat_mat(1:c_new,2)=2009; +stat_mat(1:c_new,3)=sex(1,2:c); +stat_mat(1:c_new,4)=race(1,2:c); +stat_mat(1:c_new,5)=weight(1,2:c); +stat_mat(1:c_new,6)=grade(1,2:c); + +stat_mat(c_new+1:c_new*2,1)=question_mat(2,2:c); +stat_mat(c_new+1:c_new*2,2)=2011; +stat_mat(c_new+1:c_new*2,3)=sex(2,2:c); +stat_mat(c_new+1:c_new*2,4)=race(2,2:c); +stat_mat(c_new+1:c_new*2,5)=weight(2,2:c); +stat_mat(c_new+1:c_new*2,6)=grade(2,2:c); + +stat_mat(c_new*2+1:c_new*3,1)=question_mat(3,2:c); +stat_mat(c_new*2+1:c_new*3,2)=2013; +stat_mat(c_new*2+1:c_new*3,3)=sex(3,2:c); +stat_mat(c_new*2+1:c_new*3,4)=race(3,2:c); +stat_mat(c_new*2+1:c_new*3,5)=weight(3,2:c); +stat_mat(c_new*2+1:c_new*3,6)=grade(3,2:c); + +indx_nan=find(isnan(stat_mat(:,1))==0 & isnan(stat_mat(:,5))==0); +stat_mat2=stat_mat(indx_nan, :); +[r,c]=size(stat_mat2); + +for t=1:r + stat_mat2(t,7)=stat_mat2(t,1)*stat_mat2(t,5); +end + +years=[2009 2011 2013]; +count=2; +y=2; +for j=1:2 + year=years(j); + for k=j+1:3 + year2=years(k); + p_mat{1,y}=[num2str(year) '-' num2str(year2)]; + t_mat{1,y}=p_mat{1,y}; + indx1=find(stat_mat2(:,2)==year & isnan(stat_mat2(:,7))==0); + indx2=find(stat_mat2(:,2)==year2 & isnan(stat_mat2(:,7))==0); + m=max(numel(indx1),numel(indx2)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx1),1)=stat_mat2(indx1,7); + Y(1:numel(indx2),1)=stat_mat2(indx2,7); + %[t, chi2, p]=crosstab(X,Y); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{2,count}=tstat_; + p_mat{2,count}=p; + + index_boys=find(stat_mat2(:,3)==2); + indx1b=intersect(indx1,index_boys); + indx2b=intersect(indx2,index_boys); + m=max(numel(indx1b),numel(indx2b)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx1b),1)=stat_mat2(indx1b,7); + Y(1:numel(indx2b),1)=stat_mat2(indx2b,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{3,count}=tstat_; + p_mat{3,count}=p; + + index_girls=find(stat_mat2(:,3)==1); + indx1g=intersect(indx1,index_girls); + indx2g=intersect(indx2,index_girls); + m=max(numel(indx1g),numel(indx2g)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx1g),1)=stat_mat2(indx1g,7); + Y(1:numel(indx2g),1)=stat_mat2(indx2g,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{4,count}=tstat_; + p_mat{4,count}=p; + + index_W=find(stat_mat2(:,4)==1); + indx1W=intersect(indx1,index_W); + indx2W=intersect(indx2,index_W); + m=max(numel(indx1W),numel(indx2W)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx1W),1)=stat_mat2(indx1W,7); + Y(1:numel(indx2W),1)=stat_mat2(indx2W,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{5,count}=tstat_; + p_mat{5,count}=p; + + index_B=find(stat_mat2(:,4)==2); + indx1B=intersect(indx1,index_B); + indx2B=intersect(indx2,index_B); + m=max(numel(indx1B),numel(indx2B)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx1B),1)=stat_mat2(indx1B,7); + Y(1:numel(indx2B),1)=stat_mat2(indx2B,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{6,count}=tstat_; + p_mat{6,count}=p; + + index_H=find(stat_mat2(:,4)==3); + indx1H=intersect(indx1,index_H); + indx2H=intersect(indx2,index_H); + m=max(numel(indx1H),numel(indx2H)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx1H),1)=stat_mat2(indx1H,7); + Y(1:numel(indx2H),1)=stat_mat2(indx2H,7); + %[t, chi2, p]=crosstab(X,Y); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{7,count}=tstat_; + p_mat{7,count}=p; + + index_O=find(stat_mat2(:,4)==4); + indx1O=intersect(indx1,index_O); + indx2O=intersect(indx2,index_O); + m=max(numel(indx1O),numel(indx2O)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx1O),1)=stat_mat2(indx1O,7); + Y(1:numel(indx2O),1)=stat_mat2(indx2O,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{8,count}=tstat_; + p_mat{8,count}=p; + + index_WG=intersect(index_girls,index_W); + indx1WG=intersect(indx1,index_WG); + indx2WG=intersect(indx2,index_WG); + m=max(numel(indx1WG),numel(indx2WG)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx1WG),1)=stat_mat2(indx1WG,7); + Y(1:numel(indx2WG),1)=stat_mat2(indx2WG,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{9,count}=tstat_; + p_mat{9,count}=p; + + index_BG=intersect(index_girls,index_B); + indx1BG=intersect(indx1,index_BG); + indx2BG=intersect(indx2,index_BG); + m=max(numel(indx1BG),numel(indx2BG)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx1BG),1)=stat_mat2(indx1BG,7); + Y(1:numel(indx2BG),1)=stat_mat2(indx2BG,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{10,count}=tstat_; + p_mat{10,count}=p; + + index_HG=intersect(index_girls,index_H); + indx1HG=intersect(indx1,index_HG); + indx2HG=intersect(indx2,index_HG); + m=max(numel(indx1HG),numel(indx2HG)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx1HG),1)=stat_mat2(indx1HG,7); + Y(1:numel(indx2HG),1)=stat_mat2(indx2HG,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{11,count}=tstat_; + p_mat{11,count}=p; + + index_OG=intersect(index_girls,index_O); + indx1OG=intersect(indx1,index_OG); + indx2OG=intersect(indx2,index_OG); + m=max(numel(indx1OG),numel(indx2OG)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx1OG),1)=stat_mat2(indx1OG,7); + Y(1:numel(indx2OG),1)=stat_mat2(indx2OG,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{12,count}=tstat_; + p_mat{12,count}=p; + + index_WB=intersect(index_boys,index_W); + indx1WB=intersect(indx1,index_WB); + indx2WB=intersect(indx2,index_WB); + m=max(numel(indx1WB),numel(indx2WB)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx1WB),1)=stat_mat2(indx1WB,7); + Y(1:numel(indx2WB),1)=stat_mat2(indx2WB,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{13,count}=tstat_; + p_mat{13,count}=p; + + index_BB=intersect(index_boys,index_B); + indx1BB=intersect(indx1,index_BB); + indx2BB=intersect(indx2,index_BB); + m=max(numel(indx1BB),numel(indx2BB)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx1BB),1)=stat_mat2(indx1BB,7); + Y(1:numel(indx2BB),1)=stat_mat2(indx2BB,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{14,count}=tstat_; + p_mat{14,count}=p; + + index_HB=intersect(index_boys,index_H); + indx1HB=intersect(indx1,index_HB); + indx2HB=intersect(indx2,index_HB); + m=max(numel(indx1HB),numel(indx2HB)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx1HB),1)=stat_mat2(indx1HB,7); + Y(1:numel(indx2HB),1)=stat_mat2(indx2HB,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{15,count}=tstat_; + p_mat{15,count}=p; + + index_OB=intersect(index_boys,index_O); + indx1OB=intersect(indx1,index_OB); + indx2OB=intersect(indx2,index_OB); + m=max(numel(indx1OB),numel(indx2OB)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx1OB),1)=stat_mat2(indx1OB,7); + Y(1:numel(indx2OB),1)=stat_mat2(indx2OB,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{16,count}=tstat_; + p_mat{16,count}=p; + + index_9=find(stat_mat2(:,6)==1); + indx19=intersect(indx1,index_9); + indx29=intersect(indx2,index_9); + m=max(numel(indx19),numel(indx29)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx19))=stat_mat2(indx19,7); + Y(1:numel(indx29),1)=stat_mat2(indx29,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{17,count}=tstat_; + p_mat{17,count}=p; + + index_10=find(stat_mat2(:,6)==2); + indx110=intersect(indx1,index_10); + indx210=intersect(indx2,index_10); + m=max(numel(indx110),numel(indx210)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx110))=stat_mat2(indx110,7); + Y(1:numel(indx210),1)=stat_mat2(indx210,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{20,count}=tstat_; + p_mat{20,count}=p; + + index_11=find(stat_mat2(:,6)==3); + indx111=intersect(indx1,index_11); + indx211=intersect(indx2,index_11); + m=max(numel(indx111),numel(indx211)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx111))=stat_mat2(indx111,7); + Y(1:numel(indx211),1)=stat_mat2(indx211,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{23,count}=tstat_; + p_mat{23,count}=p; + + index_12=find(stat_mat2(:,6)==4); + indx112=intersect(indx1,index_12); + indx212=intersect(indx2,index_12); + m=max(numel(indx112),numel(indx212)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx112))=stat_mat2(indx112,7); + Y(1:numel(indx212),1)=stat_mat2(indx212,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{26,count}=tstat_; + p_mat{26,count}=p; + + %girls + index_9G=intersect(index_9, index_girls); + indx19=intersect(indx1,index_9G); + indx29=intersect(indx2,index_9G); + m=max(numel(indx19),numel(indx29)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx19))=stat_mat2(indx19,7); + Y(1:numel(indx29),1)=stat_mat2(indx29,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{19,count}=tstat_; + p_mat{19,count}=p; + + index_10G=intersect(index_10, index_girls); + indx110=intersect(indx1,index_10G); + indx210=intersect(indx2,index_10G); + m=max(numel(indx110),numel(indx210)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx110))=stat_mat2(indx110,7); + Y(1:numel(indx210),1)=stat_mat2(indx210,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{22,count}=tstat_; + p_mat{22,count}=p; + + index_11G=intersect(index_11, index_girls); + indx111=intersect(indx1,index_11G); + indx211=intersect(indx2,index_11G); + m=max(numel(indx111),numel(indx211)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx111))=stat_mat2(indx111,7); + Y(1:numel(indx211),1)=stat_mat2(indx211,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{25,count}=tstat_; + p_mat{25,count}=p; + + index_12G=intersect(index_12, index_girls); + indx112=intersect(indx1,index_12G); + indx212=intersect(indx2,index_12G); + m=max(numel(indx112),numel(indx212)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx112))=stat_mat2(indx112,7); + Y(1:numel(indx212),1)=stat_mat2(indx212,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{28,count}=tstat_; + p_mat{28,count}=p; + + %boys + index_9B=intersect(index_9, index_boys); + indx19=intersect(indx1,index_9B); + indx29=intersect(indx2,index_9B); + m=max(numel(indx19),numel(indx29)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx19))=stat_mat2(indx19,7); + Y(1:numel(indx29),1)=stat_mat2(indx29,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{18,count}=tstat_; + p_mat{18,count}=p; + + index_10B=intersect(index_10, index_boys); + indx110=intersect(indx1,index_10B); + indx210=intersect(indx2,index_10B); + m=max(numel(indx110),numel(indx210)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx110))=stat_mat2(indx110,7); + Y(1:numel(indx210),1)=stat_mat2(indx210,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{21,count}=tstat_; + p_mat{21,count}=p; + + index_11B=intersect(index_11, index_boys); + indx111=intersect(indx1,index_11B); + indx211=intersect(indx2,index_11B); + m=max(numel(indx111),numel(indx211)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx111))=stat_mat2(indx111,7); + Y(1:numel(indx211),1)=stat_mat2(indx211,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{24,count}=tstat_; + p_mat{24,count}=p; + + index_12B=intersect(index_12, index_boys); + indx112=intersect(indx1,index_12B); + indx212=intersect(indx2,index_12B); + m=max(numel(indx112),numel(indx212)); + X=nan(m,1); + Y=nan(m,1); + X(1:numel(indx112))=stat_mat2(indx112,7); + Y(1:numel(indx212),1)=stat_mat2(indx212,7); + [h,p,c, STAT]=ttest(X,Y); + tstat_=STAT.tstat; + t_mat{27,count}=tstat_; + p_mat{27,count}=p; + + count=count+1; + y=y+1; + end +end + + diff --git a/prescriptiondrugs/OR_clustergram_ques_RC_Q79.m b/prescriptiondrugs/archive/OR_clustergram_ques_RC_Q79.m similarity index 100% rename from prescriptiondrugs/OR_clustergram_ques_RC_Q79.m rename to prescriptiondrugs/archive/OR_clustergram_ques_RC_Q79.m diff --git a/prescriptiondrugs/OR_clustergram_ques_RC_Q79_grade.m b/prescriptiondrugs/archive/OR_clustergram_ques_RC_Q79_grade.m similarity index 100% rename from prescriptiondrugs/OR_clustergram_ques_RC_Q79_grade.m rename to prescriptiondrugs/archive/OR_clustergram_ques_RC_Q79_grade.m diff --git a/prescriptiondrugs/PD_drugs_OR.m b/prescriptiondrugs/archive/PD_drugs_OR.m similarity index 100% rename from prescriptiondrugs/PD_drugs_OR.m rename to prescriptiondrugs/archive/PD_drugs_OR.m diff --git a/prescriptiondrugs/PO_demographics_figures.m b/prescriptiondrugs/archive/PO_demographics_figures.m similarity index 100% rename from prescriptiondrugs/PO_demographics_figures.m rename to prescriptiondrugs/archive/PO_demographics_figures.m diff --git a/prescriptiondrugs/PO_demographics_figures_more_than_20.m b/prescriptiondrugs/archive/PO_demographics_figures_more_than_20.m similarity index 100% rename from prescriptiondrugs/PO_demographics_figures_more_than_20.m rename to prescriptiondrugs/archive/PO_demographics_figures_more_than_20.m diff --git a/prescriptiondrugs/PO_heroin_HM_2009_2011.m b/prescriptiondrugs/archive/PO_heroin_HM_2009_2011.m similarity index 100% rename from prescriptiondrugs/PO_heroin_HM_2009_2011.m rename to prescriptiondrugs/archive/PO_heroin_HM_2009_2011.m diff --git a/prescriptiondrugs/PO_heroin_venns.m b/prescriptiondrugs/archive/PO_heroin_venns.m similarity index 100% rename from prescriptiondrugs/PO_heroin_venns.m rename to prescriptiondrugs/archive/PO_heroin_venns.m diff --git a/prescriptiondrugs/PO_use.m b/prescriptiondrugs/archive/PO_use.m similarity index 100% rename from prescriptiondrugs/PO_use.m rename to prescriptiondrugs/archive/PO_use.m diff --git a/prescriptiondrugs/PO_use_more20.m b/prescriptiondrugs/archive/PO_use_more20.m similarity index 100% rename from prescriptiondrugs/PO_use_more20.m rename to prescriptiondrugs/archive/PO_use_more20.m diff --git a/prescriptiondrugs/create_RR_OR_mat_RC_PresDrugs.m b/prescriptiondrugs/archive/create_RR_OR_mat_RC_PresDrugs.m similarity index 100% rename from prescriptiondrugs/create_RR_OR_mat_RC_PresDrugs.m rename to prescriptiondrugs/archive/create_RR_OR_mat_RC_PresDrugs.m diff --git a/prescriptiondrugs/create_RR_OR_mat_RC_PresDrugs_20.m b/prescriptiondrugs/archive/create_RR_OR_mat_RC_PresDrugs_20.m similarity index 100% rename from prescriptiondrugs/create_RR_OR_mat_RC_PresDrugs_20.m rename to prescriptiondrugs/archive/create_RR_OR_mat_RC_PresDrugs_20.m diff --git a/prescriptiondrugs/create_RR_OR_mat_RC_PresDrugs_grade.m b/prescriptiondrugs/archive/create_RR_OR_mat_RC_PresDrugs_grade.m similarity index 100% rename from prescriptiondrugs/create_RR_OR_mat_RC_PresDrugs_grade.m rename to prescriptiondrugs/archive/create_RR_OR_mat_RC_PresDrugs_grade.m diff --git a/prescriptiondrugs/create_RR_OR_mat_RC_PresDrugs_less_than_20.m b/prescriptiondrugs/archive/create_RR_OR_mat_RC_PresDrugs_less_than_20.m similarity index 100% rename from prescriptiondrugs/create_RR_OR_mat_RC_PresDrugs_less_than_20.m rename to prescriptiondrugs/archive/create_RR_OR_mat_RC_PresDrugs_less_than_20.m diff --git a/prescriptiondrugs/create_raw_OR_PO.m b/prescriptiondrugs/archive/create_raw_OR_PO.m similarity index 100% rename from prescriptiondrugs/create_raw_OR_PO.m rename to prescriptiondrugs/archive/create_raw_OR_PO.m diff --git a/prescriptiondrugs/drugs_PD_combo.m b/prescriptiondrugs/archive/drugs_PD_combo.m similarity index 100% rename from prescriptiondrugs/drugs_PD_combo.m rename to prescriptiondrugs/archive/drugs_PD_combo.m diff --git a/prescriptiondrugs/make_total_graphs_drugs1.m b/prescriptiondrugs/archive/make_total_graphs_drugs1.m similarity index 100% rename from prescriptiondrugs/make_total_graphs_drugs1.m rename to prescriptiondrugs/archive/make_total_graphs_drugs1.m diff --git a/prescriptiondrugs/make_total_graphs_drugs2.m b/prescriptiondrugs/archive/make_total_graphs_drugs2.m similarity index 100% rename from prescriptiondrugs/make_total_graphs_drugs2.m rename to prescriptiondrugs/archive/make_total_graphs_drugs2.m diff --git a/prescriptiondrugs/make_total_graphs_drugs3.m b/prescriptiondrugs/archive/make_total_graphs_drugs3.m similarity index 100% rename from prescriptiondrugs/make_total_graphs_drugs3.m rename to prescriptiondrugs/archive/make_total_graphs_drugs3.m diff --git a/prescriptiondrugs/make_total_graphs_drugs4.m b/prescriptiondrugs/archive/make_total_graphs_drugs4.m similarity index 100% rename from prescriptiondrugs/make_total_graphs_drugs4.m rename to prescriptiondrugs/archive/make_total_graphs_drugs4.m diff --git a/prescriptiondrugs/make_total_graphs_drugs5.m b/prescriptiondrugs/archive/make_total_graphs_drugs5.m similarity index 100% rename from prescriptiondrugs/make_total_graphs_drugs5.m rename to prescriptiondrugs/archive/make_total_graphs_drugs5.m diff --git a/prescriptiondrugs/make_total_graphs_drugs_forPO.m b/prescriptiondrugs/archive/make_total_graphs_drugs_forPO.m similarity index 100% rename from prescriptiondrugs/make_total_graphs_drugs_forPO.m rename to prescriptiondrugs/archive/make_total_graphs_drugs_forPO.m diff --git a/prescriptiondrugs/archive/rotateXLabels.m b/prescriptiondrugs/archive/rotateXLabels.m new file mode 100644 index 0000000..c9650e6 --- /dev/null +++ b/prescriptiondrugs/archive/rotateXLabels.m @@ -0,0 +1,415 @@ +function hh = rotateXLabels( ax, angle, varargin ) +%rotateXLabels: rotate any xticklabels +% +% hh = rotateXLabels(ax,angle) rotates all XLabels on axes AX by an angle +% ANGLE (in degrees). Handles to the resulting text objects are returned +% in HH. +% +% hh = rotateXLabels(ax,angle,param,value,...) also allows one or more +% optional parameters to be specified. Possible parameters are: +% 'MaxStringLength' The maximum length of label to show (default inf) +% +% Examples: +% >> bar( hsv(5)+0.05 ) +% >> days = {'Monday','Tuesday','Wednesday','Thursday','Friday'}; +% >> set( gca(), 'XTickLabel', days ) +% >> rotateXLabels( gca(), 45 ) +% +% See also: GCA, BAR + +% Copyright 2006-2013 The MathWorks Ltd. + +error( nargchk( 2, inf, nargin ) ); +if ~isnumeric( angle ) || ~isscalar( angle ) + error( 'RotateXLabels:BadAngle', 'Parameter ANGLE must be a scalar angle in degrees' ) +end +angle = mod( angle, 360 ); + +[maxStringLength] = parseInputs( varargin{:} ); + +% Get the existing label texts and clear them +[vals, labels] = findAndClearExistingLabels( ax, maxStringLength ); + +% Create the new label texts +h = createNewLabels( ax, vals, labels, angle ); + +% Reposition the axes itself to leave space for the new labels +repositionAxes( ax ); + +% If an X-label is present, move it too +repositionXLabel( ax ); + +% Store angle +setappdata( ax, 'RotateXLabelsAngle', angle ); + +% Only send outputs if requested +if nargout + hh = h; +end + +%-------------------------------------------------------------------------% + function [maxStringLength] = parseInputs( varargin ) + % Parse optional inputs + maxStringLength = inf; + if nargin > 0 + params = varargin(1:2:end); + values = varargin(2:2:end); + if numel( params ) ~= numel( values ) + error( 'RotateXLabels:BadSyntax', 'Optional arguments must be specified as parameter-value pairs.' ); + end + if any( ~cellfun( 'isclass', params, 'char' ) ) + error( 'RotateXLabels:BadSyntax', 'Optional argument names must be specified as strings.' ); + end + for pp=1:numel( params ) + switch upper( params{pp} ) + case 'MAXSTRINGLENGTH' + maxStringLength = values{pp}; + + otherwise + error( 'RotateXLabels:BadParam', 'Optional parameter ''%s'' not recognised.', params{pp} ); + end + end + end + end % parseInputs +%-------------------------------------------------------------------------% + function [vals,labels] = findAndClearExistingLabels( ax, maxStringLength ) + % Get the current tick positions so that we can place our new labels + vals = get( ax, 'XTick' ); + + % Now determine the labels. We look first at for previously rotated labels + % since if there are some the actual labels will be empty. + ex = findall( ax, 'Tag', 'RotatedXTickLabel' ); + if isempty( ex ) + % Store the positions and labels + labels = get( ax, 'XTickLabel' ); + if isempty( labels ) + % No labels! + return + else + if ~iscell(labels) + labels = cellstr(labels); + end + end + % Clear existing labels so that xlabel is in the right position + set( ax, 'XTickLabel', {}, 'XTickMode', 'Manual' ); + setappdata( ax, 'OriginalXTickLabels', labels ); + else + % Labels have already been rotated, so capture them + labels = getappdata( ax, 'OriginalXTickLabels' ); + set(ex, 'DeleteFcn', []); + delete(ex); + end + % Limit the length, if requested + if isfinite( maxStringLength ) + for ll=1:numel( labels ) + if length( labels{ll} ) > maxStringLength + labels{ll} = labels{ll}(1:maxStringLength); + end + end + end + + end % findAndClearExistingLabels +%-------------------------------------------------------------------------% + function restoreDefaultLabels( ax ) + % Restore the default axis behavior + removeListeners( ax ); + + % Try to restore the tick marks and labels + set( ax, 'XTickMode', 'auto', 'XTickLabelMode', 'auto' ); + rmappdata( ax, 'OriginalXTickLabels' ); + + % Try to restore the axes position + if isappdata( ax, 'OriginalAxesPosition' ) + set( ax, 'Position', getappdata( ax, 'OriginalAxesPosition' ) ); + rmappdata( ax, 'OriginalAxesPosition' ); + end + end +%-------------------------------------------------------------------------% + function textLabels = createNewLabels( ax, vals, labels, angle ) + % Work out the ticklabel positions + zlim = get(ax,'ZLim'); + z = zlim(1); + + % We want to work in normalised coords, but this doesn't print + % correctly. Instead we have to work in data units even though it + % makes positioning hard. + ylim = get( ax, 'YLim' ); + if strcmpi( get( ax, 'XAxisLocation' ), 'Top' ) + y = ylim(2); + else + y = ylim(1); + end + + % Now create new text objects in similar positions. + textLabels = -1*ones( numel( vals ), 1 ); + for ll=1:numel(vals) + textLabels(ll) = text( ... + 'Units', 'Data', ... + 'Position', [vals(ll), y, z], ... + 'String', labels{ll}, ... + 'Parent', ax, ... + 'Clipping', 'off', ... + 'Rotation', angle, ... + 'Tag', 'RotatedXTickLabel', ... + 'UserData', vals(ll)); + end + % So that we can respond to CLA and CLOSE, attach a delete + % callback. We only attach it to one label to save massive numbers + % of callbacks during axes shut-down. + set(textLabels(end), 'DeleteFcn', @onTextLabelDeleted); + + % Now copy font properties into the texts + updateFont(); + % Update the alignment of the text + updateAlignment(); + + end % createNewLabels + +%-------------------------------------------------------------------------% + function repositionAxes( ax ) + % Reposition the axes so that there's room for the labels + % Note that we only do this if the OuterPosition is the thing being + % controlled + if ~strcmpi( get( ax, 'ActivePositionProperty' ), 'OuterPosition' ) + return; + end + + % Work out the maximum height required for the labels + labelHeight = getLabelHeight(ax); + + % Remove listeners while we mess around with things, otherwise we'll + % trigger redraws recursively + removeListeners( ax ); + + % Change to normalized units for the position calculation + oldUnits = get( ax, 'Units' ); + set( ax, 'Units', 'Normalized' ); + + % Not sure why, but the extent seems to be proportional to the height of the axes. + % Correct that now. + set( ax, 'ActivePositionProperty', 'Position' ); + pos = get( ax, 'Position' ); + axesHeight = pos(4); + % Make sure we don't adjust away the axes entirely! + heightAdjust = min( (axesHeight*0.9), labelHeight*axesHeight ); + + % Move the axes + if isappdata( ax, 'OriginalAxesPosition' ) + pos = getappdata( ax, 'OriginalAxesPosition' ); + else + pos = get(ax,'Position'); + setappdata( ax, 'OriginalAxesPosition', pos ); + end + if strcmpi( get( ax, 'XAxisLocation' ), 'Bottom' ) + % Move it up and reduce the height + set( ax, 'Position', pos+[0 heightAdjust 0 -heightAdjust] ) + else + % Just reduce the height + set( ax, 'Position', pos+[0 0 0 -heightAdjust] ) + end + set( ax, 'Units', oldUnits ); + set( ax, 'ActivePositionProperty', 'OuterPosition' ); + + % Make sure we find out if axes properties are changed + addListeners( ax ); + + end % repositionAxes + +%-------------------------------------------------------------------------% + function repositionXLabel( ax ) + % Try to work out where to put the xlabel + removeListeners( ax ); + labelHeight = getLabelHeight(ax); + + % Use the new max extent to move the xlabel. We may also need to + % move the title + xlab = get(ax,'XLabel'); + titleh = get( ax, 'Title' ); + set( [xlab,titleh], 'Units', 'Normalized' ); + if strcmpi( get( ax, 'XAxisLocation' ), 'Top' ) + titleExtent = get( xlab, 'Extent' ); + set( xlab, 'Position', [0.5 1+labelHeight-titleExtent(4) 0] ); + set( titleh, 'Position', [0.5 1+labelHeight 0] ); + else + set( xlab, 'Position', [0.5 -labelHeight 0] ); + set( titleh, 'Position', [0.5 1 0] ); + end + addListeners( ax ); + end % repositionXLabel + +%-------------------------------------------------------------------------% + function height = getLabelHeight(ax) + height = 0; + textLabels = findall( ax, 'Tag', 'RotatedXTickLabel' ); + if isempty(textLabels) + return; + end + oldUnits = get( textLabels(1), 'Units' ); + set( textLabels, 'Units', 'Normalized' ); + for ll=1:numel(vals) + ext = get( textLabels(ll), 'Extent' ); + if ext(4) > height + height = ext(4); + end + end + set( textLabels, 'Units', oldUnits ); + end % getLabelExtent + +%-------------------------------------------------------------------------% + function updateFont() + % Update the rotated text fonts when the axes font changes + properties = { + 'FontName' + 'FontSize' + 'FontAngle' + 'FontWeight' + 'FontUnits' + }; + propertyValues = get( ax, properties ); + textLabels = findall( ax, 'Tag', 'RotatedXTickLabel' ); + set( textLabels, properties, propertyValues ); + end % updateFont + + function updateAlignment() + textLabels = findall( ax, 'Tag', 'RotatedXTickLabel' ); + angle = get( textLabels(1), 'Rotation' ); + % Depending on the angle, we may need to change the alignment. We change + % alignments within 5 degrees of each 90 degree orientation. + if strcmpi( get( ax, 'XAxisLocation' ), 'Top' ) + if 0 <= angle && angle < 5 + set( textLabels, 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Bottom' ); + elseif 5 <= angle && angle < 85 + set( textLabels, 'HorizontalAlignment', 'Left', 'VerticalAlignment', 'Bottom' ); + elseif 85 <= angle && angle < 95 + set( textLabels, 'HorizontalAlignment', 'Left', 'VerticalAlignment', 'Middle' ); + elseif 95 <= angle && angle < 175 + set( textLabels, 'HorizontalAlignment', 'Left', 'VerticalAlignment', 'Top' ); + elseif 175 <= angle && angle < 185 + set( textLabels, 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Top' ); + elseif 185 <= angle && angle < 265 + set( textLabels, 'HorizontalAlignment', 'Right', 'VerticalAlignment', 'Top' ); + elseif 265 <= angle && angle < 275 + set( textLabels, 'HorizontalAlignment', 'Right', 'VerticalAlignment', 'Middle' ); + elseif 275 <= angle && angle < 355 + set( textLabels, 'HorizontalAlignment', 'Right', 'VerticalAlignment', 'Bottom' ); + else % 355-360 + set( textLabels, 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Bottom' ); + end + else + if 0 <= angle && angle < 5 + set( textLabels, 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Top' ); + elseif 5 <= angle && angle < 85 + set( textLabels, 'HorizontalAlignment', 'Right', 'VerticalAlignment', 'Top' ); + elseif 85 <= angle && angle < 95 + set( textLabels, 'HorizontalAlignment', 'Right', 'VerticalAlignment', 'Middle' ); + elseif 95 <= angle && angle < 175 + set( textLabels, 'HorizontalAlignment', 'Right', 'VerticalAlignment', 'Bottom' ); + elseif 175 <= angle && angle < 185 + set( textLabels, 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Bottom' ); + elseif 185 <= angle && angle < 265 + set( textLabels, 'HorizontalAlignment', 'Left', 'VerticalAlignment', 'Bottom' ); + elseif 265 <= angle && angle < 275 + set( textLabels, 'HorizontalAlignment', 'Left', 'VerticalAlignment', 'Middle' ); + elseif 275 <= angle && angle < 355 + set( textLabels, 'HorizontalAlignment', 'Left', 'VerticalAlignment', 'Top' ); + else % 355-360 + set( textLabels, 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Top' ); + end + end + end + +%-------------------------------------------------------------------------% + function onAxesFontChanged( ~, ~ ) + updateFont(); + repositionAxes( ax ); + repositionXLabel( ax ); + end % onAxesFontChanged + +%-------------------------------------------------------------------------% + function onAxesPositionChanged( ~, ~ ) + % We need to accept the new position, so remove the appdata before + % redrawing + if isappdata( ax, 'OriginalAxesPosition' ) + rmappdata( ax, 'OriginalAxesPosition' ); + end + if isappdata( ax, 'OriginalXLabelPosition' ) + rmappdata( ax, 'OriginalXLabelPosition' ); + end + repositionAxes( ax ); + repositionXLabel( ax ); + end % onAxesPositionChanged + +%-------------------------------------------------------------------------% + function onXAxisLocationChanged( ~, ~ ) + updateAlignment(); + repositionAxes( ax ); + repositionXLabel( ax ); + end % onXAxisLocationChanged + +%-------------------------------------------------------------------------% + function onAxesLimitsChanged( ~, ~ ) + % The limits have moved, so make sure the labels are still ok + textLabels = findall( ax, 'Tag', 'RotatedXTickLabel' ); + xlim = get( ax, 'XLim' ); + ylim = get( ax, 'YLim' ); + if strcmpi( get( ax, 'XAxisLocation'), 'Bottom' ) + pos = [0 ylim(1)]; + else + pos = [0 ylim(2)]; + end + for tt=1:numel( textLabels ) + xval = get( textLabels(tt), 'UserData' ); + pos(1) = xval; + % If the tick is off the edge, make it invisible + if xvalxlim(2) + set( textLabels(tt), 'Visible', 'off', 'Position', pos ) + elseif ~strcmpi( get( textLabels(tt), 'Visible' ), 'on' ) + set( textLabels(tt), 'Visible', 'on', 'Position', pos ) + else + % Just set the position + set( textLabels(tt), 'Position', pos ); + end + end + + repositionXLabel( ax ); + end % onAxesPositionChanged + +%-------------------------------------------------------------------------% + function onTextLabelDeleted( ~, ~ ) + % The final text label has been deleted. This is likely from a + % "cla" or "close" call, so we should remove all of our dirty + % hacks. + restoreDefaultLabels(ax); + end + +%-------------------------------------------------------------------------% + function addListeners( ax ) + % Create listeners. We store the array of listeners in the axes to make + % sure that they have the same life-span as the axes they are listening to. + axh = handle( ax ); + listeners = [ + handle.listener( axh, findprop( axh, 'FontName' ), 'PropertyPostSet', @onAxesFontChanged ) + handle.listener( axh, findprop( axh, 'FontSize' ), 'PropertyPostSet', @onAxesFontChanged ) + handle.listener( axh, findprop( axh, 'FontWeight' ), 'PropertyPostSet', @onAxesFontChanged ) + handle.listener( axh, findprop( axh, 'FontAngle' ), 'PropertyPostSet', @onAxesFontChanged ) + handle.listener( axh, findprop( axh, 'FontUnits' ), 'PropertyPostSet', @onAxesFontChanged ) + handle.listener( axh, findprop( axh, 'OuterPosition' ), 'PropertyPostSet', @onAxesPositionChanged ) + handle.listener( axh, findprop( axh, 'XLim' ), 'PropertyPostSet', @onAxesLimitsChanged ) + handle.listener( axh, findprop( axh, 'YLim' ), 'PropertyPostSet', @onAxesLimitsChanged ) + handle.listener( axh, findprop( axh, 'XAxisLocation' ), 'PropertyPostSet', @onXAxisLocationChanged ) + ]; + setappdata( ax, 'RotateXLabelsListeners', listeners ); + end % addListeners + +%-------------------------------------------------------------------------% + function removeListeners( ax ) + % Rempove any property listeners whilst we are fiddling with the axes + if isappdata( ax, 'RotateXLabelsListeners' ) + delete( getappdata( ax, 'RotateXLabelsListeners' ) ); + rmappdata( ax, 'RotateXLabelsListeners' ); + end + end % removeListeners + + + +end % EOF \ No newline at end of file diff --git a/prescriptiondrugs/tightfig.m b/prescriptiondrugs/archive/tightfig.m similarity index 100% rename from prescriptiondrugs/tightfig.m rename to prescriptiondrugs/archive/tightfig.m diff --git a/prescriptiondrugs/use_frequency.m b/prescriptiondrugs/archive/use_frequency.m similarity index 100% rename from prescriptiondrugs/use_frequency.m rename to prescriptiondrugs/archive/use_frequency.m diff --git a/prescriptiondrugs/heroin_freqv2.m b/prescriptiondrugs/heroin_freqv2.m new file mode 100644 index 0000000..ff0d2ae --- /dev/null +++ b/prescriptiondrugs/heroin_freqv2.m @@ -0,0 +1,196 @@ + +cd .. +cd .. +cd data +cd Controls_061514 +sex=importdata('sex-NaN.txt', '\t'); +race=importdata('race-NaN.txt', '\t'); +weight=importdata('weights-NaN.txt','\t'); +grade=importdata('grade-NaN.txt','\t'); +weight=weight(5:7,:); +race=race(5:7,:); +sex=sex(5:7,:); +grade=grade(5:7,:); +cd .. +cd results_061514 +cd NaN +question_mat=importdata('Q27--NaN.txt', '\t'); +[r,c]=size(question_mat); +%question_mat=question_mat(:,2:c); +question_mat(:,1)=9; +question_mat=question_mat(5:7,:); +cd .. +cd .. +cd .. + +cd programs +cd prescriptiondrugs +cd results + +[r,c]=size(question_mat); + +for i=1:r +% total(i)=TOTAL(i,1); + index_yes{i}=find(question_mat(i,:)==1); + index_girls{i}=find(sex(i,:)==1); + index_boys{i}=find(sex(i,:)==2); + index_W{i}=find(race(i,:)== 1 ); + index_B{i}=find(race(i,:)== 2 ); + index_H{i}=find(race(i,:)== 3 ); + index_O{i}=find(race(i,:)== 4 ); + index_9{i}=find(grade(i,:)== 1 ); + index_10{i}=find(grade(i,:)== 2 ); + index_11{i}=find(grade(i,:)== 3 ); + index_12{i}=find(grade(i,:)== 4 ); +end +% ^for each desired variable we created an array of the indexes (ie. students) who are a "yes" in that variable + + +for i=1:r + index_missQ{i}=find(question_mat(i,:)==9); %students who didn't answer the Q + index_nomiss{i}=find(question_mat(i,:)==0 | question_mat(i,:)==1); %answers that were NOT missing (ie. 0's and 1's / no's and yes's) +end + +for i=1:r + missQ(i)=length(index_missQ{i}); %number of students who answered the question each year +end + + +for i=1:r + index_total_b{i}=intersect(index_nomiss{i},index_boys{i}); %index of all boys who answered + index_total_g{i}=intersect(index_nomiss{i},index_girls{i}); %index of all girls who answered +end + +% totals of each race/sex/combo: (for percentages- to compare those who said yes to total questioned) +for i=1:r + w=weight(i,:)'; + total_ans(i)=nansum(w(index_nomiss{i})); + total_girls(i)=nansum(w(index_total_g{i})); %total # of girls who answered + total_boys(i)=nansum(w(index_total_b{i})); %total number of boys who answered + total_W{i}=nansum(w(intersect(index_nomiss{i}, index_W{i}))); %total # of white students who answered + total_B{i}=nansum(w(intersect(index_nomiss{i}, index_B{i}))); %total # of black students who answered + total_H{i}=nansum(w(intersect(index_nomiss{i}, index_H{i}))); %total # of hispanic students who answered + total_O{i}=nansum(w(intersect(index_nomiss{i}, index_O{i}))); %total # of "other" students who answered + total_Wb(i)=nansum(w(intersect(index_total_b{i},index_W{i}))); + total_Wg(i)=nansum(w(intersect(index_total_g{i},index_W{i}))); + total_Bb(i)=nansum(w(intersect(index_total_b{i},index_B{i}))); + total_Bg(i)=nansum(w(intersect(index_total_g{i},index_B{i}))); + total_Hb(i)=nansum(w(intersect(index_total_b{i},index_H{i}))); + total_Hg(i)=nansum(w(intersect(index_total_g{i},index_H{i}))); + total_Ob(i)=nansum(w(intersect(index_total_b{i},index_O{i}))); + total_Og(i)=nansum(w(intersect(index_total_g{i},index_O{i}))); + total_9(i)=nansum(w(intersect((index_9{i}),index_nomiss{i}))); + total_10(i)=nansum(w(intersect((index_10{i}),index_nomiss{i}))); + total_11(i)=nansum(w(intersect((index_11{i}),index_nomiss{i}))); + total_12(i)=nansum(w(intersect((index_12{i}),index_nomiss{i}))); + total_9G(i)=nansum(w(intersect(index_9{i},index_total_g{i}))); + total_10G(i)=nansum(w(intersect(index_10{i},index_total_g{i}))); + total_11G(i)=nansum(w(intersect(index_11{i},index_total_g{i}))); + total_12G(i)=nansum(w(intersect(index_12{i},index_total_g{i}))); + total_9B(i)=nansum(w(intersect(index_9{i},index_total_b{i}))); + total_10B(i)=nansum(w(intersect(index_10{i},index_total_b{i}))); + total_11B(i)=nansum(w(intersect(index_11{i},index_total_b{i}))); + total_12B(i)=nansum(w(intersect(index_12{i},index_total_b{i}))); +end + +% %yes-es: +for i=1:r + w=weight(i,:)'; + index_yesgirls{i}=intersect(index_yes{i},index_girls{i}); + index_yesboys{i}=intersect(index_yes{i},index_boys{i}); + yes_girls(i)=nansum(w(index_yesgirls{i})); + yes_boys(i)=nansum(w(index_yesboys{i})); + yes_W(i)=nansum(w(intersect(index_yes{i}, index_W{i}))); + yes_B(i)=nansum(w(intersect(index_yes{i}, index_B{i}))); + yes_H(i)=nansum(w(intersect(index_yes{i}, index_H{i}))); + yes_O(i)=nansum(w(intersect(index_yes{i}, index_O{i}))); + yes_WG(i)=nansum(w(intersect(index_yesgirls{i},index_W{i}))); + yes_BG(i)=nansum(w(intersect(index_yesgirls{i},index_B{i}))); + yes_HG(i)=nansum(w(intersect(index_yesgirls{i},index_H{i}))); + yes_OG(i)=nansum(w(intersect(index_yesgirls{i},index_O{i}))); + yes_WB(i)=nansum(w(intersect(index_yesboys{i},index_W{i}))); + yes_BB(i)=nansum(w(intersect(index_yesboys{i},index_B{i}))); + yes_HB(i)=nansum(w(intersect(index_yesboys{i},index_H{i}))); + yes_OB(i)=nansum(w(intersect(index_yesboys{i},index_O{i}))); + total_yes(i)=nansum(w(index_yes{i})); + yes_9(i)=nansum(w(intersect(index_yes{i},index_9{i}))); + yes_10(i)=nansum(w(intersect(index_yes{i},index_10{i}))); + yes_11(i)=nansum(w(intersect(index_yes{i},index_11{i}))); + yes_12(i)=nansum(w(intersect(index_yes{i},index_12{i}))); + yes_9B(i)=nansum(w(intersect(index_yesboys{i},index_9{i}))); + yes_10B(i)=nansum(w(intersect(index_yesboys{i},index_10{i}))); + yes_11B(i)=nansum(w(intersect(index_yesboys{i},index_11{i}))); + yes_12B(i)=nansum(w(intersect(index_yesboys{i},index_12{i}))); + yes_9G(i)=nansum(w(intersect(index_yesgirls{i},index_9{i}))); + yes_10G(i)=nansum(w(intersect(index_yesgirls{i},index_10{i}))); + yes_11G(i)=nansum(w(intersect(index_yesgirls{i},index_11{i}))); + yes_12G(i)=nansum(w(intersect(index_yesgirls{i},index_12{i}))); + total_w(i)=total_W{i}; + total_b(i)=total_B{i}; + total_h(i)=total_H{i}; + total_o(i)=total_O{i}; +end + +%put them all into a matrix as percent values: +per_mat = zeros(16,r); +for i=1:r + per_mat(1,i)=total_yes(i)/total_ans(i)*100; %total + per_mat(2,i)=yes_boys(i)/total_boys(i)*100; %boys + per_mat(3,i)=yes_girls(i)/total_girls(i)*100; %girls + per_mat(4,i)=yes_W(i)/total_w(i)*100; %whites + per_mat(5,i)=yes_B(i)/total_b(i)*100; %blacks + per_mat(6,i)=yes_H(i)/total_h(i)*100; %hispanics + per_mat(7,i)=yes_O(i)/total_o(i)*100; %other + per_mat(8, i)=yes_WB(i)/total_Wb(i)*100; %WB + per_mat(9, i)=yes_WG(i)/total_Wg(i)*100; %WG + per_mat(10, i)=yes_BB(i)/total_Bb(i)*100; %BB + per_mat(11, i)=yes_BG(i)/total_Bg(i)*100; %BG + per_mat(12, i)=yes_HB(i)/total_Hb(i)*100; %HB + per_mat(13, i)=yes_HG(i)/total_Hg(i)*100; %HG + per_mat(14, i)=yes_OB(i)/total_Ob(i)*100; %OB + per_mat(15, i)=yes_OG(i)/total_Og(i)*100; %OG + per_mat(16, i)=yes_9(i)/total_9(i)*100; + per_mat(17, i)=yes_9B(i)/total_9B(i)*100; + per_mat(18, i)=yes_9G(i)/total_9G(i)*100; + per_mat(19, i)=yes_10(i)/total_10(i)*100; + per_mat(20, i)=yes_10B(i)/total_10B(i)*100; + per_mat(21, i)=yes_10G(i)/total_10G(i)*100; + per_mat(22, i)=yes_11(i)/total_11(i)*100; + per_mat(23, i)=yes_11B(i)/total_11B(i)*100; + per_mat(24, i)=yes_11G(i)/total_11G(i)*100; + per_mat(25, i)=yes_12(i)/total_12(i)*100; + per_mat(26, i)=yes_12B(i)/total_12B(i)*100; + per_mat(27, i)=yes_12G(i)/total_12G(i)*100; +end + +%per_mat=flipud(per_mat); + + label_year={'2009','2011', '2013'}; + label_cell2={'Total', 'Boys', 'Girls', 'W', 'B', 'H', 'O', 'W Boys', 'W Girls', 'B Boys', 'B Girls', 'H Boys', 'H Girls', 'O Boys', 'O Girls', '9th', '9th-Boys', '9th-Girls', '10th','10th-Boys', '10th-Girls', '11th', '11th-Boys', '11th-Girls', '12th', '12th-Boys', '12th-Girls'}; + %per_mat_map(1:15,1:r)=per_mat(1:15,1:r); + %per_mat_map=flipdim(per_mat_map,1); + max_mat=max(max(per_mat)); + if max_mat>75 + M=100; + elseif max_mat>50 + M=75; + elseif max_mat>25 + M=50; + else + M=25; + end + M=10; + %get rid of deimals + per_mat=per_mat*10; + per_mat=round(per_mat); + per_mat=per_mat/10; + h=figure; + title1={'During your life, how many times have you used heroin'; '(also called smack, junk or China White)?'}; + [hImage]=heatmap_rb(per_mat, label_year, label_cell2, 1, M, 0, 'Colormap','money', 'UseLogColormap', false, 'ShowAllTicks',true, 'Colorbar',true,'TextColor','k', 'FontSize', 12); + title (title1, 'FontSize', 12, 'fontWeight','bold'); + set (gca, 'FontSize',12); + + saveas (gcf, 'heroin_heatmap.fig'); %can make pdf, jnp, or jpg + print (gcf, '-dpdf', 'heroin_heatmap.pdf'); + + close all diff --git a/prescriptiondrugs/injected_freqv2.m b/prescriptiondrugs/injected_freqv2.m new file mode 100644 index 0000000..c65b1ce --- /dev/null +++ b/prescriptiondrugs/injected_freqv2.m @@ -0,0 +1,194 @@ + +cd .. +cd .. +cd data +cd Controls_061514 +sex=importdata('sex-NaN.txt', '\t'); +race=importdata('race-NaN.txt', '\t'); +weight=importdata('weights-NaN.txt','\t'); +grade=importdata('grade-NaN.txt','\t'); +weight=weight(5:7,:); +race=race(5:7,:); +sex=sex(5:7,:); +grade=grade(5:7,:); +cd .. +cd results_061514 +cd NaN +question_mat=importdata('Q31--NaN.txt', '\t'); +[r,c]=size(question_mat); +%question_mat=question_mat(:,2:c); +question_mat(:,1)=9; +question_mat=question_mat(5:7,:); +cd .. +cd .. +cd .. +cd programs +cd prescriptiondrugs +cd results +[r,c]=size(question_mat); + +for i=1:r +% total(i)=TOTAL(i,1); + index_yes{i}=find(question_mat(i,:)==1); + index_girls{i}=find(sex(i,:)==1); + index_boys{i}=find(sex(i,:)==2); + index_W{i}=find(race(i,:)== 1 ); + index_B{i}=find(race(i,:)== 2 ); + index_H{i}=find(race(i,:)== 3 ); + index_O{i}=find(race(i,:)== 4 ); + index_9{i}=find(grade(i,:)== 1 ); + index_10{i}=find(grade(i,:)== 2 ); + index_11{i}=find(grade(i,:)== 3 ); + index_12{i}=find(grade(i,:)== 4 ); +end +% ^for each desired variable we created an array of the indexes (ie. students) who are a "yes" in that variable + + +for i=1:r + index_missQ{i}=find(question_mat(i,:)==9); %students who didn't answer the Q + index_nomiss{i}=find(question_mat(i,:)==0 | question_mat(i,:)==1); %answers that were NOT missing (ie. 0's and 1's / no's and yes's) +end + +for i=1:r + missQ(i)=length(index_missQ{i}); %number of students who answered the question each year +end + + +for i=1:r + index_total_b{i}=intersect(index_nomiss{i},index_boys{i}); %index of all boys who answered + index_total_g{i}=intersect(index_nomiss{i},index_girls{i}); %index of all girls who answered +end + +% totals of each race/sex/combo: (for percentages- to compare those who said yes to total questioned) +for i=1:r + w=weight(i,:)'; + total_ans(i)=nansum(w(index_nomiss{i})); + total_girls(i)=nansum(w(index_total_g{i})); %total # of girls who answered + total_boys(i)=nansum(w(index_total_b{i})); %total number of boys who answered + total_W{i}=nansum(w(intersect(index_nomiss{i}, index_W{i}))); %total # of white students who answered + total_B{i}=nansum(w(intersect(index_nomiss{i}, index_B{i}))); %total # of black students who answered + total_H{i}=nansum(w(intersect(index_nomiss{i}, index_H{i}))); %total # of hispanic students who answered + total_O{i}=nansum(w(intersect(index_nomiss{i}, index_O{i}))); %total # of "other" students who answered + total_Wb(i)=nansum(w(intersect(index_total_b{i},index_W{i}))); + total_Wg(i)=nansum(w(intersect(index_total_g{i},index_W{i}))); + total_Bb(i)=nansum(w(intersect(index_total_b{i},index_B{i}))); + total_Bg(i)=nansum(w(intersect(index_total_g{i},index_B{i}))); + total_Hb(i)=nansum(w(intersect(index_total_b{i},index_H{i}))); + total_Hg(i)=nansum(w(intersect(index_total_g{i},index_H{i}))); + total_Ob(i)=nansum(w(intersect(index_total_b{i},index_O{i}))); + total_Og(i)=nansum(w(intersect(index_total_g{i},index_O{i}))); + total_9(i)=nansum(w(intersect((index_9{i}),index_nomiss{i}))); + total_10(i)=nansum(w(intersect((index_10{i}),index_nomiss{i}))); + total_11(i)=nansum(w(intersect((index_11{i}),index_nomiss{i}))); + total_12(i)=nansum(w(intersect((index_12{i}),index_nomiss{i}))); + total_9G(i)=nansum(w(intersect(index_9{i},index_total_g{i}))); + total_10G(i)=nansum(w(intersect(index_10{i},index_total_g{i}))); + total_11G(i)=nansum(w(intersect(index_11{i},index_total_g{i}))); + total_12G(i)=nansum(w(intersect(index_12{i},index_total_g{i}))); + total_9B(i)=nansum(w(intersect(index_9{i},index_total_b{i}))); + total_10B(i)=nansum(w(intersect(index_10{i},index_total_b{i}))); + total_11B(i)=nansum(w(intersect(index_11{i},index_total_b{i}))); + total_12B(i)=nansum(w(intersect(index_12{i},index_total_b{i}))); +end + +% %yes-es: +for i=1:r + w=weight(i,:)'; + index_yesgirls{i}=intersect(index_yes{i},index_girls{i}); + index_yesboys{i}=intersect(index_yes{i},index_boys{i}); + yes_girls(i)=nansum(w(index_yesgirls{i})); + yes_boys(i)=nansum(w(index_yesboys{i})); + yes_W(i)=nansum(w(intersect(index_yes{i}, index_W{i}))); + yes_B(i)=nansum(w(intersect(index_yes{i}, index_B{i}))); + yes_H(i)=nansum(w(intersect(index_yes{i}, index_H{i}))); + yes_O(i)=nansum(w(intersect(index_yes{i}, index_O{i}))); + yes_WG(i)=nansum(w(intersect(index_yesgirls{i},index_W{i}))); + yes_BG(i)=nansum(w(intersect(index_yesgirls{i},index_B{i}))); + yes_HG(i)=nansum(w(intersect(index_yesgirls{i},index_H{i}))); + yes_OG(i)=nansum(w(intersect(index_yesgirls{i},index_O{i}))); + yes_WB(i)=nansum(w(intersect(index_yesboys{i},index_W{i}))); + yes_BB(i)=nansum(w(intersect(index_yesboys{i},index_B{i}))); + yes_HB(i)=nansum(w(intersect(index_yesboys{i},index_H{i}))); + yes_OB(i)=nansum(w(intersect(index_yesboys{i},index_O{i}))); + total_yes(i)=nansum(w(index_yes{i})); + yes_9(i)=nansum(w(intersect(index_yes{i},index_9{i}))); + yes_10(i)=nansum(w(intersect(index_yes{i},index_10{i}))); + yes_11(i)=nansum(w(intersect(index_yes{i},index_11{i}))); + yes_12(i)=nansum(w(intersect(index_yes{i},index_12{i}))); + yes_9B(i)=nansum(w(intersect(index_yesboys{i},index_9{i}))); + yes_10B(i)=nansum(w(intersect(index_yesboys{i},index_10{i}))); + yes_11B(i)=nansum(w(intersect(index_yesboys{i},index_11{i}))); + yes_12B(i)=nansum(w(intersect(index_yesboys{i},index_12{i}))); + yes_9G(i)=nansum(w(intersect(index_yesgirls{i},index_9{i}))); + yes_10G(i)=nansum(w(intersect(index_yesgirls{i},index_10{i}))); + yes_11G(i)=nansum(w(intersect(index_yesgirls{i},index_11{i}))); + yes_12G(i)=nansum(w(intersect(index_yesgirls{i},index_12{i}))); + total_w(i)=total_W{i}; + total_b(i)=total_B{i}; + total_h(i)=total_H{i}; + total_o(i)=total_O{i}; +end + +%put them all into a matrix as percent values: +per_mat = zeros(16,r); +for i=1:r + per_mat(1,i)=total_yes(i)/total_ans(i)*100; %total + per_mat(2,i)=yes_boys(i)/total_boys(i)*100; %boys + per_mat(3,i)=yes_girls(i)/total_girls(i)*100; %girls + per_mat(4,i)=yes_W(i)/total_w(i)*100; %whites + per_mat(5,i)=yes_B(i)/total_b(i)*100; %blacks + per_mat(6,i)=yes_H(i)/total_h(i)*100; %hispanics + per_mat(7,i)=yes_O(i)/total_o(i)*100; %other + per_mat(8, i)=yes_WB(i)/total_Wb(i)*100; %WB + per_mat(9, i)=yes_WG(i)/total_Wg(i)*100; %WG + per_mat(10, i)=yes_BB(i)/total_Bb(i)*100; %BB + per_mat(11, i)=yes_BG(i)/total_Bg(i)*100; %BG + per_mat(12, i)=yes_HB(i)/total_Hb(i)*100; %HB + per_mat(13, i)=yes_HG(i)/total_Hg(i)*100; %HG + per_mat(14, i)=yes_OB(i)/total_Ob(i)*100; %OB + per_mat(15, i)=yes_OG(i)/total_Og(i)*100; %OG + per_mat(16, i)=yes_9(i)/total_9(i)*100; + per_mat(17, i)=yes_9B(i)/total_9B(i)*100; + per_mat(18, i)=yes_9G(i)/total_9G(i)*100; + per_mat(19, i)=yes_10(i)/total_10(i)*100; + per_mat(20, i)=yes_10B(i)/total_10B(i)*100; + per_mat(21, i)=yes_10G(i)/total_10G(i)*100; + per_mat(22, i)=yes_11(i)/total_11(i)*100; + per_mat(23, i)=yes_11B(i)/total_11B(i)*100; + per_mat(24, i)=yes_11G(i)/total_11G(i)*100; + per_mat(25, i)=yes_12(i)/total_12(i)*100; + per_mat(26, i)=yes_12B(i)/total_12B(i)*100; + per_mat(27, i)=yes_12G(i)/total_12G(i)*100; +end + +%per_mat=flipud(per_mat); + + label_year={'2009','2011', '2013'}; + label_cell2={'Total', 'Boys', 'Girls', 'W', 'B', 'H', 'O', 'W Boys', 'W Girls', 'B Boys', 'B Girls', 'H Boys', 'H Girls', 'O Boys', 'O Girls', '9th', '9th-Boys', '9th-Girls', '10th','10th-Boys', '10th-Girls', '11th', '11th-Boys', '11th-Girls', '12th', '12th-Boys', '12th-Girls'}; + %per_mat_map(1:15,1:r)=per_mat(1:15,1:r); + %per_mat_map=flipdim(per_mat_map,1); + max_mat=max(max(per_mat)); + if max_mat>75 + M=100; + elseif max_mat>50 + M=75; + elseif max_mat>25 + M=50; + else + M=25; + end + M=10; + %get rid of deimals + per_mat=per_mat*10; + per_mat=round(per_mat); + per_mat=per_mat/10; + h=figure; + title1={'During your life, how many times have you used'; ' a needle to inject any illegal drug into your body?';}; + [hImage]=heatmap_rb(per_mat, label_year, label_cell2, 1, M, 0, 'Colormap','money', 'UseLogColormap', false, 'ShowAllTicks',true, 'Colorbar',true,'TextColor','k', 'FontSize', 12); + title (title1, 'FontSize', 12, 'fontWeight','bold'); + set (gca, 'FontSize',12); + + saveas (gcf, 'injected_heatmap.fig'); %can make pdf, jnp, or jpg + print (gcf, '-dpdf', 'injected_heatmap.pdf'); + + close all diff --git a/prescriptiondrugs/use_frequencyv2.m b/prescriptiondrugs/use_frequencyv2.m new file mode 100644 index 0000000..9c391cd --- /dev/null +++ b/prescriptiondrugs/use_frequencyv2.m @@ -0,0 +1,84 @@ +files1=dir(fullfile('C:','Users','kruggles7','Documents','MATLAB', 'CDC', 'data','results_061514','cat', '*.txt')); + +cd .. +cd .. +cd data +cd results_061514 +cd cat +index_yes=double.empty; + +questions={'Q79', 'Q27', 'Q29', 'Q25', 'Q32', 'Q31' }; +labels={'Prescription Drugs', 'Heroin', 'Methamphetamine', 'Cocaine', 'Ecstasy', 'Injected'}; +P=length(questions); +total=double.empty; +for n=1:P + q1=questions{n}; + q_name=[q1 '-cat-NaN.txt']; + quest_1=importdata(q_name, '\t'); + [r1,c1]=size(quest_1); + Q=double.empty; + count=1; + for k=1:r1 + check=quest_1(k,1); + if (check==2011 || check==2013 || check==2009) + q1_=quest_1(k,2:c1); + Q=cat(2,Q,q1_); + count=count+1; + end + max_=max(Q); + for i=2:max_ + indx=find(Q==i); + index_yes(n, i-1)=numel(indx); + end + indx_total=find(Q>0); + total(n)=numel(indx_total); + end +end + +sum_yes=sum(index_yes,2); +[r,c]=size(index_yes); +percent=zeros(r,c); +for n=1:c + for m=1:r + percent(m,n)=index_yes(m,n)/sum_yes(m,1); + end +end + +bar (percent, 'stacked'); +ylim([0 1]); +set(gca, 'xticklabel', labels); +legend('1 or 2 times','3 to 9 times', '10 to 19 times', '20 to 39 times', '40 or more times', 'Location','EastOutside' ); + +cd .. +cd .. +cd .. +cd programs +cd prescriptiondrugs +cd results +saveas (gca, 'PO_drugs_frequency_percentage.fig'); +close + + +[r,c]=size(index_yes); +freq=zeros(r,c); +for n=1:c + for m=1:r + freq(m,n)=index_yes(m,n)/total(n)*100; + end +end + +bar(index_yes,'stacked'); +set(gca, 'xticklabel', labels); +set(gca, 'xticklabel', labels); +legend('1 or 2 times','3 to 9 times', '10 to 19 times', '20 to 39 times', '40 or more times', 'Location','NorthEast' ); +ylabel('Number of participants surveyed'); +saveas (gca, 'PO_drugs_frequency.fig'); +close + +bar(freq,'stacked'); +set(gca, 'xticklabel', labels); +set(gca, 'xticklabel', labels); +legend('1 or 2 times','3 to 9 times', '10 to 19 times', '20 to 39 times', '40 or more times', 'Location','NorthEast' ); +ylabel('Percent of participants surveyed'); +saveas (gca, 'PO_drugs_frequency_total.fig'); + diff --git a/schooldrugs/plot_demographic_graphs.m b/schooldrugs/plot_demographic_graphs.m new file mode 100644 index 0000000..a3cfb6e --- /dev/null +++ b/schooldrugs/plot_demographic_graphs.m @@ -0,0 +1,289 @@ + function [per_mat1, per_mat2, ratio_mat] = plot_demographic_graphs( question_mat1, question_mat2, label ) +cd .. +cd .. +cd data +cd controls +sex=importdata('sex-NaN.txt', '\t'); +race=importdata('race-NaN.txt', '\t'); +weight=importdata('weight.txt','\t'); +cd .. +cd .. +cd programs +cd schooldrugs +%question 1------------------------------------------------------------- +[r,c]=size(question_mat1); +for i=1:r + index_yes{i}=find(question_mat1(i,:)==1); + index_girls{i}=find(sex(i,:)==1); + index_boys{i}=find(sex(i,:)==2); + index_W{i}=find(race(i,:)== 1 ); + index_B{i}=find(race(i,:)== 2 ); + index_H{i}=find(race(i,:)== 3 ); + index_O{i}=find(race(i,:)== 4 ); +end + +for i=1:r + index_missQ{i}=find(question_mat1(i,:)==9); %students who didn't answer the Q + index_nomiss{i}=find(question_mat1(i,:)==0 | question_mat1(i,:)==1); %answers that were NOT missing (ie. 0's and 1's / no's and yes's) +end + +for i=1:r + missQ(i)=length(index_missQ{i}); %number of students who answered the question each year +end + + +for i=1:r + index_total_b{i}=intersect(index_nomiss{i},index_boys{i}); %index of all boys who answered + index_total_g{i}=intersect(index_nomiss{i},index_girls{i}); %index of all girls who answered +end + +% totals of each race/sex/combo: (for percentages- to compare those who said yes to total questioned) +for i=1:r + w=weight(i,:)'; + total_ans(i)=nansum(w(index_nomiss{i})); + total_girls(i)=nansum(w(index_total_g{i})); %total # of girls who answered + total_boys(i)=nansum(w(index_total_b{i})); %total number of boys who answered + total_W{i}=nansum(w(intersect(index_nomiss{i}, index_W{i}))); %total # of white students who answered + total_B{i}=nansum(w(intersect(index_nomiss{i}, index_B{i}))); %total # of black students who answered + total_H{i}=nansum(w(intersect(index_nomiss{i}, index_H{i}))); %total # of hispanic students who answered + total_O{i}=nansum(w(intersect(index_nomiss{i}, index_O{i}))); %total # of "other" students who answered + total_Wb(i)=nansum(w(intersect(index_total_b{i},index_W{i}))); + total_Wg(i)=nansum(w(intersect(index_total_g{i},index_W{i}))); + total_Bb(i)=nansum(w(intersect(index_total_b{i},index_B{i}))); + total_Bg(i)=nansum(w(intersect(index_total_g{i},index_B{i}))); + total_Hb(i)=nansum(w(intersect(index_total_b{i},index_H{i}))); + total_Hg(i)=nansum(w(intersect(index_total_g{i},index_H{i}))); + total_Ob(i)=nansum(w(intersect(index_total_b{i},index_O{i}))); + total_Og(i)=nansum(w(intersect(index_total_g{i},index_O{i}))); +end + +% %yes-es: +for i=1:r + w=weight(i,:)'; + index_yesgirls{i}=intersect(index_yes{i},index_girls{i}); + index_yesboys{i}=intersect(index_yes{i},index_boys{i}); + yes_girls(i)=nansum(w(index_yesgirls{i})); + yes_boys(i)=nansum(w(index_yesboys{i})); + yes_W(i)=nansum(w(intersect(index_yes{i}, index_W{i}))); + yes_B(i)=nansum(w(intersect(index_yes{i}, index_B{i}))); + yes_H(i)=nansum(w(intersect(index_yes{i}, index_H{i}))); + yes_O(i)=nansum(w(intersect(index_yes{i}, index_O{i}))); + yes_WG(i)=nansum(w(intersect(index_yesgirls{i},index_W{i}))); + yes_BG(i)=nansum(w(intersect(index_yesgirls{i},index_B{i}))); + yes_HG(i)=nansum(w(intersect(index_yesgirls{i},index_H{i}))); + yes_OG(i)=nansum(w(intersect(index_yesgirls{i},index_O{i}))); + yes_WB(i)=nansum(w(intersect(index_yesboys{i},index_W{i}))); + yes_BB(i)=nansum(w(intersect(index_yesboys{i},index_B{i}))); + yes_HB(i)=nansum(w(intersect(index_yesboys{i},index_H{i}))); + yes_OB(i)=nansum(w(intersect(index_yesboys{i},index_O{i}))); + total_yes(i)=nansum(w(index_yes{i})); + total_w(i)=total_W{i}; + total_b(i)=total_B{i}; + total_h(i)=total_H{i}; + total_o(i)=total_O{i}; +end + +%put them all into a matrix as percent values: +per_mat1 = zeros(15,r); +for i=1:r + per_mat1(1, i)=total_yes(i)/total_ans(i)*100; %total + per_mat1(2, i)=yes_boys(i)/total_boys(i)*100; %boys + per_mat1(3, i)=yes_girls(i)/total_girls(i)*100; %girls + per_mat1(4, i)=yes_W(i)/total_w(i)*100; %whites + per_mat1(5, i)=yes_B(i)/total_b(i)*100; %blacks + per_mat1(6, i)=yes_H(i)/total_h(i)*100; %hispanics + per_mat1(7, i)=yes_O(i)/total_o(i)*100; %other + per_mat1(8, i)=yes_WB(i)/total_Wb(i)*100; %WB + per_mat1(9, i)=yes_WG(i)/total_Wg(i)*100; %WG + per_mat1(10, i)=yes_BB(i)/total_Bb(i)*100; %BB + per_mat1(11, i)=yes_BG(i)/total_Bg(i)*100; %BG + per_mat1(12, i)=yes_HB(i)/total_Hb(i)*100; %HB + per_mat1(13, i)=yes_HG(i)/total_Hg(i)*100; %HG + per_mat1(14, i)=yes_OB(i)/total_Ob(i)*100; %OB + per_mat1(15, i)=yes_OG(i)/total_Og(i)*100; %OG +end + +%QUESTION2--------------------------------------------------------------- + + +[r,c]=size(question_mat2); +per_mat_map=double.empty; +for i=1:r + index_yes{i}=find(question_mat2(i,:)==1); + index_girls{i}=find(sex(i,:)==1); + index_boys{i}=find(sex(i,:)==2); + index_W{i}=find(race(i,:)== 1 ); + index_B{i}=find(race(i,:)== 2 ); + index_H{i}=find(race(i,:)== 3 ); + index_O{i}=find(race(i,:)== 4 ); +end + +for i=1:r + index_missQ{i}=find(question_mat2(i,:)==9); %students who didn't answer the Q + index_nomiss{i}=find(question_mat2(i,:)==0 | question_mat2(i,:)==1); %answers that were NOT missing (ie. 0's and 1's / no's and yes's) +end + +for i=1:r + missQ(i)=length(index_missQ{i}); %number of students who answered the question each year +end + + +for i=1:r + index_total_b{i}=intersect(index_nomiss{i},index_boys{i}); %index of all boys who answered + index_total_g{i}=intersect(index_nomiss{i},index_girls{i}); %index of all girls who answered +end + +% totals of each race/sex/combo: (for percentages- to compare those who said yes to total questioned) +for i=1:r + w=weight(i,:)'; + total_ans(i)=nansum(w(index_nomiss{i})); + total_girls(i)=nansum(w(index_total_g{i})); %total # of girls who answered + total_boys(i)=nansum(w(index_total_b{i})); %total number of boys who answered + total_W{i}=nansum(w(intersect(index_nomiss{i}, index_W{i}))); %total # of white students who answered + total_B{i}=nansum(w(intersect(index_nomiss{i}, index_B{i}))); %total # of black students who answered + total_H{i}=nansum(w(intersect(index_nomiss{i}, index_H{i}))); %total # of hispanic students who answered + total_O{i}=nansum(w(intersect(index_nomiss{i}, index_O{i}))); %total # of "other" students who answered + total_Wb(i)=nansum(w(intersect(index_total_b{i},index_W{i}))); + total_Wg(i)=nansum(w(intersect(index_total_g{i},index_W{i}))); + total_Bb(i)=nansum(w(intersect(index_total_b{i},index_B{i}))); + total_Bg(i)=nansum(w(intersect(index_total_g{i},index_B{i}))); + total_Hb(i)=nansum(w(intersect(index_total_b{i},index_H{i}))); + total_Hg(i)=nansum(w(intersect(index_total_g{i},index_H{i}))); + total_Ob(i)=nansum(w(intersect(index_total_b{i},index_O{i}))); + total_Og(i)=nansum(w(intersect(index_total_g{i},index_O{i}))); +end + +% %yes-es: +for i=1:r + w=weight(i,:)'; + index_yesgirls{i}=intersect(index_yes{i},index_girls{i}); + index_yesboys{i}=intersect(index_yes{i},index_boys{i}); + yes_girls(i)=nansum(w(index_yesgirls{i})); + yes_boys(i)=nansum(w(index_yesboys{i})); + yes_W(i)=nansum(w(intersect(index_yes{i}, index_W{i}))); + yes_B(i)=nansum(w(intersect(index_yes{i}, index_B{i}))); + yes_H(i)=nansum(w(intersect(index_yes{i}, index_H{i}))); + yes_O(i)=nansum(w(intersect(index_yes{i}, index_O{i}))); + yes_WG(i)=nansum(w(intersect(index_yesgirls{i},index_W{i}))); + yes_BG(i)=nansum(w(intersect(index_yesgirls{i},index_B{i}))); + yes_HG(i)=nansum(w(intersect(index_yesgirls{i},index_H{i}))); + yes_OG(i)=nansum(w(intersect(index_yesgirls{i},index_O{i}))); + yes_WB(i)=nansum(w(intersect(index_yesboys{i},index_W{i}))); + yes_BB(i)=nansum(w(intersect(index_yesboys{i},index_B{i}))); + yes_HB(i)=nansum(w(intersect(index_yesboys{i},index_H{i}))); + yes_OB(i)=nansum(w(intersect(index_yesboys{i},index_O{i}))); + total_yes(i)=nansum(w(index_yes{i})); + total_w(i)=total_W{i}; + total_b(i)=total_B{i}; + total_h(i)=total_H{i}; + total_o(i)=total_O{i}; +end + +%put them all into a matrix as percent values: +per_mat2 = zeros(15,r); +for i=1:r + per_mat2(1, i)=total_yes(i)/total_ans(i)*100; %total + per_mat2(2, i)=yes_boys(i)/total_boys(i)*100; %boys + per_mat2(3, i)=yes_girls(i)/total_girls(i)*100; %girls + per_mat2(4, i)=yes_W(i)/total_w(i)*100; %whites + per_mat2(5, i)=yes_B(i)/total_b(i)*100; %blacks + per_mat2(6, i)=yes_H(i)/total_h(i)*100; %hispanics + per_mat2(7, i)=yes_O(i)/total_o(i)*100; %other + per_mat2(8, i)=yes_WB(i)/total_Wb(i)*100; %WB + per_mat2(9, i)=yes_WG(i)/total_Wg(i)*100; %WG + per_mat2(10, i)=yes_BB(i)/total_Bb(i)*100; %BB + per_mat2(11, i)=yes_BG(i)/total_Bg(i)*100; %BG + per_mat2(12, i)=yes_HB(i)/total_Hb(i)*100; %HB + per_mat2(13, i)=yes_HG(i)/total_Hg(i)*100; %HG + per_mat2(14, i)=yes_OB(i)/total_Ob(i)*100; %OB + per_mat2(15, i)=yes_OG(i)/total_Og(i)*100; %OG +end + +%PLOT-------------------------------------------------------------------- +label_year={'2001', '2003','2005','2007','2009','2011'}; +label_cell2={'Total', 'Boys', 'Girls', 'W', 'B', 'H', 'O', 'W Boys', 'W Girls', 'B Boys', 'B Girls', 'H Boys', 'H Girls', 'O Boys', 'O Girls'}; +x=2001:2:2011; +count=1; +max_v=max(max(per_mat1)); +max_v=max_v/10; +max_v=ceil(max_v); +max_v=max_v*10; +for i=1:7 + f2=subplot(14,1,count:count+1); + plot(x, per_mat1(i,:), '-ro'); + hold on + plot (x, per_mat2(i,:), '--b*'); + hold off + ylabel([label_cell2{i}]); + ylim ([0 max_v]); + if i<7 + %set(f2, 'xTick', []); + %set(f2, 'yTick', []); + set(f2,'xTickLabel',[]); + else + %set(f2, 'yTick', []); + set(f2, 'xTickLabel', label_year); + end + if i>1 %move the plot upward + pos1=get(f1,'Position'); + pos2=get(f2,'Position'); + %set widths equal + pos2(3)=pos1(3); + set (f2, 'Position', pos2); + %set x of first equal to second + pos1(1)=pos2(1); + set(f1, 'Position', pos1); + %move the second to touch first + pos2(2)= pos1(2) - pos2(4); %was pos1(3) + set (f2, 'Position', pos2); + end + f1=f2; + count=count+2; +end + +saveas (gcf, [label '1.fig']); +print (gcf, '-dpng', [label '1.png']); +close all + +count=1; +for i=8:15 + f2=subplot(16,1,count:count+1); + plot(x, per_mat1(i,:), '-ro'); + hold on + plot (x, per_mat2(i,:), '--b*'); + hold off + ylabel([label_cell2{i}]); + set(f2, 'xTick', x); + ylim ([0 max_v]); + if i<15 + %set(f2, 'xTick', []); + %set(f2, 'yTick', []); + set(f2,'xTickLabel',[]); + else + %set(f2, 'yTick', []); + set(f2, 'xTickLabel', label_year); + end + if i>8 %move the plot upward + pos1=get(f1,'Position'); + pos2=get(f2,'Position'); + %set widths equal + pos2(3)=pos1(3); + set (f2, 'Position', pos2); + %set x of first equal to second + pos1(1)=pos2(1); + set(f1, 'Position', pos1); + %move the second to touch first + pos2(2)= pos1(2) - pos2(4); %was pos1(3) + set (f2, 'Position', pos2); + end + f1=f2; + count=count+2; +end + +saveas (gcf, [label '2.fig']); +print (gcf, '-dpng', [label '2.png']); +close all + +ratio_mat=per_mat2./per_mat; + + end %end of function diff --git a/schooldrugs/plot_demographic_graphsv2.m b/schooldrugs/plot_demographic_graphsv2.m new file mode 100644 index 0000000..59c99e1 --- /dev/null +++ b/schooldrugs/plot_demographic_graphsv2.m @@ -0,0 +1,261 @@ + function [per_mat1, per_mat2, ratio_mat] = plot_demographic_graphsv2( question_mat1, question_mat2, label ) +cd .. +cd .. +cd data +cd controls +sex=importdata('sex-NaN.txt', '\t'); +race=importdata('race-NaN.txt', '\t'); +weight=importdata('weight.txt','\t'); +cd .. +cd .. +cd programs +cd schooldrugs +%question 1------------------------------------------------------------- +[r,c]=size(question_mat1); +for i=1:r + index_yes{i}=find(question_mat1(i,:)==1); + index_girls{i}=find(sex(i,:)==1); + index_boys{i}=find(sex(i,:)==2); + index_W{i}=find(race(i,:)== 1 ); + index_B{i}=find(race(i,:)== 2 ); + index_H{i}=find(race(i,:)== 3 ); + index_O{i}=find(race(i,:)== 4 ); +end + +for i=1:r + index_missQ{i}=find(question_mat1(i,:)==9); %students who didn't answer the Q + index_nomiss{i}=find(question_mat1(i,:)==0 | question_mat1(i,:)==1); %answers that were NOT missing (ie. 0's and 1's / no's and yes's) +end + +for i=1:r + missQ(i)=length(index_missQ{i}); %number of students who answered the question each year +end + + +for i=1:r + index_total_b{i}=intersect(index_nomiss{i},index_boys{i}); %index of all boys who answered + index_total_g{i}=intersect(index_nomiss{i},index_girls{i}); %index of all girls who answered +end + +% totals of each race/sex/combo: (for percentages- to compare those who said yes to total questioned) +for i=1:r + w=weight(i,:)'; + total_ans(i)=nansum(w(index_nomiss{i})); + total_girls(i)=nansum(w(index_total_g{i})); %total # of girls who answered + total_boys(i)=nansum(w(index_total_b{i})); %total number of boys who answered + total_W{i}=nansum(w(intersect(index_nomiss{i}, index_W{i}))); %total # of white students who answered + total_B{i}=nansum(w(intersect(index_nomiss{i}, index_B{i}))); %total # of black students who answered + total_H{i}=nansum(w(intersect(index_nomiss{i}, index_H{i}))); %total # of hispanic students who answered + total_O{i}=nansum(w(intersect(index_nomiss{i}, index_O{i}))); %total # of "other" students who answered + total_Wb(i)=nansum(w(intersect(index_total_b{i},index_W{i}))); + total_Wg(i)=nansum(w(intersect(index_total_g{i},index_W{i}))); + total_Bb(i)=nansum(w(intersect(index_total_b{i},index_B{i}))); + total_Bg(i)=nansum(w(intersect(index_total_g{i},index_B{i}))); + total_Hb(i)=nansum(w(intersect(index_total_b{i},index_H{i}))); + total_Hg(i)=nansum(w(intersect(index_total_g{i},index_H{i}))); + total_Ob(i)=nansum(w(intersect(index_total_b{i},index_O{i}))); + total_Og(i)=nansum(w(intersect(index_total_g{i},index_O{i}))); +end + +% %yes-es: +for i=1:r + w=weight(i,:)'; + index_yesgirls{i}=intersect(index_yes{i},index_girls{i}); + index_yesboys{i}=intersect(index_yes{i},index_boys{i}); + yes_girls(i)=nansum(w(index_yesgirls{i})); + yes_boys(i)=nansum(w(index_yesboys{i})); + yes_W(i)=nansum(w(intersect(index_yes{i}, index_W{i}))); + yes_B(i)=nansum(w(intersect(index_yes{i}, index_B{i}))); + yes_H(i)=nansum(w(intersect(index_yes{i}, index_H{i}))); + yes_O(i)=nansum(w(intersect(index_yes{i}, index_O{i}))); + yes_WG(i)=nansum(w(intersect(index_yesgirls{i},index_W{i}))); + yes_BG(i)=nansum(w(intersect(index_yesgirls{i},index_B{i}))); + yes_HG(i)=nansum(w(intersect(index_yesgirls{i},index_H{i}))); + yes_OG(i)=nansum(w(intersect(index_yesgirls{i},index_O{i}))); + yes_WB(i)=nansum(w(intersect(index_yesboys{i},index_W{i}))); + yes_BB(i)=nansum(w(intersect(index_yesboys{i},index_B{i}))); + yes_HB(i)=nansum(w(intersect(index_yesboys{i},index_H{i}))); + yes_OB(i)=nansum(w(intersect(index_yesboys{i},index_O{i}))); + total_yes(i)=nansum(w(index_yes{i})); + total_w(i)=total_W{i}; + total_b(i)=total_B{i}; + total_h(i)=total_H{i}; + total_o(i)=total_O{i}; +end + +%put them all into a matrix as percent values: +per_mat1 = zeros(15,r); +for i=1:r + per_mat1(1, i)=total_yes(i)/total_ans(i)*100; %total + per_mat1(2, i)=yes_boys(i)/total_boys(i)*100; %boys + per_mat1(3, i)=yes_girls(i)/total_girls(i)*100; %girls + per_mat1(4, i)=yes_W(i)/total_w(i)*100; %whites + per_mat1(5, i)=yes_B(i)/total_b(i)*100; %blacks + per_mat1(6, i)=yes_H(i)/total_h(i)*100; %hispanics + per_mat1(7, i)=yes_O(i)/total_o(i)*100; %other + per_mat1(8, i)=yes_WB(i)/total_Wb(i)*100; %WB + per_mat1(9, i)=yes_WG(i)/total_Wg(i)*100; %WG + per_mat1(10, i)=yes_BB(i)/total_Bb(i)*100; %BB + per_mat1(11, i)=yes_BG(i)/total_Bg(i)*100; %BG + per_mat1(12, i)=yes_HB(i)/total_Hb(i)*100; %HB + per_mat1(13, i)=yes_HG(i)/total_Hg(i)*100; %HG + per_mat1(14, i)=yes_OB(i)/total_Ob(i)*100; %OB + per_mat1(15, i)=yes_OG(i)/total_Og(i)*100; %OG +end + +%QUESTION2--------------------------------------------------------------- + + +[r,c]=size(question_mat2); +per_mat_map=double.empty; +for i=1:r + index_yes{i}=find(question_mat2(i,:)==1); + index_girls{i}=find(sex(i,:)==1); + index_boys{i}=find(sex(i,:)==2); + index_W{i}=find(race(i,:)== 1 ); + index_B{i}=find(race(i,:)== 2 ); + index_H{i}=find(race(i,:)== 3 ); + index_O{i}=find(race(i,:)== 4 ); +end + +for i=1:r + index_missQ{i}=find(question_mat2(i,:)==9); %students who didn't answer the Q + index_nomiss{i}=find(question_mat2(i,:)==0 | question_mat2(i,:)==1); %answers that were NOT missing (ie. 0's and 1's / no's and yes's) +end + +for i=1:r + missQ(i)=length(index_missQ{i}); %number of students who answered the question each year +end + + +for i=1:r + index_total_b{i}=intersect(index_nomiss{i},index_boys{i}); %index of all boys who answered + index_total_g{i}=intersect(index_nomiss{i},index_girls{i}); %index of all girls who answered +end + +% totals of each race/sex/combo: (for percentages- to compare those who said yes to total questioned) +for i=1:r + w=weight(i,:)'; + total_ans(i)=nansum(w(index_nomiss{i})); + total_girls(i)=nansum(w(index_total_g{i})); %total # of girls who answered + total_boys(i)=nansum(w(index_total_b{i})); %total number of boys who answered + total_W{i}=nansum(w(intersect(index_nomiss{i}, index_W{i}))); %total # of white students who answered + total_B{i}=nansum(w(intersect(index_nomiss{i}, index_B{i}))); %total # of black students who answered + total_H{i}=nansum(w(intersect(index_nomiss{i}, index_H{i}))); %total # of hispanic students who answered + total_O{i}=nansum(w(intersect(index_nomiss{i}, index_O{i}))); %total # of "other" students who answered + total_Wb(i)=nansum(w(intersect(index_total_b{i},index_W{i}))); + total_Wg(i)=nansum(w(intersect(index_total_g{i},index_W{i}))); + total_Bb(i)=nansum(w(intersect(index_total_b{i},index_B{i}))); + total_Bg(i)=nansum(w(intersect(index_total_g{i},index_B{i}))); + total_Hb(i)=nansum(w(intersect(index_total_b{i},index_H{i}))); + total_Hg(i)=nansum(w(intersect(index_total_g{i},index_H{i}))); + total_Ob(i)=nansum(w(intersect(index_total_b{i},index_O{i}))); + total_Og(i)=nansum(w(intersect(index_total_g{i},index_O{i}))); +end + +% %yes-es: +for i=1:r + w=weight(i,:)'; + index_yesgirls{i}=intersect(index_yes{i},index_girls{i}); + index_yesboys{i}=intersect(index_yes{i},index_boys{i}); + yes_girls(i)=nansum(w(index_yesgirls{i})); + yes_boys(i)=nansum(w(index_yesboys{i})); + yes_W(i)=nansum(w(intersect(index_yes{i}, index_W{i}))); + yes_B(i)=nansum(w(intersect(index_yes{i}, index_B{i}))); + yes_H(i)=nansum(w(intersect(index_yes{i}, index_H{i}))); + yes_O(i)=nansum(w(intersect(index_yes{i}, index_O{i}))); + yes_WG(i)=nansum(w(intersect(index_yesgirls{i},index_W{i}))); + yes_BG(i)=nansum(w(intersect(index_yesgirls{i},index_B{i}))); + yes_HG(i)=nansum(w(intersect(index_yesgirls{i},index_H{i}))); + yes_OG(i)=nansum(w(intersect(index_yesgirls{i},index_O{i}))); + yes_WB(i)=nansum(w(intersect(index_yesboys{i},index_W{i}))); + yes_BB(i)=nansum(w(intersect(index_yesboys{i},index_B{i}))); + yes_HB(i)=nansum(w(intersect(index_yesboys{i},index_H{i}))); + yes_OB(i)=nansum(w(intersect(index_yesboys{i},index_O{i}))); + total_yes(i)=nansum(w(index_yes{i})); + total_w(i)=total_W{i}; + total_b(i)=total_B{i}; + total_h(i)=total_H{i}; + total_o(i)=total_O{i}; +end + +%put them all into a matrix as percent values: +per_mat2 = zeros(15,r); +for i=1:r + per_mat2(1, i)=total_yes(i)/total_ans(i)*100; %total + per_mat2(2, i)=yes_boys(i)/total_boys(i)*100; %boys + per_mat2(3, i)=yes_girls(i)/total_girls(i)*100; %girls + per_mat2(4, i)=yes_W(i)/total_w(i)*100; %whites + per_mat2(5, i)=yes_B(i)/total_b(i)*100; %blacks + per_mat2(6, i)=yes_H(i)/total_h(i)*100; %hispanics + per_mat2(7, i)=yes_O(i)/total_o(i)*100; %other + per_mat2(8, i)=yes_WB(i)/total_Wb(i)*100; %WB + per_mat2(9, i)=yes_WG(i)/total_Wg(i)*100; %WG + per_mat2(10, i)=yes_BB(i)/total_Bb(i)*100; %BB + per_mat2(11, i)=yes_BG(i)/total_Bg(i)*100; %BG + per_mat2(12, i)=yes_HB(i)/total_Hb(i)*100; %HB + per_mat2(13, i)=yes_HG(i)/total_Hg(i)*100; %HG + per_mat2(14, i)=yes_OB(i)/total_Ob(i)*100; %OB + per_mat2(15, i)=yes_OG(i)/total_Og(i)*100; %OG +end + +%PLOT-------------------------------------------------------------------- +%label_year={'2001', '2003','2005','2007','2009','2011'}; +label_cell={'Total', 'Boys', 'Girls', 'W', 'B', 'H', 'O', 'W Boys', 'W Girls', 'B Boys', 'B Girls', 'H Boys', 'H Girls', 'O Boys', 'O Girls'}; +x=2001:2:2011; +count=1; +max_v=max(max(per_mat1)); +max_v=max_v/10; +max_v=ceil(max_v); +max_v=max_v*10; +for i=1:15 + f1=subplot(2,1,1); + x1=[i i i i i i]; + scatter(x1, per_mat1(i,:), 10, 'or', 'filled'); + m1=mean(per_mat1(i,:)); + hold on + x_=[i-0.25; i+0.25]; + y_=[m1; m1]; + plot(x_, y_, '-k'); + x2=[i i i i i i]; + scatter(x2, per_mat2(i,:),15,'*b'); + m2=mean(per_mat2(i,:)); + x_=[i-0.25; i+0.25]; + y_=[m2; m2]; + plot(x_, y_, '-k'); + f2=subplot(2,1,2); + ratio=per_mat2(i,:)./per_mat1(i,:); + scatter(x1, ratio, 10,'og', 'filled'); + hold on + m3=mean(ratio); + x_=[i-0.25; i+0.25]; + y_=[m3; m3]; + plot(x_, y_, '-k'); + ylim([0 1]); +end +x=[0 16]; +y=1; +plot(x,y,'-k'); +hold off +set(f2, 'Xtick', 1:15); +set(f2, 'XTickLabel', label_cell); +if i>1 %move the plot upward + pos1=get(f1,'Position'); + pos2=get(f2,'Position'); + %set widths equal + pos2(3)=pos1(3); + set (f2, 'Position', pos2); + %set x of first equal to second + pos1(1)=pos2(1); + set(f1, 'Position', pos1); + %move the second to touch first + pos2(2)= pos1(2) - pos2(4); %was pos1(3) + set (f2, 'Position', pos2); +end +saveas (gcf, [label '.fig']); +print (gcf, '-dpng', [label '.png']); +close all + +ratio_mat=per_mat2./per_mat1; + + end %end of function diff --git a/schooldrugs/plot_demographic_graphsv3.m b/schooldrugs/plot_demographic_graphsv3.m new file mode 100644 index 0000000..eb217e1 --- /dev/null +++ b/schooldrugs/plot_demographic_graphsv3.m @@ -0,0 +1,247 @@ + function [per_mat1, per_mat2, ratio_mat] = plot_demographic_graphsv3( question_mat1, question_mat2, label ) +cd .. +cd .. +cd data +cd controls +sex=importdata('sex-NaN.txt', '\t'); +race=importdata('race-NaN.txt', '\t'); +weight=importdata('weight.txt','\t'); +cd .. +cd .. +cd programs +cd schooldrugs +%question 1------------------------------------------------------------- +[r,c]=size(question_mat1); +for i=1:r + index_yes{i}=find(question_mat1(i,:)==1); + index_girls{i}=find(sex(i,:)==1); + index_boys{i}=find(sex(i,:)==2); + index_W{i}=find(race(i,:)== 1 ); + index_B{i}=find(race(i,:)== 2 ); + index_H{i}=find(race(i,:)== 3 ); + index_O{i}=find(race(i,:)== 4 ); +end + +for i=1:r + index_missQ{i}=find(question_mat1(i,:)==9); %students who didn't answer the Q + index_nomiss{i}=find(question_mat1(i,:)==0 | question_mat1(i,:)==1); %answers that were NOT missing (ie. 0's and 1's / no's and yes's) +end + +for i=1:r + missQ(i)=length(index_missQ{i}); %number of students who answered the question each year +end + + +for i=1:r + index_total_b{i}=intersect(index_nomiss{i},index_boys{i}); %index of all boys who answered + index_total_g{i}=intersect(index_nomiss{i},index_girls{i}); %index of all girls who answered +end + +% totals of each race/sex/combo: (for percentages- to compare those who said yes to total questioned) +for i=1:r + w=weight(i,:)'; + total_ans(i)=nansum(w(index_nomiss{i})); + total_girls(i)=nansum(w(index_total_g{i})); %total # of girls who answered + total_boys(i)=nansum(w(index_total_b{i})); %total number of boys who answered + total_W{i}=nansum(w(intersect(index_nomiss{i}, index_W{i}))); %total # of white students who answered + total_B{i}=nansum(w(intersect(index_nomiss{i}, index_B{i}))); %total # of black students who answered + total_H{i}=nansum(w(intersect(index_nomiss{i}, index_H{i}))); %total # of hispanic students who answered + total_O{i}=nansum(w(intersect(index_nomiss{i}, index_O{i}))); %total # of "other" students who answered + total_Wb(i)=nansum(w(intersect(index_total_b{i},index_W{i}))); + total_Wg(i)=nansum(w(intersect(index_total_g{i},index_W{i}))); + total_Bb(i)=nansum(w(intersect(index_total_b{i},index_B{i}))); + total_Bg(i)=nansum(w(intersect(index_total_g{i},index_B{i}))); + total_Hb(i)=nansum(w(intersect(index_total_b{i},index_H{i}))); + total_Hg(i)=nansum(w(intersect(index_total_g{i},index_H{i}))); + total_Ob(i)=nansum(w(intersect(index_total_b{i},index_O{i}))); + total_Og(i)=nansum(w(intersect(index_total_g{i},index_O{i}))); +end + +% %yes-es: +for i=1:r + w=weight(i,:)'; + index_yesgirls{i}=intersect(index_yes{i},index_girls{i}); + index_yesboys{i}=intersect(index_yes{i},index_boys{i}); + yes_girls(i)=nansum(w(index_yesgirls{i})); + yes_boys(i)=nansum(w(index_yesboys{i})); + yes_W(i)=nansum(w(intersect(index_yes{i}, index_W{i}))); + yes_B(i)=nansum(w(intersect(index_yes{i}, index_B{i}))); + yes_H(i)=nansum(w(intersect(index_yes{i}, index_H{i}))); + yes_O(i)=nansum(w(intersect(index_yes{i}, index_O{i}))); + yes_WG(i)=nansum(w(intersect(index_yesgirls{i},index_W{i}))); + yes_BG(i)=nansum(w(intersect(index_yesgirls{i},index_B{i}))); + yes_HG(i)=nansum(w(intersect(index_yesgirls{i},index_H{i}))); + yes_OG(i)=nansum(w(intersect(index_yesgirls{i},index_O{i}))); + yes_WB(i)=nansum(w(intersect(index_yesboys{i},index_W{i}))); + yes_BB(i)=nansum(w(intersect(index_yesboys{i},index_B{i}))); + yes_HB(i)=nansum(w(intersect(index_yesboys{i},index_H{i}))); + yes_OB(i)=nansum(w(intersect(index_yesboys{i},index_O{i}))); + total_yes(i)=nansum(w(index_yes{i})); + total_w(i)=total_W{i}; + total_b(i)=total_B{i}; + total_h(i)=total_H{i}; + total_o(i)=total_O{i}; +end + +%put them all into a matrix as percent values: +per_mat1 = zeros(15,r); +for i=1:r + per_mat1(1, i)=total_yes(i)/total_ans(i)*100; %total + per_mat1(2, i)=yes_boys(i)/total_boys(i)*100; %boys + per_mat1(3, i)=yes_girls(i)/total_girls(i)*100; %girls + per_mat1(4, i)=yes_W(i)/total_w(i)*100; %whites + per_mat1(5, i)=yes_B(i)/total_b(i)*100; %blacks + per_mat1(6, i)=yes_H(i)/total_h(i)*100; %hispanics + per_mat1(7, i)=yes_O(i)/total_o(i)*100; %other + per_mat1(8, i)=yes_WB(i)/total_Wb(i)*100; %WB + per_mat1(9, i)=yes_WG(i)/total_Wg(i)*100; %WG + per_mat1(10, i)=yes_BB(i)/total_Bb(i)*100; %BB + per_mat1(11, i)=yes_BG(i)/total_Bg(i)*100; %BG + per_mat1(12, i)=yes_HB(i)/total_Hb(i)*100; %HB + per_mat1(13, i)=yes_HG(i)/total_Hg(i)*100; %HG + per_mat1(14, i)=yes_OB(i)/total_Ob(i)*100; %OB + per_mat1(15, i)=yes_OG(i)/total_Og(i)*100; %OG +end + +%QUESTION2--------------------------------------------------------------- + + +[r,c]=size(question_mat2); +per_mat_map=double.empty; +for i=1:r + index_yes{i}=find(question_mat2(i,:)==1); + index_girls{i}=find(sex(i,:)==1); + index_boys{i}=find(sex(i,:)==2); + index_W{i}=find(race(i,:)== 1 ); + index_B{i}=find(race(i,:)== 2 ); + index_H{i}=find(race(i,:)== 3 ); + index_O{i}=find(race(i,:)== 4 ); +end + +for i=1:r + index_missQ{i}=find(question_mat2(i,:)==9); %students who didn't answer the Q + index_nomiss{i}=find(question_mat2(i,:)==0 | question_mat2(i,:)==1); %answers that were NOT missing (ie. 0's and 1's / no's and yes's) +end + +for i=1:r + missQ(i)=length(index_missQ{i}); %number of students who answered the question each year +end + + +for i=1:r + index_total_b{i}=intersect(index_nomiss{i},index_boys{i}); %index of all boys who answered + index_total_g{i}=intersect(index_nomiss{i},index_girls{i}); %index of all girls who answered +end + +% totals of each race/sex/combo: (for percentages- to compare those who said yes to total questioned) +for i=1:r + w=weight(i,:)'; + total_ans(i)=nansum(w(index_nomiss{i})); + total_girls(i)=nansum(w(index_total_g{i})); %total # of girls who answered + total_boys(i)=nansum(w(index_total_b{i})); %total number of boys who answered + total_W{i}=nansum(w(intersect(index_nomiss{i}, index_W{i}))); %total # of white students who answered + total_B{i}=nansum(w(intersect(index_nomiss{i}, index_B{i}))); %total # of black students who answered + total_H{i}=nansum(w(intersect(index_nomiss{i}, index_H{i}))); %total # of hispanic students who answered + total_O{i}=nansum(w(intersect(index_nomiss{i}, index_O{i}))); %total # of "other" students who answered + total_Wb(i)=nansum(w(intersect(index_total_b{i},index_W{i}))); + total_Wg(i)=nansum(w(intersect(index_total_g{i},index_W{i}))); + total_Bb(i)=nansum(w(intersect(index_total_b{i},index_B{i}))); + total_Bg(i)=nansum(w(intersect(index_total_g{i},index_B{i}))); + total_Hb(i)=nansum(w(intersect(index_total_b{i},index_H{i}))); + total_Hg(i)=nansum(w(intersect(index_total_g{i},index_H{i}))); + total_Ob(i)=nansum(w(intersect(index_total_b{i},index_O{i}))); + total_Og(i)=nansum(w(intersect(index_total_g{i},index_O{i}))); +end + +% %yes-es: +for i=1:r + w=weight(i,:)'; + index_yesgirls{i}=intersect(index_yes{i},index_girls{i}); + index_yesboys{i}=intersect(index_yes{i},index_boys{i}); + yes_girls(i)=nansum(w(index_yesgirls{i})); + yes_boys(i)=nansum(w(index_yesboys{i})); + yes_W(i)=nansum(w(intersect(index_yes{i}, index_W{i}))); + yes_B(i)=nansum(w(intersect(index_yes{i}, index_B{i}))); + yes_H(i)=nansum(w(intersect(index_yes{i}, index_H{i}))); + yes_O(i)=nansum(w(intersect(index_yes{i}, index_O{i}))); + yes_WG(i)=nansum(w(intersect(index_yesgirls{i},index_W{i}))); + yes_BG(i)=nansum(w(intersect(index_yesgirls{i},index_B{i}))); + yes_HG(i)=nansum(w(intersect(index_yesgirls{i},index_H{i}))); + yes_OG(i)=nansum(w(intersect(index_yesgirls{i},index_O{i}))); + yes_WB(i)=nansum(w(intersect(index_yesboys{i},index_W{i}))); + yes_BB(i)=nansum(w(intersect(index_yesboys{i},index_B{i}))); + yes_HB(i)=nansum(w(intersect(index_yesboys{i},index_H{i}))); + yes_OB(i)=nansum(w(intersect(index_yesboys{i},index_O{i}))); + total_yes(i)=nansum(w(index_yes{i})); + total_w(i)=total_W{i}; + total_b(i)=total_B{i}; + total_h(i)=total_H{i}; + total_o(i)=total_O{i}; +end + +%put them all into a matrix as percent values: +per_mat2 = zeros(15,r); +for i=1:r + per_mat2(1, i)=total_yes(i)/total_ans(i)*100; %total + per_mat2(2, i)=yes_boys(i)/total_boys(i)*100; %boys + per_mat2(3, i)=yes_girls(i)/total_girls(i)*100; %girls + per_mat2(4, i)=yes_W(i)/total_w(i)*100; %whites + per_mat2(5, i)=yes_B(i)/total_b(i)*100; %blacks + per_mat2(6, i)=yes_H(i)/total_h(i)*100; %hispanics + per_mat2(7, i)=yes_O(i)/total_o(i)*100; %other + per_mat2(8, i)=yes_WB(i)/total_Wb(i)*100; %WB + per_mat2(9, i)=yes_WG(i)/total_Wg(i)*100; %WG + per_mat2(10, i)=yes_BB(i)/total_Bb(i)*100; %BB + per_mat2(11, i)=yes_BG(i)/total_Bg(i)*100; %BG + per_mat2(12, i)=yes_HB(i)/total_Hb(i)*100; %HB + per_mat2(13, i)=yes_HG(i)/total_Hg(i)*100; %HG + per_mat2(14, i)=yes_OB(i)/total_Ob(i)*100; %OB + per_mat2(15, i)=yes_OG(i)/total_Og(i)*100; %OG +end + +%PLOT-------------------------------------------------------------------- +%label_year={'2001', '2003','2005','2007','2009','2011'}; +label_cell={'Total', 'Boys', 'Girls', 'W', 'B', 'H', 'O', 'W Boys', 'W Girls', 'B Boys', 'B Girls', 'H Boys', 'H Girls', 'O Boys', 'O Girls'}; +x=2001:2:2011; +count=1; +max_v=max(max(per_mat1)); +max_v=max_v/10; +max_v=ceil(max_v); +max_v=max_v*10; + +table_final=cell(16,7); +table_final(2:16,1)=label_cell; +label_cell2={'2001', '2003', '2005', '2007', '2009', '2011'}; +table_final(1,2:7)=label_cell2; +for i=1:15 + x1=[i i i i i i]; + scatter(x1, per_mat1(i,:), 10, 'or', 'filled'); + m1=mean(per_mat1(i,:)); + hold on + x_=[i-0.25; i+0.25]; + y_=[m1; m1]; + plot(x_, y_, '-k'); + x2=[i i i i i i]; + scatter(x2, per_mat2(i,:),15,'*b'); + m2=mean(per_mat2(i,:)); + x_=[i-0.25; i+0.25]; + y_=[m2; m2]; + plot(x_, y_, '-k'); + ratio=per_mat2(i,:)./per_mat1(i,:); + for j=2:7 + table_final{i+1,j}=num2str(ratio(j-1)); + end +end +hold off +set(gca, 'Xtick', 1:15); +set(gca, 'XTickLabel', label_cell); +rotateXLabels(gca,90); +saveas (gcf, [label '.fig']); +print (gcf, '-dpng', [label '.png']); +close all + +save([label '.mat'], 'table_final'); + +ratio_mat=per_mat2./per_mat1; + + end %end of function diff --git a/schooldrugs/rotateXLabels.m b/schooldrugs/rotateXLabels.m new file mode 100644 index 0000000..c9650e6 --- /dev/null +++ b/schooldrugs/rotateXLabels.m @@ -0,0 +1,415 @@ +function hh = rotateXLabels( ax, angle, varargin ) +%rotateXLabels: rotate any xticklabels +% +% hh = rotateXLabels(ax,angle) rotates all XLabels on axes AX by an angle +% ANGLE (in degrees). Handles to the resulting text objects are returned +% in HH. +% +% hh = rotateXLabels(ax,angle,param,value,...) also allows one or more +% optional parameters to be specified. Possible parameters are: +% 'MaxStringLength' The maximum length of label to show (default inf) +% +% Examples: +% >> bar( hsv(5)+0.05 ) +% >> days = {'Monday','Tuesday','Wednesday','Thursday','Friday'}; +% >> set( gca(), 'XTickLabel', days ) +% >> rotateXLabels( gca(), 45 ) +% +% See also: GCA, BAR + +% Copyright 2006-2013 The MathWorks Ltd. + +error( nargchk( 2, inf, nargin ) ); +if ~isnumeric( angle ) || ~isscalar( angle ) + error( 'RotateXLabels:BadAngle', 'Parameter ANGLE must be a scalar angle in degrees' ) +end +angle = mod( angle, 360 ); + +[maxStringLength] = parseInputs( varargin{:} ); + +% Get the existing label texts and clear them +[vals, labels] = findAndClearExistingLabels( ax, maxStringLength ); + +% Create the new label texts +h = createNewLabels( ax, vals, labels, angle ); + +% Reposition the axes itself to leave space for the new labels +repositionAxes( ax ); + +% If an X-label is present, move it too +repositionXLabel( ax ); + +% Store angle +setappdata( ax, 'RotateXLabelsAngle', angle ); + +% Only send outputs if requested +if nargout + hh = h; +end + +%-------------------------------------------------------------------------% + function [maxStringLength] = parseInputs( varargin ) + % Parse optional inputs + maxStringLength = inf; + if nargin > 0 + params = varargin(1:2:end); + values = varargin(2:2:end); + if numel( params ) ~= numel( values ) + error( 'RotateXLabels:BadSyntax', 'Optional arguments must be specified as parameter-value pairs.' ); + end + if any( ~cellfun( 'isclass', params, 'char' ) ) + error( 'RotateXLabels:BadSyntax', 'Optional argument names must be specified as strings.' ); + end + for pp=1:numel( params ) + switch upper( params{pp} ) + case 'MAXSTRINGLENGTH' + maxStringLength = values{pp}; + + otherwise + error( 'RotateXLabels:BadParam', 'Optional parameter ''%s'' not recognised.', params{pp} ); + end + end + end + end % parseInputs +%-------------------------------------------------------------------------% + function [vals,labels] = findAndClearExistingLabels( ax, maxStringLength ) + % Get the current tick positions so that we can place our new labels + vals = get( ax, 'XTick' ); + + % Now determine the labels. We look first at for previously rotated labels + % since if there are some the actual labels will be empty. + ex = findall( ax, 'Tag', 'RotatedXTickLabel' ); + if isempty( ex ) + % Store the positions and labels + labels = get( ax, 'XTickLabel' ); + if isempty( labels ) + % No labels! + return + else + if ~iscell(labels) + labels = cellstr(labels); + end + end + % Clear existing labels so that xlabel is in the right position + set( ax, 'XTickLabel', {}, 'XTickMode', 'Manual' ); + setappdata( ax, 'OriginalXTickLabels', labels ); + else + % Labels have already been rotated, so capture them + labels = getappdata( ax, 'OriginalXTickLabels' ); + set(ex, 'DeleteFcn', []); + delete(ex); + end + % Limit the length, if requested + if isfinite( maxStringLength ) + for ll=1:numel( labels ) + if length( labels{ll} ) > maxStringLength + labels{ll} = labels{ll}(1:maxStringLength); + end + end + end + + end % findAndClearExistingLabels +%-------------------------------------------------------------------------% + function restoreDefaultLabels( ax ) + % Restore the default axis behavior + removeListeners( ax ); + + % Try to restore the tick marks and labels + set( ax, 'XTickMode', 'auto', 'XTickLabelMode', 'auto' ); + rmappdata( ax, 'OriginalXTickLabels' ); + + % Try to restore the axes position + if isappdata( ax, 'OriginalAxesPosition' ) + set( ax, 'Position', getappdata( ax, 'OriginalAxesPosition' ) ); + rmappdata( ax, 'OriginalAxesPosition' ); + end + end +%-------------------------------------------------------------------------% + function textLabels = createNewLabels( ax, vals, labels, angle ) + % Work out the ticklabel positions + zlim = get(ax,'ZLim'); + z = zlim(1); + + % We want to work in normalised coords, but this doesn't print + % correctly. Instead we have to work in data units even though it + % makes positioning hard. + ylim = get( ax, 'YLim' ); + if strcmpi( get( ax, 'XAxisLocation' ), 'Top' ) + y = ylim(2); + else + y = ylim(1); + end + + % Now create new text objects in similar positions. + textLabels = -1*ones( numel( vals ), 1 ); + for ll=1:numel(vals) + textLabels(ll) = text( ... + 'Units', 'Data', ... + 'Position', [vals(ll), y, z], ... + 'String', labels{ll}, ... + 'Parent', ax, ... + 'Clipping', 'off', ... + 'Rotation', angle, ... + 'Tag', 'RotatedXTickLabel', ... + 'UserData', vals(ll)); + end + % So that we can respond to CLA and CLOSE, attach a delete + % callback. We only attach it to one label to save massive numbers + % of callbacks during axes shut-down. + set(textLabels(end), 'DeleteFcn', @onTextLabelDeleted); + + % Now copy font properties into the texts + updateFont(); + % Update the alignment of the text + updateAlignment(); + + end % createNewLabels + +%-------------------------------------------------------------------------% + function repositionAxes( ax ) + % Reposition the axes so that there's room for the labels + % Note that we only do this if the OuterPosition is the thing being + % controlled + if ~strcmpi( get( ax, 'ActivePositionProperty' ), 'OuterPosition' ) + return; + end + + % Work out the maximum height required for the labels + labelHeight = getLabelHeight(ax); + + % Remove listeners while we mess around with things, otherwise we'll + % trigger redraws recursively + removeListeners( ax ); + + % Change to normalized units for the position calculation + oldUnits = get( ax, 'Units' ); + set( ax, 'Units', 'Normalized' ); + + % Not sure why, but the extent seems to be proportional to the height of the axes. + % Correct that now. + set( ax, 'ActivePositionProperty', 'Position' ); + pos = get( ax, 'Position' ); + axesHeight = pos(4); + % Make sure we don't adjust away the axes entirely! + heightAdjust = min( (axesHeight*0.9), labelHeight*axesHeight ); + + % Move the axes + if isappdata( ax, 'OriginalAxesPosition' ) + pos = getappdata( ax, 'OriginalAxesPosition' ); + else + pos = get(ax,'Position'); + setappdata( ax, 'OriginalAxesPosition', pos ); + end + if strcmpi( get( ax, 'XAxisLocation' ), 'Bottom' ) + % Move it up and reduce the height + set( ax, 'Position', pos+[0 heightAdjust 0 -heightAdjust] ) + else + % Just reduce the height + set( ax, 'Position', pos+[0 0 0 -heightAdjust] ) + end + set( ax, 'Units', oldUnits ); + set( ax, 'ActivePositionProperty', 'OuterPosition' ); + + % Make sure we find out if axes properties are changed + addListeners( ax ); + + end % repositionAxes + +%-------------------------------------------------------------------------% + function repositionXLabel( ax ) + % Try to work out where to put the xlabel + removeListeners( ax ); + labelHeight = getLabelHeight(ax); + + % Use the new max extent to move the xlabel. We may also need to + % move the title + xlab = get(ax,'XLabel'); + titleh = get( ax, 'Title' ); + set( [xlab,titleh], 'Units', 'Normalized' ); + if strcmpi( get( ax, 'XAxisLocation' ), 'Top' ) + titleExtent = get( xlab, 'Extent' ); + set( xlab, 'Position', [0.5 1+labelHeight-titleExtent(4) 0] ); + set( titleh, 'Position', [0.5 1+labelHeight 0] ); + else + set( xlab, 'Position', [0.5 -labelHeight 0] ); + set( titleh, 'Position', [0.5 1 0] ); + end + addListeners( ax ); + end % repositionXLabel + +%-------------------------------------------------------------------------% + function height = getLabelHeight(ax) + height = 0; + textLabels = findall( ax, 'Tag', 'RotatedXTickLabel' ); + if isempty(textLabels) + return; + end + oldUnits = get( textLabels(1), 'Units' ); + set( textLabels, 'Units', 'Normalized' ); + for ll=1:numel(vals) + ext = get( textLabels(ll), 'Extent' ); + if ext(4) > height + height = ext(4); + end + end + set( textLabels, 'Units', oldUnits ); + end % getLabelExtent + +%-------------------------------------------------------------------------% + function updateFont() + % Update the rotated text fonts when the axes font changes + properties = { + 'FontName' + 'FontSize' + 'FontAngle' + 'FontWeight' + 'FontUnits' + }; + propertyValues = get( ax, properties ); + textLabels = findall( ax, 'Tag', 'RotatedXTickLabel' ); + set( textLabels, properties, propertyValues ); + end % updateFont + + function updateAlignment() + textLabels = findall( ax, 'Tag', 'RotatedXTickLabel' ); + angle = get( textLabels(1), 'Rotation' ); + % Depending on the angle, we may need to change the alignment. We change + % alignments within 5 degrees of each 90 degree orientation. + if strcmpi( get( ax, 'XAxisLocation' ), 'Top' ) + if 0 <= angle && angle < 5 + set( textLabels, 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Bottom' ); + elseif 5 <= angle && angle < 85 + set( textLabels, 'HorizontalAlignment', 'Left', 'VerticalAlignment', 'Bottom' ); + elseif 85 <= angle && angle < 95 + set( textLabels, 'HorizontalAlignment', 'Left', 'VerticalAlignment', 'Middle' ); + elseif 95 <= angle && angle < 175 + set( textLabels, 'HorizontalAlignment', 'Left', 'VerticalAlignment', 'Top' ); + elseif 175 <= angle && angle < 185 + set( textLabels, 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Top' ); + elseif 185 <= angle && angle < 265 + set( textLabels, 'HorizontalAlignment', 'Right', 'VerticalAlignment', 'Top' ); + elseif 265 <= angle && angle < 275 + set( textLabels, 'HorizontalAlignment', 'Right', 'VerticalAlignment', 'Middle' ); + elseif 275 <= angle && angle < 355 + set( textLabels, 'HorizontalAlignment', 'Right', 'VerticalAlignment', 'Bottom' ); + else % 355-360 + set( textLabels, 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Bottom' ); + end + else + if 0 <= angle && angle < 5 + set( textLabels, 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Top' ); + elseif 5 <= angle && angle < 85 + set( textLabels, 'HorizontalAlignment', 'Right', 'VerticalAlignment', 'Top' ); + elseif 85 <= angle && angle < 95 + set( textLabels, 'HorizontalAlignment', 'Right', 'VerticalAlignment', 'Middle' ); + elseif 95 <= angle && angle < 175 + set( textLabels, 'HorizontalAlignment', 'Right', 'VerticalAlignment', 'Bottom' ); + elseif 175 <= angle && angle < 185 + set( textLabels, 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Bottom' ); + elseif 185 <= angle && angle < 265 + set( textLabels, 'HorizontalAlignment', 'Left', 'VerticalAlignment', 'Bottom' ); + elseif 265 <= angle && angle < 275 + set( textLabels, 'HorizontalAlignment', 'Left', 'VerticalAlignment', 'Middle' ); + elseif 275 <= angle && angle < 355 + set( textLabels, 'HorizontalAlignment', 'Left', 'VerticalAlignment', 'Top' ); + else % 355-360 + set( textLabels, 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Top' ); + end + end + end + +%-------------------------------------------------------------------------% + function onAxesFontChanged( ~, ~ ) + updateFont(); + repositionAxes( ax ); + repositionXLabel( ax ); + end % onAxesFontChanged + +%-------------------------------------------------------------------------% + function onAxesPositionChanged( ~, ~ ) + % We need to accept the new position, so remove the appdata before + % redrawing + if isappdata( ax, 'OriginalAxesPosition' ) + rmappdata( ax, 'OriginalAxesPosition' ); + end + if isappdata( ax, 'OriginalXLabelPosition' ) + rmappdata( ax, 'OriginalXLabelPosition' ); + end + repositionAxes( ax ); + repositionXLabel( ax ); + end % onAxesPositionChanged + +%-------------------------------------------------------------------------% + function onXAxisLocationChanged( ~, ~ ) + updateAlignment(); + repositionAxes( ax ); + repositionXLabel( ax ); + end % onXAxisLocationChanged + +%-------------------------------------------------------------------------% + function onAxesLimitsChanged( ~, ~ ) + % The limits have moved, so make sure the labels are still ok + textLabels = findall( ax, 'Tag', 'RotatedXTickLabel' ); + xlim = get( ax, 'XLim' ); + ylim = get( ax, 'YLim' ); + if strcmpi( get( ax, 'XAxisLocation'), 'Bottom' ) + pos = [0 ylim(1)]; + else + pos = [0 ylim(2)]; + end + for tt=1:numel( textLabels ) + xval = get( textLabels(tt), 'UserData' ); + pos(1) = xval; + % If the tick is off the edge, make it invisible + if xvalxlim(2) + set( textLabels(tt), 'Visible', 'off', 'Position', pos ) + elseif ~strcmpi( get( textLabels(tt), 'Visible' ), 'on' ) + set( textLabels(tt), 'Visible', 'on', 'Position', pos ) + else + % Just set the position + set( textLabels(tt), 'Position', pos ); + end + end + + repositionXLabel( ax ); + end % onAxesPositionChanged + +%-------------------------------------------------------------------------% + function onTextLabelDeleted( ~, ~ ) + % The final text label has been deleted. This is likely from a + % "cla" or "close" call, so we should remove all of our dirty + % hacks. + restoreDefaultLabels(ax); + end + +%-------------------------------------------------------------------------% + function addListeners( ax ) + % Create listeners. We store the array of listeners in the axes to make + % sure that they have the same life-span as the axes they are listening to. + axh = handle( ax ); + listeners = [ + handle.listener( axh, findprop( axh, 'FontName' ), 'PropertyPostSet', @onAxesFontChanged ) + handle.listener( axh, findprop( axh, 'FontSize' ), 'PropertyPostSet', @onAxesFontChanged ) + handle.listener( axh, findprop( axh, 'FontWeight' ), 'PropertyPostSet', @onAxesFontChanged ) + handle.listener( axh, findprop( axh, 'FontAngle' ), 'PropertyPostSet', @onAxesFontChanged ) + handle.listener( axh, findprop( axh, 'FontUnits' ), 'PropertyPostSet', @onAxesFontChanged ) + handle.listener( axh, findprop( axh, 'OuterPosition' ), 'PropertyPostSet', @onAxesPositionChanged ) + handle.listener( axh, findprop( axh, 'XLim' ), 'PropertyPostSet', @onAxesLimitsChanged ) + handle.listener( axh, findprop( axh, 'YLim' ), 'PropertyPostSet', @onAxesLimitsChanged ) + handle.listener( axh, findprop( axh, 'XAxisLocation' ), 'PropertyPostSet', @onXAxisLocationChanged ) + ]; + setappdata( ax, 'RotateXLabelsListeners', listeners ); + end % addListeners + +%-------------------------------------------------------------------------% + function removeListeners( ax ) + % Rempove any property listeners whilst we are fiddling with the axes + if isappdata( ax, 'RotateXLabelsListeners' ) + delete( getappdata( ax, 'RotateXLabelsListeners' ) ); + rmappdata( ax, 'RotateXLabelsListeners' ); + end + end % removeListeners + + + +end % EOF \ No newline at end of file diff --git a/schooldrugs/run_demographic_groups.m b/schooldrugs/run_demographic_groups.m new file mode 100644 index 0000000..0e1a569 --- /dev/null +++ b/schooldrugs/run_demographic_groups.m @@ -0,0 +1,68 @@ + +ques1= {'Q18'; 'Q23'; 'Q56'}; +ques2= {'Q20'; 'Q24'; 'Q57'}; +label= {'alcohol', 'marijuana', 'cigarettes'}; +files=dir(fullfile('C:','Users','kruggles7','Documents','MATLAB', 'Rajan', 'heatmaps_final','NaN_files', '*.txt')); +final_mat=zeros(3, 15, 6); +for i=1:numel(ques1); + cd .. + cd .. + cd data + cd results_020314 + cd NaN + question_mat1=importdata([ques1{i} '--NaN.txt'], '\t'); + question_mat2=importdata([ques2{i} '--NaN.txt'], '\t'); + cd .. + cd .. + cd .. + cd programs + cd schooldrugs + [ permat1, permat2, ratio_mat ] = plot_demographic_graphs ( question_mat1, question_mat2, label{i} ); + final_mat(i,:,:)=ratio_mat; +end + +label_year={'2001', '2003','2005','2007','2009','2011'}; +label_cell2={'Total', 'Boys', 'Girls', 'W', 'B', 'H', 'O', 'W Boys', 'W Girls', 'B Boys', 'B Girls', 'H Boys', 'H Girls', 'O Boys', 'O Girls'}; +x=2001:2:2011; +colors={'g', 'm', 'b'}; +markers={'o', '*', 's'}; +count=1; +for i=1:15 + for j=1:3 + f2=subplot(30,1,count:count+1); + y=final_mat(j,i,:); + y=reshape(y,1,6); + plot(x, y, 'Color', colors{j}, 'Marker', markers{j}, 'MarkerFaceColor', colors{j}, 'MarkerSize', 2); + hold on + end + hold off + ylabel([label_cell2{i}]); + set(f2, 'xTick', x); + ylim ([0 1]); + if i<15 + %set(f2, 'xTick', []); + %set(f2, 'yTick', []); + set(f2,'xTickLabel',[]); + else + %set(f2, 'yTick', []); + set(f2, 'xTickLabel', label_year); + end + if i>1 %move the plot upward + pos1=get(f1,'Position'); + pos2=get(f2,'Position'); + %set widths equal + pos2(3)=pos1(3); + set (f2, 'Position', pos2); + %set x of first equal to second + pos1(1)=pos2(1); + set(f1, 'Position', pos1); + %move the second to touch first + pos2(2)= pos1(2) - pos2(4); %was pos1(3) + set (f2, 'Position', pos2); + end + f1=f2; + count=count+2; +end +saveas (gcf, ['school_drug_ratio.fig']); +print (gcf, '-dpng', ['school_drug_ratio.png']); +close all \ No newline at end of file diff --git a/schooldrugs/run_demographic_groupsv2.m b/schooldrugs/run_demographic_groupsv2.m new file mode 100644 index 0000000..2f8fdfe --- /dev/null +++ b/schooldrugs/run_demographic_groupsv2.m @@ -0,0 +1,68 @@ + +% ques1= {'Q18'; 'Q23'; 'Q56'}; +% ques2= {'Q20'; 'Q24'; 'Q57'}; +% label= {'alcohol', 'marijuana', 'cigarettes'}; +% files=dir(fullfile('C:','Users','kruggles7','Documents','MATLAB', 'Rajan', 'heatmaps_final','NaN_files', '*.txt')); +% final_mat=zeros(3, 15, 6); +% for i=1:numel(ques1); +% cd .. +% cd .. +% cd data +% cd results_020314 +% cd NaN +% question_mat1=importdata([ques1{i} '--NaN.txt'], '\t'); +% question_mat2=importdata([ques2{i} '--NaN.txt'], '\t'); +% cd .. +% cd .. +% cd .. +% cd programs +% cd schooldrugs +% [ permat1, permat2, ratio_mat ] = plot_demographic_graphs ( question_mat1, question_mat2, label{i} ); +% final_mat(i,:,:)=ratio_mat; +% end + +label_year={'2001', '2003','2005','2007','2009','2011'}; +label_cell2={'Total', 'Boys', 'Girls', 'W', 'B', 'H', 'O', 'W Boys', 'W Girls', 'B Boys', 'B Girls', 'H Boys', 'H Girls', 'O Boys', 'O Girls'}; +x=2001:2:2011; +colors={'g', 'm', 'b'}; +markers={'o', '*', 's'}; +count=1; +for i=1:15 + for j=1:3 + f2=subplot(30,count:count+1,1); + y=final_mat(j,i,:); + y=reshape(y,1,6); + plot(x, y, 'Color', colors{j}, 'Marker', markers{j}, 'MarkerFaceColor', colors{j}, 'MarkerSize', 2); + hold on + end + hold off + ylabel([label_cell2{i}]); + set(f2, 'xTick', x); + ylim ([0 1]); + if i<15 + %set(f2, 'xTick', []); + %set(f2, 'yTick', []); + set(f2,'xTickLabel',[]); + else + %set(f2, 'yTick', []); + set(f2, 'xTickLabel', label_year); + end + if i>1 %move the plot upward + pos1=get(f1,'Position'); + pos2=get(f2,'Position'); + %set heights equal + pos2(4)=pos1(4); + set (f2, 'Position', pos2); + %set x of first equal to second + pos1(2)=pos2(2); + set(f1, 'Position', pos1); + %move the second to touch first + pos2(1)= pos1(1) - pos2(3); %was pos1(3) + set (f2, 'Position', pos2); + end + f1=f2; + count=count+2; +end +saveas (gcf, ['school_drug_ratio.fig']); +print (gcf, '-dpng', ['school_drug_ratio.png']); +close all \ No newline at end of file diff --git a/schooldrugs/run_demographic_groupsv3.m b/schooldrugs/run_demographic_groupsv3.m new file mode 100644 index 0000000..cc604f9 --- /dev/null +++ b/schooldrugs/run_demographic_groupsv3.m @@ -0,0 +1,22 @@ + +ques1= {'Q18'; 'Q23'; 'Q56'}; +ques2= {'Q20'; 'Q24'; 'Q57'}; +label= {'alcohol', 'marijuana', 'cigarettes'}; +files=dir(fullfile('C:','Users','kruggles7','Documents','MATLAB', 'Rajan', 'heatmaps_final','NaN_files', '*.txt')); +final_mat=zeros(3, 15, 6); +for i=1:numel(ques1); + cd .. + cd .. + cd data + cd results_020314 + cd NaN + question_mat1=importdata([ques1{i} '--NaN.txt'], '\t'); + question_mat2=importdata([ques2{i} '--NaN.txt'], '\t'); + cd .. + cd .. + cd .. + cd programs + cd schooldrugs + [ permat1, permat2, ratio_mat ] = plot_demographic_graphsv3 ( question_mat1, question_mat2, label{i} ); + final_mat(i,:,:)=ratio_mat; +end diff --git a/usda_guidelines/USDA.m b/usda_guidelines/USDA.m new file mode 100644 index 0000000..89b0bde --- /dev/null +++ b/usda_guidelines/USDA.m @@ -0,0 +1,174 @@ +function []=USDA(sex, grade, race, weight, question_mat, cutoff, label) + [r,c]=size(question_mat); + label_year={'2001', '2003', '2005', '2007','2009','2011'}; + if r<6 + n=6-r; + s=n+1; + sex=sex(s:6,:); + race=race(s:6,:); + weight=weight(s:6,:); + grade=grade(s:6,:); + label_year=label_year(s:6); + end + + for i=1:r + % total(i)=TOTAL(i,1); + index_yes{i}=find(question_mat(i,:)>=cutoff); + index_girls{i}=find(sex(i,:)==1); + index_boys{i}=find(sex(i,:)==2); + index_W{i}=find(race(i,:)== 1 ); + index_B{i}=find(race(i,:)== 2 ); + index_H{i}=find(race(i,:)== 3 ); + index_O{i}=find(race(i,:)== 4 ); + index_9{i}=find(grade(i,:)== 1 ); + index_10{i}=find(grade(i,:)== 2 ); + index_11{i}=find(grade(i,:)== 3 ); + index_12{i}=find(grade(i,:)== 4 ); + end + % ^for each desired variable we created an array of the indexes (ie. students) who are a "yes" in that variable + + + for i=1:r + index_missQ{i}=find(isnan(question_mat(i,:))==1); %students who didn't answer the Q + index_nomiss{i}=find(isnan(question_mat(i,:))==0); %answers that were NOT missing (ie. 0's and 1's / no's and yes's) + end + + % for i=1:r + % missQ(i)=length(index_missQ{i}); %number of students who answered the question each year + % end + + + for i=1:r + index_total_b{i}=intersect(index_nomiss{i},index_boys{i}); %index of all boys who answered + index_total_g{i}=intersect(index_nomiss{i},index_girls{i}); %index of all girls who answered + end + + % totals of each race/sex/combo: (for percentages- to compare those who said yes to total questioned) + for i=1:r + w=weight(i,:)'; + total_ans(i)=nansum(w(index_nomiss{i})); + total_girls(i)=nansum(w(index_total_g{i})); %total # of girls who answered + total_boys(i)=nansum(w(index_total_b{i})); %total number of boys who answered + total_W{i}=nansum(w(intersect(index_nomiss{i}, index_W{i}))); %total # of white students who answered + total_B{i}=nansum(w(intersect(index_nomiss{i}, index_B{i}))); %total # of black students who answered + total_H{i}=nansum(w(intersect(index_nomiss{i}, index_H{i}))); %total # of hispanic students who answered + total_O{i}=nansum(w(intersect(index_nomiss{i}, index_O{i}))); %total # of "other" students who answered + total_Wb(i)=nansum(w(intersect(index_total_b{i},index_W{i}))); + total_Wg(i)=nansum(w(intersect(index_total_g{i},index_W{i}))); + total_Bb(i)=nansum(w(intersect(index_total_b{i},index_B{i}))); + total_Bg(i)=nansum(w(intersect(index_total_g{i},index_B{i}))); + total_Hb(i)=nansum(w(intersect(index_total_b{i},index_H{i}))); + total_Hg(i)=nansum(w(intersect(index_total_g{i},index_H{i}))); + total_Ob(i)=nansum(w(intersect(index_total_b{i},index_O{i}))); + total_Og(i)=nansum(w(intersect(index_total_g{i},index_O{i}))); + total_9(i)=nansum(w(intersect((index_9{i}),index_nomiss{i}))); + total_10(i)=nansum(w(intersect((index_10{i}),index_nomiss{i}))); + total_11(i)=nansum(w(intersect((index_11{i}),index_nomiss{i}))); + total_12(i)=nansum(w(intersect((index_12{i}),index_nomiss{i}))); + total_9G(i)=nansum(w(intersect(index_9{i},index_total_g{i}))); + total_10G(i)=nansum(w(intersect(index_10{i},index_total_g{i}))); + total_11G(i)=nansum(w(intersect(index_11{i},index_total_g{i}))); + total_12G(i)=nansum(w(intersect(index_12{i},index_total_g{i}))); + total_9B(i)=nansum(w(intersect(index_9{i},index_total_b{i}))); + total_10B(i)=nansum(w(intersect(index_10{i},index_total_b{i}))); + total_11B(i)=nansum(w(intersect(index_11{i},index_total_b{i}))); + total_12B(i)=nansum(w(intersect(index_12{i},index_total_b{i}))); + end + + % %yes-es: + for i=1:r + w=weight(i,:)'; + index_yesgirls{i}=intersect(index_yes{i},index_girls{i}); + index_yesboys{i}=intersect(index_yes{i},index_boys{i}); + yes_girls(i)=nansum(w(index_yesgirls{i})); + yes_boys(i)=nansum(w(index_yesboys{i})); + total_yes(i)=nansum(w(index_yes{i})); + yes_W(i)=nansum(w(intersect(index_yes{i}, index_W{i}))); + yes_B(i)=nansum(w(intersect(index_yes{i}, index_B{i}))); + yes_H(i)=nansum(w(intersect(index_yes{i}, index_H{i}))); + yes_O(i)=nansum(w(intersect(index_yes{i}, index_O{i}))); + yes_WG(i)=nansum(w(intersect(index_yesgirls{i},index_W{i}))); + yes_BG(i)=nansum(w(intersect(index_yesgirls{i},index_B{i}))); + yes_HG(i)=nansum(w(intersect(index_yesgirls{i},index_H{i}))); + yes_OG(i)=nansum(w(intersect(index_yesgirls{i},index_O{i}))); + yes_WB(i)=nansum(w(intersect(index_yesboys{i},index_W{i}))); + yes_BB(i)=nansum(w(intersect(index_yesboys{i},index_B{i}))); + yes_HB(i)=nansum(w(intersect(index_yesboys{i},index_H{i}))); + yes_OB(i)=nansum(w(intersect(index_yesboys{i},index_O{i}))); + yes_9(i)=nansum(w(intersect(index_yes{i},index_9{i}))); + yes_10(i)=nansum(w(intersect(index_yes{i},index_10{i}))); + yes_11(i)=nansum(w(intersect(index_yes{i},index_11{i}))); + yes_12(i)=nansum(w(intersect(index_yes{i},index_12{i}))); + yes_9B(i)=nansum(w(intersect(index_yesboys{i},index_9{i}))); + yes_10B(i)=nansum(w(intersect(index_yesboys{i},index_10{i}))); + yes_11B(i)=nansum(w(intersect(index_yesboys{i},index_11{i}))); + yes_12B(i)=nansum(w(intersect(index_yesboys{i},index_12{i}))); + yes_9G(i)=nansum(w(intersect(index_yesgirls{i},index_9{i}))); + yes_10G(i)=nansum(w(intersect(index_yesgirls{i},index_10{i}))); + yes_11G(i)=nansum(w(intersect(index_yesgirls{i},index_11{i}))); + yes_12G(i)=nansum(w(intersect(index_yesgirls{i},index_12{i}))); + %total_yes(i)=nansum(w(intersect(index_yes{i},index_nomiss{i}))); + total_w(i)=total_W{i}; + total_b(i)=total_B{i}; + total_h(i)=total_H{i}; + total_o(i)=total_O{i}; + end + + %put them all into a matrix as percent values: + per_mat = zeros(27,r); + for i=1:r + per_mat(1, i)=total_yes(i)/total_ans(i)*100; %total + per_mat(2, i)=yes_boys(i)/total_boys(i)*100; %boys + per_mat(3, i)=yes_girls(i)/total_girls(i)*100; %girls + per_mat(4, i)=yes_W(i)/total_w(i)*100; %whites + per_mat(5, i)=yes_B(i)/total_b(i)*100; %blacks + per_mat(6, i)=yes_H(i)/total_h(i)*100; %hispanics + per_mat(7, i)=yes_O(i)/total_o(i)*100; %other + per_mat(8, i)=yes_WB(i)/total_Wb(i)*100; %WB + per_mat(9, i)=yes_WG(i)/total_Wg(i)*100; %WG + per_mat(10, i)=yes_BB(i)/total_Bb(i)*100; %BB + per_mat(11, i)=yes_BG(i)/total_Bg(i)*100; %BG + per_mat(12, i)=yes_HB(i)/total_Hb(i)*100; %HB + per_mat(13, i)=yes_HG(i)/total_Hg(i)*100; %HG + per_mat(14, i)=yes_OB(i)/total_Ob(i)*100; %OB + per_mat(15, i)=yes_OG(i)/total_Og(i)*100; %OG + per_mat(16, i)=yes_9(i)/total_9(i)*100; + per_mat(17, i)=yes_10(i)/total_10(i)*100; + per_mat(18, i)=yes_11(i)/total_11(i)*100; + per_mat(19, i)=yes_12(i)/total_12(i)*100; + per_mat(20, i)=yes_9B(i)/total_9B(i)*100; + per_mat(21, i)=yes_10B(i)/total_10B(i)*100; + per_mat(22, i)=yes_11B(i)/total_11B(i)*100; + per_mat(23, i)=yes_12B(i)/total_12B(i)*100; + per_mat(24, i)=yes_9G(i)/total_9G(i)*100; + per_mat(25, i)=yes_10G(i)/total_10G(i)*100; + per_mat(26, i)=yes_11G(i)/total_11G(i)*100; + per_mat(27, i)=yes_12G(i)/total_12G(i)*100; + end + + + label_cell2={'Total', 'Boys', 'Girls', 'W', 'B', 'H', 'O', 'W Boys', 'W Girls', 'B Boys', 'B Girls', 'H Boys', 'H Girls', 'O Boys', 'O Girls', '9th','10th','11th','12th', '9th-Boys', '10th-Boys', '11th-Boys', '12th-Boys', '9th-Girls', '10th-Girls', '11th-Girls', '12th-Girls' }; + %per_mat_map=flipdim(per_mat_map,1); + max_mat=max(max(per_mat)); + if max_mat>75 + M=100; + elseif max_mat>50 + M=75; + elseif max_mat>25 + M=50; + else + M=25; + end + %get rid of deimals + per_mat=per_mat*10; + per_mat=round(per_mat); + per_mat=per_mat/10; + h=figure; + %M=25; + [hImage]=heatmap_rb(per_mat, label_year, label_cell2, 1, M, 0, 'Colormap','money', 'UseLogColormap', false, 'ShowAllTicks',true, 'Colorbar',true,'TextColor','k', 'FontSize', 12); + %title (title1, 'FontSize', 12); + set (gca, 'FontSize',12); + print (gcf,'-dpng', [label '.png' ]); %can make pdf, jnp, or jpg + + +end \ No newline at end of file diff --git a/usda_guidelines/USDA_2mat.m b/usda_guidelines/USDA_2mat.m new file mode 100644 index 0000000..ef689e8 --- /dev/null +++ b/usda_guidelines/USDA_2mat.m @@ -0,0 +1,164 @@ +function []=USDA_2mat(question_mat, cutoff,question_mat2, cutoff2, label) + + for i=1:r + % total(i)=TOTAL(i,1); + index_yes{i}=find(question_mat(i,:)>=cutoff && question_mat2(i,:)>=cutoff2); + index_girls{i}=find(sex(i,:)==1); + index_boys{i}=find(sex(i,:)==2); + index_W{i}=find(race(i,:)== 1 ); + index_B{i}=find(race(i,:)== 2 ); + index_H{i}=find(race(i,:)== 3 ); + index_O{i}=find(race(i,:)== 4 ); + index_9{i}=find(grade(i,:)== 1 ); + index_10{i}=find(grade(i,:)== 2 ); + index_11{i}=find(grade(i,:)== 3 ); + index_12{i}=find(grade(i,:)== 4 ); + end + % ^for each desired variable we created an array of the indexes (ie. students) who are a "yes" in that variable + + + for i=1:r + index_missQ{i}=find(question_mat(i,:)== 0 & question_mat2(i,:)==0); %students who didn't answer the Q + index_nomiss{i}=find(question_mat(i,:)>0 & question_mat2(i,:)>0); %answers that were NOT missing (ie. 0's and 1's / no's and yes's) + end + + % for i=1:r + % missQ(i)=length(index_missQ{i}); %number of students who answered the question each year + % end + + + for i=1:r + index_total_b{i}=intersect(index_nomiss{i},index_boys{i}); %index of all boys who answered + index_total_g{i}=intersect(index_nomiss{i},index_girls{i}); %index of all girls who answered + end + + % totals of each race/sex/combo: (for percentages- to compare those who said yes to total questioned) + for i=1:r + w=weight(i,:)'; + total_ans(i)=nansum(w(index_nomiss{i})); + total_girls(i)=nansum(w(index_total_g{i})); %total # of girls who answered + total_boys(i)=nansum(w(index_total_b{i})); %total number of boys who answered + total_W{i}=nansum(w(intersect(index_nomiss{i}, index_W{i}))); %total # of white students who answered + total_B{i}=nansum(w(intersect(index_nomiss{i}, index_B{i}))); %total # of black students who answered + total_H{i}=nansum(w(intersect(index_nomiss{i}, index_H{i}))); %total # of hispanic students who answered + total_O{i}=nansum(w(intersect(index_nomiss{i}, index_O{i}))); %total # of "other" students who answered + total_Wb(i)=nansum(w(intersect(index_total_b{i},index_W{i}))); + total_Wg(i)=nansum(w(intersect(index_total_g{i},index_W{i}))); + total_Bb(i)=nansum(w(intersect(index_total_b{i},index_B{i}))); + total_Bg(i)=nansum(w(intersect(index_total_g{i},index_B{i}))); + total_Hb(i)=nansum(w(intersect(index_total_b{i},index_H{i}))); + total_Hg(i)=nansum(w(intersect(index_total_g{i},index_H{i}))); + total_Ob(i)=nansum(w(intersect(index_total_b{i},index_O{i}))); + total_Og(i)=nansum(w(intersect(index_total_g{i},index_O{i}))); + total_9(i)=nansum(w(intersect((index_9{i}),index_nomiss{i}))); + total_10(i)=nansum(w(intersect((index_10{i}),index_nomiss{i}))); + total_11(i)=nansum(w(intersect((index_11{i}),index_nomiss{i}))); + total_12(i)=nansum(w(intersect((index_12{i}),index_nomiss{i}))); + total_9G(i)=nansum(w(intersect(index_9{i},index_total_g{i}))); + total_10G(i)=nansum(w(intersect(index_10{i},index_total_g{i}))); + total_11G(i)=nansum(w(intersect(index_11{i},index_total_g{i}))); + total_12G(i)=nansum(w(intersect(index_12{i},index_total_g{i}))); + total_9B(i)=nansum(w(intersect(index_9{i},index_total_b{i}))); + total_10B(i)=nansum(w(intersect(index_10{i},index_total_b{i}))); + total_11B(i)=nansum(w(intersect(index_11{i},index_total_b{i}))); + total_12B(i)=nansum(w(intersect(index_12{i},index_total_b{i}))); + end + + % %yes-es: + for i=1:r + w=weight(i,:)'; + index_yesgirls{i}=intersect(index_yes{i},index_girls{i}); + index_yesboys{i}=intersect(index_yes{i},index_boys{i}); + yes_girls(i)=nansum(w(index_yesgirls{i})); + yes_boys(i)=nansum(w(index_yesboys{i})); + total_yes(i)=nansum(w(index_yes{i})); + yes_W(i)=nansum(w(intersect(index_yes{i}, index_W{i}))); + yes_B(i)=nansum(w(intersect(index_yes{i}, index_B{i}))); + yes_H(i)=nansum(w(intersect(index_yes{i}, index_H{i}))); + yes_O(i)=nansum(w(intersect(index_yes{i}, index_O{i}))); + yes_WG(i)=nansum(w(intersect(index_yesgirls{i},index_W{i}))); + yes_BG(i)=nansum(w(intersect(index_yesgirls{i},index_B{i}))); + yes_HG(i)=nansum(w(intersect(index_yesgirls{i},index_H{i}))); + yes_OG(i)=nansum(w(intersect(index_yesgirls{i},index_O{i}))); + yes_WB(i)=nansum(w(intersect(index_yesboys{i},index_W{i}))); + yes_BB(i)=nansum(w(intersect(index_yesboys{i},index_B{i}))); + yes_HB(i)=nansum(w(intersect(index_yesboys{i},index_H{i}))); + yes_OB(i)=nansum(w(intersect(index_yesboys{i},index_O{i}))); + yes_9(i)=nansum(w(intersect(index_yes{i},index_9{i}))); + yes_10(i)=nansum(w(intersect(index_yes{i},index_10{i}))); + yes_11(i)=nansum(w(intersect(index_yes{i},index_11{i}))); + yes_12(i)=nansum(w(intersect(index_yes{i},index_12{i}))); + yes_9B(i)=nansum(w(intersect(index_yesboys{i},index_9{i}))); + yes_10B(i)=nansum(w(intersect(index_yesboys{i},index_10{i}))); + yes_11B(i)=nansum(w(intersect(index_yesboys{i},index_11{i}))); + yes_12B(i)=nansum(w(intersect(index_yesboys{i},index_12{i}))); + yes_9G(i)=nansum(w(intersect(index_yesgirls{i},index_9{i}))); + yes_10G(i)=nansum(w(intersect(index_yesgirls{i},index_10{i}))); + yes_11G(i)=nansum(w(intersect(index_yesgirls{i},index_11{i}))); + yes_12G(i)=nansum(w(intersect(index_yesgirls{i},index_12{i}))); + %total_yes(i)=nansum(w(intersect(index_yes{i},index_nomiss{i}))); + total_w(i)=total_W{i}; + total_b(i)=total_B{i}; + total_h(i)=total_H{i}; + total_o(i)=total_O{i}; + end + + %put them all into a matrix as percent values: + per_mat = zeros(27,r); + for i=1:r + per_mat(1, i)=total_yes(i)/total_ans(i)*100; %total + per_mat(2, i)=yes_boys(i)/total_boys(i)*100; %boys + per_mat(3, i)=yes_girls(i)/total_girls(i)*100; %girls + per_mat(4, i)=yes_W(i)/total_w(i)*100; %whites + per_mat(5, i)=yes_B(i)/total_b(i)*100; %blacks + per_mat(6, i)=yes_H(i)/total_h(i)*100; %hispanics + per_mat(7, i)=yes_O(i)/total_o(i)*100; %other + per_mat(8, i)=yes_WB(i)/total_Wb(i)*100; %WB + per_mat(9, i)=yes_WG(i)/total_Wg(i)*100; %WG + per_mat(10, i)=yes_BB(i)/total_Bb(i)*100; %BB + per_mat(11, i)=yes_BG(i)/total_Bg(i)*100; %BG + per_mat(12, i)=yes_HB(i)/total_Hb(i)*100; %HB + per_mat(13, i)=yes_HG(i)/total_Hg(i)*100; %HG + per_mat(14, i)=yes_OB(i)/total_Ob(i)*100; %OB + per_mat(15, i)=yes_OG(i)/total_Og(i)*100; %OG + per_mat(16, i)=yes_9(i)/total_9(i)*100; + per_mat(17, i)=yes_10(i)/total_10(i)*100; + per_mat(18, i)=yes_11(i)/total_11(i)*100; + per_mat(19, i)=yes_12(i)/total_12(i)*100; + per_mat(20, i)=yes_9B(i)/total_9B(i)*100; + per_mat(21, i)=yes_10B(i)/total_10B(i)*100; + per_mat(22, i)=yes_11B(i)/total_11B(i)*100; + per_mat(23, i)=yes_12B(i)/total_12B(i)*100; + per_mat(24, i)=yes_9G(i)/total_9G(i)*100; + per_mat(25, i)=yes_10G(i)/total_10G(i)*100; + per_mat(26, i)=yes_11G(i)/total_11G(i)*100; + per_mat(27, i)=yes_12G(i)/total_12G(i)*100; + end + + label_year={'2007','2009','2011'}; + label_cell2={'Total', 'Boys', 'Girls', 'W', 'B', 'H', 'O', 'W Boys', 'W Girls', 'B Boys', 'B Girls', 'H Boys', 'H Girls', 'O Boys', 'O Girls'}; + per_mat_map(1:15,1:r)=per_mat(1:15,1:r); + %per_mat_map=flipdim(per_mat_map,1); + max_mat=max(max(per_mat_map)); + if max_mat>75 + M=100; + elseif max_mat>50 + M=75; + elseif max_mat>25 + M=50; + else + M=25; + end + %get rid of deimals + per_mat_map=per_mat_map*10; + per_mat_map=round(per_mat_map); + per_mat_map=per_mat_map/10; + h=figure; + M=25; + [hImage]=heatmap_rb(per_mat_map, label_year, label_cell2, 1, M, 0, 'Colormap','money', 'UseLogColormap', false, 'ShowAllTicks',true, 'Colorbar',true,'TextColor','k', 'FontSize', 12); + %title (title1, 'FontSize', 12); + set (gca, 'FontSize',12); + print (gcf,'-dpng', [label '.png' ]); %can make pdf, jnp, or jpg + + +end \ No newline at end of file diff --git a/usda_guidelines/USDA_3mat.m b/usda_guidelines/USDA_3mat.m new file mode 100644 index 0000000..80a3730 --- /dev/null +++ b/usda_guidelines/USDA_3mat.m @@ -0,0 +1,164 @@ +function []=USDA_3mat(sex, race, grade, question_mat, cutoff,question_mat2, cutoff2, question_mat3, cutoff3, label) + + for i=1:r + % total(i)=TOTAL(i,1); + index_yes{i}=find(question_mat(i,:)>=cutoff & question_mat2(i,:)>=cutoff2 & question_mat3(i,:)>=cutoff3); + index_girls{i}=find(sex(i,:)==1); + index_boys{i}=find(sex(i,:)==2); + index_W{i}=find(race(i,:)== 1 ); + index_B{i}=find(race(i,:)== 2 ); + index_H{i}=find(race(i,:)== 3 ); + index_O{i}=find(race(i,:)== 4 ); + index_9{i}=find(grade(i,:)== 1 ); + index_10{i}=find(grade(i,:)== 2 ); + index_11{i}=find(grade(i,:)== 3 ); + index_12{i}=find(grade(i,:)== 4 ); + end + % ^for each desired variable we created an array of the indexes (ie. students) who are a "yes" in that variable + + + for i=1:r + index_missQ{i}=find(question_mat(i,:)== 0 & question_mat2(i,:)==0); %students who didn't answer the Q + index_nomiss{i}=find(question_mat(i,:)>0 & question_mat2(i,:)>0); %answers that were NOT missing (ie. 0's and 1's / no's and yes's) + end + + % for i=1:r + % missQ(i)=length(index_missQ{i}); %number of students who answered the question each year + % end + + + for i=1:r + index_total_b{i}=intersect(index_nomiss{i},index_boys{i}); %index of all boys who answered + index_total_g{i}=intersect(index_nomiss{i},index_girls{i}); %index of all girls who answered + end + + % totals of each race/sex/combo: (for percentages- to compare those who said yes to total questioned) + for i=1:r + w=weight(i,:)'; + total_ans(i)=nansum(w(index_nomiss{i})); + total_girls(i)=nansum(w(index_total_g{i})); %total # of girls who answered + total_boys(i)=nansum(w(index_total_b{i})); %total number of boys who answered + total_W{i}=nansum(w(intersect(index_nomiss{i}, index_W{i}))); %total # of white students who answered + total_B{i}=nansum(w(intersect(index_nomiss{i}, index_B{i}))); %total # of black students who answered + total_H{i}=nansum(w(intersect(index_nomiss{i}, index_H{i}))); %total # of hispanic students who answered + total_O{i}=nansum(w(intersect(index_nomiss{i}, index_O{i}))); %total # of "other" students who answered + total_Wb(i)=nansum(w(intersect(index_total_b{i},index_W{i}))); + total_Wg(i)=nansum(w(intersect(index_total_g{i},index_W{i}))); + total_Bb(i)=nansum(w(intersect(index_total_b{i},index_B{i}))); + total_Bg(i)=nansum(w(intersect(index_total_g{i},index_B{i}))); + total_Hb(i)=nansum(w(intersect(index_total_b{i},index_H{i}))); + total_Hg(i)=nansum(w(intersect(index_total_g{i},index_H{i}))); + total_Ob(i)=nansum(w(intersect(index_total_b{i},index_O{i}))); + total_Og(i)=nansum(w(intersect(index_total_g{i},index_O{i}))); + total_9(i)=nansum(w(intersect((index_9{i}),index_nomiss{i}))); + total_10(i)=nansum(w(intersect((index_10{i}),index_nomiss{i}))); + total_11(i)=nansum(w(intersect((index_11{i}),index_nomiss{i}))); + total_12(i)=nansum(w(intersect((index_12{i}),index_nomiss{i}))); + total_9G(i)=nansum(w(intersect(index_9{i},index_total_g{i}))); + total_10G(i)=nansum(w(intersect(index_10{i},index_total_g{i}))); + total_11G(i)=nansum(w(intersect(index_11{i},index_total_g{i}))); + total_12G(i)=nansum(w(intersect(index_12{i},index_total_g{i}))); + total_9B(i)=nansum(w(intersect(index_9{i},index_total_b{i}))); + total_10B(i)=nansum(w(intersect(index_10{i},index_total_b{i}))); + total_11B(i)=nansum(w(intersect(index_11{i},index_total_b{i}))); + total_12B(i)=nansum(w(intersect(index_12{i},index_total_b{i}))); + end + + % %yes-es: + for i=1:r + w=weight(i,:)'; + index_yesgirls{i}=intersect(index_yes{i},index_girls{i}); + index_yesboys{i}=intersect(index_yes{i},index_boys{i}); + yes_girls(i)=nansum(w(index_yesgirls{i})); + yes_boys(i)=nansum(w(index_yesboys{i})); + total_yes(i)=nansum(w(index_yes{i})); + yes_W(i)=nansum(w(intersect(index_yes{i}, index_W{i}))); + yes_B(i)=nansum(w(intersect(index_yes{i}, index_B{i}))); + yes_H(i)=nansum(w(intersect(index_yes{i}, index_H{i}))); + yes_O(i)=nansum(w(intersect(index_yes{i}, index_O{i}))); + yes_WG(i)=nansum(w(intersect(index_yesgirls{i},index_W{i}))); + yes_BG(i)=nansum(w(intersect(index_yesgirls{i},index_B{i}))); + yes_HG(i)=nansum(w(intersect(index_yesgirls{i},index_H{i}))); + yes_OG(i)=nansum(w(intersect(index_yesgirls{i},index_O{i}))); + yes_WB(i)=nansum(w(intersect(index_yesboys{i},index_W{i}))); + yes_BB(i)=nansum(w(intersect(index_yesboys{i},index_B{i}))); + yes_HB(i)=nansum(w(intersect(index_yesboys{i},index_H{i}))); + yes_OB(i)=nansum(w(intersect(index_yesboys{i},index_O{i}))); + yes_9(i)=nansum(w(intersect(index_yes{i},index_9{i}))); + yes_10(i)=nansum(w(intersect(index_yes{i},index_10{i}))); + yes_11(i)=nansum(w(intersect(index_yes{i},index_11{i}))); + yes_12(i)=nansum(w(intersect(index_yes{i},index_12{i}))); + yes_9B(i)=nansum(w(intersect(index_yesboys{i},index_9{i}))); + yes_10B(i)=nansum(w(intersect(index_yesboys{i},index_10{i}))); + yes_11B(i)=nansum(w(intersect(index_yesboys{i},index_11{i}))); + yes_12B(i)=nansum(w(intersect(index_yesboys{i},index_12{i}))); + yes_9G(i)=nansum(w(intersect(index_yesgirls{i},index_9{i}))); + yes_10G(i)=nansum(w(intersect(index_yesgirls{i},index_10{i}))); + yes_11G(i)=nansum(w(intersect(index_yesgirls{i},index_11{i}))); + yes_12G(i)=nansum(w(intersect(index_yesgirls{i},index_12{i}))); + %total_yes(i)=nansum(w(intersect(index_yes{i},index_nomiss{i}))); + total_w(i)=total_W{i}; + total_b(i)=total_B{i}; + total_h(i)=total_H{i}; + total_o(i)=total_O{i}; + end + + %put them all into a matrix as percent values: + per_mat = zeros(27,r); + for i=1:r + per_mat(1, i)=total_yes(i)/total_ans(i)*100; %total + per_mat(2, i)=yes_boys(i)/total_boys(i)*100; %boys + per_mat(3, i)=yes_girls(i)/total_girls(i)*100; %girls + per_mat(4, i)=yes_W(i)/total_w(i)*100; %whites + per_mat(5, i)=yes_B(i)/total_b(i)*100; %blacks + per_mat(6, i)=yes_H(i)/total_h(i)*100; %hispanics + per_mat(7, i)=yes_O(i)/total_o(i)*100; %other + per_mat(8, i)=yes_WB(i)/total_Wb(i)*100; %WB + per_mat(9, i)=yes_WG(i)/total_Wg(i)*100; %WG + per_mat(10, i)=yes_BB(i)/total_Bb(i)*100; %BB + per_mat(11, i)=yes_BG(i)/total_Bg(i)*100; %BG + per_mat(12, i)=yes_HB(i)/total_Hb(i)*100; %HB + per_mat(13, i)=yes_HG(i)/total_Hg(i)*100; %HG + per_mat(14, i)=yes_OB(i)/total_Ob(i)*100; %OB + per_mat(15, i)=yes_OG(i)/total_Og(i)*100; %OG + per_mat(16, i)=yes_9(i)/total_9(i)*100; + per_mat(17, i)=yes_10(i)/total_10(i)*100; + per_mat(18, i)=yes_11(i)/total_11(i)*100; + per_mat(19, i)=yes_12(i)/total_12(i)*100; + per_mat(20, i)=yes_9B(i)/total_9B(i)*100; + per_mat(21, i)=yes_10B(i)/total_10B(i)*100; + per_mat(22, i)=yes_11B(i)/total_11B(i)*100; + per_mat(23, i)=yes_12B(i)/total_12B(i)*100; + per_mat(24, i)=yes_9G(i)/total_9G(i)*100; + per_mat(25, i)=yes_10G(i)/total_10G(i)*100; + per_mat(26, i)=yes_11G(i)/total_11G(i)*100; + per_mat(27, i)=yes_12G(i)/total_12G(i)*100; + end + + label_year={'2007','2009','2011'}; + label_cell2={'Total', 'Boys', 'Girls', 'W', 'B', 'H', 'O', 'W Boys', 'W Girls', 'B Boys', 'B Girls', 'H Boys', 'H Girls', 'O Boys', 'O Girls'}; + per_mat_map(1:15,1:r)=per_mat(1:15,1:r); + %per_mat_map=flipdim(per_mat_map,1); + max_mat=max(max(per_mat_map)); + if max_mat>75 + M=100; + elseif max_mat>50 + M=75; + elseif max_mat>25 + M=50; + else + M=25; + end + %get rid of deimals + per_mat_map=per_mat_map*10; + per_mat_map=round(per_mat_map); + per_mat_map=per_mat_map/10; + h=figure; + M=25; + [hImage]=heatmap_rb(per_mat_map, label_year, label_cell2, 1, M, 0, 'Colormap','money', 'UseLogColormap', false, 'ShowAllTicks',true, 'Colorbar',true,'TextColor','k', 'FontSize', 12); + %title (title1, 'FontSize', 12); + set (gca, 'FontSize',12); + print (gcf,'-dpng', [label '.png' ]); %can make pdf, jnp, or jpg + + +end \ No newline at end of file diff --git a/usda_guidelines/USDA_fruits_veggies.m b/usda_guidelines/USDA_fruits_veggies.m new file mode 100644 index 0000000..fa93e50 --- /dev/null +++ b/usda_guidelines/USDA_fruits_veggies.m @@ -0,0 +1,175 @@ +function []=USDA_fruits_veggies(question_mat1, question_mat2, question_mat3) + + q_3s=ones(r,c-1); + q_3s=q_3s*3; + + question_final=question_mat1+question_mat2; + question_final=question_final+question_mat3; + question_final=question_final-q_3s; + + for i=1:r + %if fullfill in one question + index_yes1{i}=find(question_mat1(i,:)>=6); + index_yes2{i}=find(question_mat2(i,:)>=6); + index_yes3{i}=find(question_mat3(i,:)>=6); + + + index_girls{i}=find(sex(i,:)==1); + index_boys{i}=find(sex(i,:)==2); + index_W{i}=find(race(i,:)== 1 ); + index_B{i}=find(race(i,:)== 2 ); + index_H{i}=find(race(i,:)== 3 ); + index_O{i}=find(race(i,:)== 4 ); + index_9{i}=find(grade(i,:)== 1 ); + index_10{i}=find(grade(i,:)== 2 ); + index_11{i}=find(grade(i,:)== 3 ); + index_12{i}=find(grade(i,:)== 4 ); + end + % ^for each desired variable we created an array of the indexes (ie. students) who are a "yes" in that variable + + + for i=1:r + index_missQ{i}=find(question_mat(i,:)== 0); %students who didn't answer the Q + index_nomiss{i}=find(question_mat(i,:)>0); %answers that were NOT missing (ie. 0's and 1's / no's and yes's) + end + + % for i=1:r + % missQ(i)=length(index_missQ{i}); %number of students who answered the question each year + % end + + + for i=1:r + index_total_b{i}=intersect(index_nomiss{i},index_boys{i}); %index of all boys who answered + index_total_g{i}=intersect(index_nomiss{i},index_girls{i}); %index of all girls who answered + end + + % totals of each race/sex/combo: (for percentages- to compare those who said yes to total questioned) + for i=1:r + w=weight(i,:)'; + total_ans(i)=nansum(w(index_nomiss{i})); + total_girls(i)=nansum(w(index_total_g{i})); %total # of girls who answered + total_boys(i)=nansum(w(index_total_b{i})); %total number of boys who answered + total_W{i}=nansum(w(intersect(index_nomiss{i}, index_W{i}))); %total # of white students who answered + total_B{i}=nansum(w(intersect(index_nomiss{i}, index_B{i}))); %total # of black students who answered + total_H{i}=nansum(w(intersect(index_nomiss{i}, index_H{i}))); %total # of hispanic students who answered + total_O{i}=nansum(w(intersect(index_nomiss{i}, index_O{i}))); %total # of "other" students who answered + total_Wb(i)=nansum(w(intersect(index_total_b{i},index_W{i}))); + total_Wg(i)=nansum(w(intersect(index_total_g{i},index_W{i}))); + total_Bb(i)=nansum(w(intersect(index_total_b{i},index_B{i}))); + total_Bg(i)=nansum(w(intersect(index_total_g{i},index_B{i}))); + total_Hb(i)=nansum(w(intersect(index_total_b{i},index_H{i}))); + total_Hg(i)=nansum(w(intersect(index_total_g{i},index_H{i}))); + total_Ob(i)=nansum(w(intersect(index_total_b{i},index_O{i}))); + total_Og(i)=nansum(w(intersect(index_total_g{i},index_O{i}))); + total_9(i)=nansum(w(intersect((index_9{i}),index_nomiss{i}))); + total_10(i)=nansum(w(intersect((index_10{i}),index_nomiss{i}))); + total_11(i)=nansum(w(intersect((index_11{i}),index_nomiss{i}))); + total_12(i)=nansum(w(intersect((index_12{i}),index_nomiss{i}))); + total_9G(i)=nansum(w(intersect(index_9{i},index_total_g{i}))); + total_10G(i)=nansum(w(intersect(index_10{i},index_total_g{i}))); + total_11G(i)=nansum(w(intersect(index_11{i},index_total_g{i}))); + total_12G(i)=nansum(w(intersect(index_12{i},index_total_g{i}))); + total_9B(i)=nansum(w(intersect(index_9{i},index_total_b{i}))); + total_10B(i)=nansum(w(intersect(index_10{i},index_total_b{i}))); + total_11B(i)=nansum(w(intersect(index_11{i},index_total_b{i}))); + total_12B(i)=nansum(w(intersect(index_12{i},index_total_b{i}))); + end + + % %yes-es: + for i=1:r + w=weight(i,:)'; + index_yesgirls{i}=intersect(index_yes{i},index_girls{i}); + index_yesboys{i}=intersect(index_yes{i},index_boys{i}); + yes_girls(i)=nansum(w(index_yesgirls{i})); + yes_boys(i)=nansum(w(index_yesboys{i})); + total_yes(i)=nansum(w(index_yes{i})); + yes_W(i)=nansum(w(intersect(index_yes{i}, index_W{i}))); + yes_B(i)=nansum(w(intersect(index_yes{i}, index_B{i}))); + yes_H(i)=nansum(w(intersect(index_yes{i}, index_H{i}))); + yes_O(i)=nansum(w(intersect(index_yes{i}, index_O{i}))); + yes_WG(i)=nansum(w(intersect(index_yesgirls{i},index_W{i}))); + yes_BG(i)=nansum(w(intersect(index_yesgirls{i},index_B{i}))); + yes_HG(i)=nansum(w(intersect(index_yesgirls{i},index_H{i}))); + yes_OG(i)=nansum(w(intersect(index_yesgirls{i},index_O{i}))); + yes_WB(i)=nansum(w(intersect(index_yesboys{i},index_W{i}))); + yes_BB(i)=nansum(w(intersect(index_yesboys{i},index_B{i}))); + yes_HB(i)=nansum(w(intersect(index_yesboys{i},index_H{i}))); + yes_OB(i)=nansum(w(intersect(index_yesboys{i},index_O{i}))); + yes_9(i)=nansum(w(intersect(index_yes{i},index_9{i}))); + yes_10(i)=nansum(w(intersect(index_yes{i},index_10{i}))); + yes_11(i)=nansum(w(intersect(index_yes{i},index_11{i}))); + yes_12(i)=nansum(w(intersect(index_yes{i},index_12{i}))); + yes_9B(i)=nansum(w(intersect(index_yesboys{i},index_9{i}))); + yes_10B(i)=nansum(w(intersect(index_yesboys{i},index_10{i}))); + yes_11B(i)=nansum(w(intersect(index_yesboys{i},index_11{i}))); + yes_12B(i)=nansum(w(intersect(index_yesboys{i},index_12{i}))); + yes_9G(i)=nansum(w(intersect(index_yesgirls{i},index_9{i}))); + yes_10G(i)=nansum(w(intersect(index_yesgirls{i},index_10{i}))); + yes_11G(i)=nansum(w(intersect(index_yesgirls{i},index_11{i}))); + yes_12G(i)=nansum(w(intersect(index_yesgirls{i},index_12{i}))); + %total_yes(i)=nansum(w(intersect(index_yes{i},index_nomiss{i}))); + total_w(i)=total_W{i}; + total_b(i)=total_B{i}; + total_h(i)=total_H{i}; + total_o(i)=total_O{i}; + end + + %put them all into a matrix as percent values: + per_mat = zeros(27,r); + for i=1:r + per_mat(1, i)=total_yes(i)/total_ans(i)*100; %total + per_mat(2, i)=yes_boys(i)/total_boys(i)*100; %boys + per_mat(3, i)=yes_girls(i)/total_girls(i)*100; %girls + per_mat(4, i)=yes_W(i)/total_w(i)*100; %whites + per_mat(5, i)=yes_B(i)/total_b(i)*100; %blacks + per_mat(6, i)=yes_H(i)/total_h(i)*100; %hispanics + per_mat(7, i)=yes_O(i)/total_o(i)*100; %other + per_mat(8, i)=yes_WB(i)/total_Wb(i)*100; %WB + per_mat(9, i)=yes_WG(i)/total_Wg(i)*100; %WG + per_mat(10, i)=yes_BB(i)/total_Bb(i)*100; %BB + per_mat(11, i)=yes_BG(i)/total_Bg(i)*100; %BG + per_mat(12, i)=yes_HB(i)/total_Hb(i)*100; %HB + per_mat(13, i)=yes_HG(i)/total_Hg(i)*100; %HG + per_mat(14, i)=yes_OB(i)/total_Ob(i)*100; %OB + per_mat(15, i)=yes_OG(i)/total_Og(i)*100; %OG + per_mat(16, i)=yes_9(i)/total_9(i)*100; + per_mat(17, i)=yes_10(i)/total_10(i)*100; + per_mat(18, i)=yes_11(i)/total_11(i)*100; + per_mat(19, i)=yes_12(i)/total_12(i)*100; + per_mat(20, i)=yes_9B(i)/total_9B(i)*100; + per_mat(21, i)=yes_10B(i)/total_10B(i)*100; + per_mat(22, i)=yes_11B(i)/total_11B(i)*100; + per_mat(23, i)=yes_12B(i)/total_12B(i)*100; + per_mat(24, i)=yes_9G(i)/total_9G(i)*100; + per_mat(25, i)=yes_10G(i)/total_10G(i)*100; + per_mat(26, i)=yes_11G(i)/total_11G(i)*100; + per_mat(27, i)=yes_12G(i)/total_12G(i)*100; + end + + label_year={'2007','2009','2011'}; + label_cell2={'Total', 'Boys', 'Girls', 'W', 'B', 'H', 'O', 'W Boys', 'W Girls', 'B Boys', 'B Girls', 'H Boys', 'H Girls', 'O Boys', 'O Girls'}; + per_mat_map(1:15,1:r)=per_mat(1:15,1:r); + %per_mat_map=flipdim(per_mat_map,1); + max_mat=max(max(per_mat_map)); + if max_mat>75 + M=100; + elseif max_mat>50 + M=75; + elseif max_mat>25 + M=50; + else + M=25; + end + %get rid of deimals + per_mat_map=per_mat_map*10; + per_mat_map=round(per_mat_map); + per_mat_map=per_mat_map/10; + h=figure; + M=25; + [hImage]=heatmap_rb(per_mat_map, label_year, label_cell2, 1, M, 0, 'Colormap','money', 'UseLogColormap', false, 'ShowAllTicks',true, 'Colorbar',true,'TextColor','k', 'FontSize', 12); + %title (title1, 'FontSize', 12); + set (gca, 'FontSize',12); + print (gcf,'-dpng', [label '.png' ]); %can make pdf, jnp, or jpg + + +end \ No newline at end of file diff --git a/usda_guidelines/USDA_run.m b/usda_guidelines/USDA_run.m new file mode 100644 index 0000000..e1ab03c --- /dev/null +++ b/usda_guidelines/USDA_run.m @@ -0,0 +1,146 @@ +%redo everything with categorical data (>=5)----------------------- +cd .. +cd .. +cd data +cd controls +sex=importdata('sex-NaN.txt', '\t'); +race=importdata('race-NaN.txt', '\t'); +weight=importdata('weight.txt','\t'); +grade=importdata('grade-NaN.txt','\t'); +[r,c]=size(sex); +weight=weight(:,2:c); +race=race(:,2:c); +sex=sex(:,2:c); +grade=grade(:,2:c); + +cd .. +cd .. +cd programs +cd sleep + +%FRUITS AND VEGGIES-------------------------------------------------- +%Vegetables and fruits=2.5-6.5 fruits/vegetables 2010 +%Q42(fruit in 1 week): 1: 0, 2: 1-3 times, 3: 4-6 times, D:1 time/day, E: 2 +%times/day, F:3 times/day, G: 4 times/day +%Q43 salad (in a week): 1: 0, 2: 1-3 times, 3: 4-6 times, D:1 time/day, E: 2 +%times/day, F:3 times/day, G: 4 times/day +%Q46 (vegetables in a week): 1: 0, 2: 1-3 times, 3: 4-6 times, D:1 time/day, E: 2 +%times/day, F:3 times/day, G: 4 times/day +%---make the cutoff at least 2.5 fruits/veggies per day = 2.5*7=17.5~18 +% +cd .. +cd .. +cd data +cd results_020314 +cd cat +question_mat1=importdata('Q42-cat-NaN.txt', '\t'); +[r,c]=size(question_mat1); +question_mat1=question_mat1(:,2:c); +question_mat1(question_mat1==0)=NaN; +question_mat1(question_mat1==7)=4*7; +question_mat1(question_mat1==6)=3*7; +question_mat1(question_mat1==5)=2*7; +question_mat1(question_mat1==4)=7; +question_mat1(question_mat1==3)=4; +question_mat1(question_mat1==2)=1; +question_mat1(question_mat1==1)=0; + +question_mat2=importdata('Q43-cat-NaN.txt', '\t'); +[r,c]=size(question_mat2); +question_mat2=question_mat2(:,2:c); +question_mat2(question_mat2==0)=NaN; +question_mat2(question_mat2==7)=4*7; +question_mat2(question_mat2==6)=3*7; +question_mat2(question_mat2==5)=2*7; +question_mat2(question_mat2==4)=7; +question_mat2(question_mat2==3)=4; +question_mat2(question_mat2==2)=1; +question_mat2(question_mat2==1)=0; + +question_mat3=importdata('Q46-cat-NaN.txt', '\t'); +[r,c]=size(question_mat3); +question_mat3=question_mat3(:,2:c); +question_mat3(question_mat3==0)=NaN; +question_mat3(question_mat3==7)=4*7; +question_mat3(question_mat3==6)=3*7; +question_mat3(question_mat3==5)=2*7; +question_mat3(question_mat3==4)=7; +question_mat3(question_mat3==3)=4; +question_mat3(question_mat3==2)=1; +question_mat3(question_mat3==1)=0; + +question_final=question_mat1+question_mat2; +question_final=question_final+question_mat3; + +label='fruit_veggies_18perweek'; +cd .. +cd .. +cd .. +cd programs +cd USDA_guidelines + +USDA(sex, grade, race, weight, question_final, 18,label); + +%milk, 3 cups per day +%Q47 (milk in a week): 1: 0, 2: 1-3 times, 3: 4-6 times, 4:1 time/day, 5: 2 +%times/day, 6:3 times/day, 7: 4 times/day +cd .. +cd .. +cd data +cd results_020314 +cd cat +question_mat1=importdata('Q47-cat-NaN.txt', '\t'); +[r,c]=size(question_mat1); +question_mat1=question_mat1(:,2:c); +label='milk_3timesday'; +cd .. +cd .. +cd .. +cd programs +cd USDA_guidelines +USDA (sex, grade, race, weight, question_mat1,6, label); + +%physical activities 60 minutes per day +%Q33 (PE class): 1: 0 days, 2: 1 day, 3: 2 days, 4: 3 days, 5: 4 days, 6; 5 +%days +%Q81 (physical activity 60 minutes): 1: 0 days, 2: 1 day, 3: 2 days, 4: 3 +%days, 5: 4 days, 6: 5 days, 7:6 days, 8:7 days + +cd .. +cd .. +cd data +cd results_020314 +cd cat +question_mat1=importdata('Q33-cat-NaN.txt', '\t'); +[r,c]=size(question_mat1); +question_mat1=question_mat1(3:6,2:c); +question_mat1(question_mat1==0)=NaN; +question_mat1(question_mat1==1)=0; +question_mat1(question_mat1==2)=1; +question_mat1(question_mat1==3)=2; +question_mat1(question_mat1==4)=3; +question_mat1(question_mat1==5)=4; +question_mat1(question_mat1==6)=5; + +question_mat2=importdata('Q81-cat-NaN.txt', '\t'); +[r,c]=size(question_mat2); +question_mat2=question_mat2(:,2:c); +question_mat2(question_mat2==0)=NaN; +question_mat2(question_mat2==1)=0; +question_mat2(question_mat2==2)=1; +question_mat2(question_mat2==3)=2; +question_mat2(question_mat2==4)=3; +question_mat2(question_mat2==5)=4; +question_mat2(question_mat2==6)=5; +question_mat2(question_mat2==7)=6; +question_mat2(question_mat2==8)=7; + +question_final=question_mat1+question_mat2; +label='physical_activity_perday'; +cd .. +cd .. +cd .. +cd programs +cd USDA_guidelines + +USDA(sex, grade, race, weight, question_final, 7,label); \ No newline at end of file