% PS02_2.m This is the m-file answer for Problem set 2 question 2 clear t=2; % t is the figure display lifetime clc linespace=[' ']; ans1=['The instructions given in the homework assume that the depth']; ans2=['values for no3 are positive. Because they are actually negative']; ans3=['if you follow Dave''s command D(:,1)=-1*D(:,1), you make them']; ans4=['positive. Not only does the new plot look upside down, but']; ans5=['the filtering commands you issue later don''t work because they']; ans6=['are looking for negative depths. Everything works fine if you']; ans7=['skip the above command. Of course, if you downloaded a corrected'];; ans7_5=['version, you must then use the above command.']; ans8=['Not to beat an already dead horse, if all the depths were positive']; ans9=['and you wanted to plot them increasing down, you could first plot it']; ans10=['and then use the command axis ij to flip the y axis.']; ans11=['One final note before we begin. When Matlab makes a plot, it usually']; ans12=['makes the figure window the active window. The only way I know of so']; ans13=['far, to make the matlab window the active window is to either delete']; ans14=['the figure after a certain time as I did in PS02_1 or use the mouse']; ans15=['and click on the matlab window to activate it. What I chose to do']; ans16=['for the answers is to display the figure for 15 seconds and then']; ans17=['kill it. If you need more time to view the figure, you can simply']; ans17_1=['run it again or you can edit my m-file and change the variable t']; ans17_2=['to whatever you want it to be. ']; disp(ans1),disp(ans2),disp(ans3),disp(ans4),disp(ans5),disp(ans6),disp(ans7),disp(ans7_5) disp(linespace),disp(ans8),disp(ans9),disp(ans10), disp(linespace),disp(ans11),disp(ans12),disp(ans13),disp(ans14),disp(ans15) disp(ans16),disp(ans17),disp(ans17_1),disp(ans17_2) pause clc ans1=['Let''s first load the no3 data, create the D variable, and make the']; ans2=['first plot using the commands given in the assignment load no3.dat,']; ans3=['D=no3, and plot(D(:,2),D(:,1),''+''). ']; disp(ans1),disp(ans2),disp(ans3) pause clc load no3.dat D=no3; figure(1) clf plot(D(:,2),D(:,1),'+'); title('PS02-2 Plot of NO_3 vs. Depth (all data)') xlabel('NO_3 concentration (uM)') ylabel('Depth (M)') pause(t) delete(figure(1)) ans1=['Now multiply the depth data using the command D(:,1)=-1*D(:,1)']; ans2=['and replot it. ']; disp(ans1),disp(ans2) pause clc D(:,1)=-1*D(:,1); plot(D(:,2),D(:,1),'+'); title('PS02-2 Plot of NO_3 vs. Depth (all data)') xlabel('NO_3 concentration (uM)') ylabel('Depth (M)') pause(t) delete(figure(1)) clc ans1=['Now that we know which way is up, the next step is to sub-sample our']; ans2=['data according to depth criteria using the sorting commands']; ans3=['Du=D(D(:,1)<=-2000 & D(:,1)>-3000,:); and Dl=D(D(:,1)<=-3000,:);']; ans4=['']; Du=D(D(:,1)<=-2000 & D(:,1)>-3000,:); Dl=D(D(:,1)<=-3000,:); disp(ans1),disp(ans2),disp(ans3),disp(ans4) pause clc ans1=['Once we have created Du and Dl we have a few ways we can view them.']; ans2=['We can replot the sub-samples over the original using a different']; ans3=['color and the hold on command. ']; disp(ans1),disp(ans2),disp(ans3) pause figure(1) hold on plot(D(:,2),D(:,1),'+'); title('PS02-2 Plot of NO_3 vs. Depth (all data)') xlabel('NO_3 concentration (uM)') ylabel('Depth (M)') plot(Du(:,2),Du(:,1),'r*'); plot(Dl(:,2),Dl(:,1),'go'); pause(t) delete(figure(1)) clc ans1=['Alternately, we could use the subplot command to plot both sets']; ans2=['of data in the same figure. ']; disp(ans1),disp(ans2) pause figure(2) clf %title('PS02-2 Plot of NO_3 vs. Depth (sub-sampled data)') subplot(2,1,1) plot(Du(:,2),Du(:,1),'+'); title('PS02-2 Plot of NO_3 vs. Depth (sub-sampled data)') ylabel('Depth (M)') subplot(2,1,2) plot(Dl(:,2),Dl(:,1),'+'); ylabel('Depth (M)') xlabel('NO_3 concentration (uM)') pause(t) delete(figure(2)) clc ans1=['Now that we have two populations, we can form the null hypothesis']; ans2=['that there is no difference between the populations. By inspecting']; ans3=['the previous figures we see there is a depth trend so we pose the']; ans4=['null hypothesis with the intention of rejecting it. This will reduce']; ans5=['the probability of making a type II error. By choosing a significance']; ans6=['of .05, we are accepting a one in twenty chance of making a type I']; ans7=['That is, rejecting the null hypothesis when it is, in fact, true.']; ans8=['One time in twenty we will conclude that the populations are']; ans9=['different when they are actually the same. ']; disp(ans1),disp(ans2),disp(ans3),disp(ans4),disp(ans5),disp(ans6),disp(ans7),disp(ans8),disp(ans9) pause clc ans1=['We are now ready to run our ttest using the command from the homework']; ans2=['[H,sig,ci]=ttest2(Du(:,2),Dl(:,2),0.05,0). ttest2.m compares the']; ans3=['averages of two samples and performs a t-test to determine whether']; ans4=['two samples from a normal distribution (with unknown but equal']; ans5=['variances) could have the same mean. Let''s do the ttest. ']; disp(ans1),disp(ans2),disp(ans3),disp(ans4),disp(ans5) pause clc [H,sig,ci]=ttest2(Du(:,2),Dl(:,2),0.05,0); ans1=['Let''s examine the output step by step starting with H. ']; disp(ans1) pause clc H ans1=['An H=1 means we can reject the null hypothesis at a significance']; ans2=['level of alpha which is 0.05 in this case. This means we can say']; ans3=['that the populations are in fact different and be 95% sure that we']; ans4=['are correct in our assumption. There is of course a one in twenty']; ans5=['chance that we might be wrong (committing a type I error).']; ans6=['Regardless of what the populations actually were, we chose this level']; ans7=['of confidence a priori so we can expect to do no better. ']; disp(ans1),disp(ans2),disp(ans3),disp(ans4),disp(ans5),disp(ans6),disp(ans7) pause clc sig ans1=['A significance of 7.2595e-05 is another indicator that we may reject']; ans2=['our null hypothesis. What it means is that given two sample']; ans3=['populations from a normally distributed parent population, the odds']; ans4=['are 1 in 725950 that their respective means will differ by the amount']; ans5=['observed in this problem. ']; disp(ans1),disp(ans2),disp(ans3),disp(ans4),disp(ans5) pause clc ci ans1=['Actually, ci does stand for confidence interval. In the case of']; ans2=['ttest2, it is not the interval of the means but actually the interval']; ans3=['of the difference of the means. The values are negative because of']; ans4=['the way we set our problem up (Du is smaller than Dl). Ci is the last']; ans5=['word in rejecting the null hypothesis. What it means is that there is']; ans6=['a 95%% chance that the difference between the means of these two']; ans7=['sample populations will fall between -2.8555 and -1.0163. Turning']; ans8=['this around, we would expect that, if the two sample populations came']; ans9=['from a normally distributed parent population, the interval would']; ans10=['pass through zero (ie. sometimes there would be no difference']; ans11=['between the samples). So, regardless of the sign, if the ci interval']; ans12=['doesn''t pass through zero, it is a good indicator that your samples']; ans13=['are not from the same population.']; disp(ans1),disp(ans2),disp(ans3),disp(ans4),disp(ans5),disp(ans6),disp(ans7) disp(ans8),disp(ans9),disp(ans10),disp(ans11),disp(ans12) disp(ans13) pause clc ans1=['Now let''s determine the mean and standard deviation of each of our']; ans2=['sample populations. ']; disp(ans1),disp(ans2) pause Dmu=mean(Du(:,2)); Dsu=std(Du(:,2)); Dml=mean(Dl(:,2)); Dsl=std(Dl(:,2)); Sample_means_and_standard_deviations=[Dmu Dsu;Dml Dsl] ans1=['In this case, the null hypothesis should be rejected. ']; disp(ans1) pause clc ans1=['End of problem 2']; disp(ans1)