function capillary_glucose_analyses(left,right)
%used to analyze the fingerstick glucose values from 3 groups: the one individual with longitudinal self check on two hands daily, the one-time simultaneous measurement of both hands, and the one-time simultaneous double measure on one hand

close all

%A:  single_subject_glucose_over_weeks -- left and right are the daily values from the respective hands over time
figure, hold on

lgreater=repmat(nan,1,length(left));
rgreater=lgreater;
eq=lgreater;

for day=1:length(left)
    cv=std([left(day) right(day)])/mean([left(day) right(day)]);
    if left(day)>right(day)
        lgreater(day)=cv;
    elseif left(day)<right(day)
        rgreater(day)=cv;
    else
        eq(day)=cv;
    end
end

plot(lgreater,'o','markeredgecolor','k','markerfacecolor','w','markersize',8)
plot(rgreater,'s','markeredgecolor','k','markerfacecolor','w','markersize',8)
plot(eq,'^','markeredgecolor','k','markerfacecolor','w','markersize',8)

xlabel('day')
ylabel('CV')
%legend('Left greater','Right greater','Equal')
%legend boxoff
ylim([-.005 .205])
set(gca,'ytick',[0 0.05 0.1 0.15 0.2])

%from here'left' is a two-column variable holding the glucose measurements for one group (say, the between-hand group), with one subject per row;  'right' is analogous, for the other group 

%B: measure alpha v measure beta for both within-hand and across-hands data sets
figure, hold on
plot([50 400],[50 400],'k:')
plot(left(:,1),left(:,2),'o','markeredgecolor','k','markerfacecolor','w','markersize',8)
plot(right(:,1),right(:,2),'s','markeredgecolor','k','markerfacecolor','w','markersize',8)
xlabel('Measure A')
ylabel('Measure B')
axis([50 400 50 400])

%C: cv of the two readings from each subject (except the longitudinal subject) as cumulative histograms
figure, hold on
stdl=std(left')';cvl=stdl./mean(left,2);
stdr=std(right')';cvr=stdr./mean(right,2);
cumulative_histogram(cvl,'k:')
cumulative_histogram(cvr,'k--')
[h,p]=kstest2(cvl,cvr)

%D: text results, no figure:  mean and std and SEM of the CV for the two groups
meancvl=mean(cvl),stdcvl=std(cvl),seml=stdcvl/sqrt(length(cvl))
meancvr=mean(cvr),stdcvr=std(cvr),semr=stdcvr/sqrt(length(cvr))

%E: trying to show the CV is not larger for larger mean glucose, so it's not just b/c the two hand set has some higher means that we see the higher CV
figure,hold on
plot(mean(left,2),cvl,'o','markeredgecolor','k','markerfacecolor','w','markersize',8) %-- why the twohands set has an inverse relation between mean and CV is unclear
rone=corrcoef(mean(left,2),cvl)
plot(mean(right,2),cvr,'s','markeredgecolor','k','markerfacecolor','w','markersize',8)
rtwo=corrcoef(mean(right,2),cvr)
xlabel('mean')
ylabel('CV')



