
***********************************************************************************************************************
**** Program blandaltman (one parameter against another parameter) startet ********************************************	
***********************************************************************************************************************	
	
*	set more off
*	capture program drop blandaltman
	program blandaltman
		syntax varlist(max=2)

		// prepare for Bland Altman Interreader
			tempvar diff_xy
			tempvar avg_xy
			tempvar lower
			tempvar higher
			tempvar MW
			tempvar SE
			tempvar CIhigher
			tempvar CIlower
			
			generate `diff_xy'=0
			generate `avg_xy'=0
			generate `lower'=0
			generate `higher'=0
			generate `MW'=0
			generate `SE'=0
			generate `CIhigher'=0
			generate `CIlower'=0
			
		// count the variable: how many variable are in the list?
			local noofvars : word count `varlist'
			display as text "The variable list of this program counts " `noofvars' " variables"
			display as result " "
			display as result " "
			
		// Interreader
			local x = 1
			local y = 1
			foreach varx of varlist `varlist' { 
				foreach vary of varlist `varlist'{
					if `y' >`x'{
					quietly replace `avg_xy'=(`varx'+`vary')/2
					quietly replace `diff_xy'=`varx'-`vary'
					display as result " Bland Altman Plot of `varx' and `vary'"
					quietly sum `diff_xy'
					quietly return list
					quietly replace `MW'=r(mean)
					quietly replace `lower'=r(mean)-2*r(sd)
					quietly replace `higher'=r(mean)+2*r(sd)
					quietly replace `SE'=(r(sd))/(sqrt(r(N)))
					quietly replace `CIlower'=r(mean)-2*`SE'
					quietly replace `CIhigher'=r(mean)+2*`SE'
					display as result "- mean of difference between `varx' and `vary' is "r(mean)
					display as result "- sd of difference between `varx' and `vary' is "r(sd)
					display as result "- lower limit of difference between `varx' and `vary' is " `lower'
					display as result "- higher limit of difference between `varx' and `vary' is " `higher'
					display as result "- Limits of agreement (Reference Range for difference): " `lower' " to " `higher'
					display as result "- Mean difference:" `MW' " (CI " `CIlower' " to " `CIhigher' ")"
					display as result " "
					display as result " "
					
					label var `diff_xy' "Values"
					label var `MW' "mean of difference"
					label var `lower' "lower limit of agreement"
					label var `higher' "higher limit of agreement"
					twoway (scatter `diff_xy' `avg_xy', msymbol(smcircle_hollow) mcolor(ebblue)) (line `MW' `avg_xy', lcolor(red))(line `lower' `avg_xy', lcolor(black) ) (line `higher' `avg_xy', lcolor(black) ),  title(Bland Altman Plot, size(8)) subtitle(,size(5)) xtitle(Average of `varx' and `vary') ytitle(Difference of `varx' and `vary') caption() note(NOTE)  legend(off) 
					}
				local y = `y'+1
				}
			local y = 1
			local x =`x'+1	
			}
	end
 
***********************************************************************************************************************
**** Program blandaltman (one parameter against another parameter) endet **********************************************	
***********************************************************************************************************************	
		
		
		
		
//	EXAMPLE 
		
*	sysuse bpwide.dta
	
	// to create a bland altman plot use the command "blandaltman" with variable1  and variable2
*	blandaltman bp_before bp_after
	
	