*! version 1.0.0 10mar2000
program define auccalc, rclass
version 6.0

	syntax varlist(numeric min=2 max=2) [if] [in] /*
		*/ [, Trapezoid fit(string) line exp log Graph Connect(string) * ]

	marksample touse
	tokenize `varlist'
	tempvar lnc conc time
	gen double `conc' = `2'
	gen double `time' = `1'
	compress `time' `conc'

	if "`fit'" == "" {
		local fit = 3
	}
	else {
		qui capture confirm integer number `fit'
		if _rc {
			noi di in red "fit() must be an integer"
			exit 198
		}
	}


	qui {
		gen double `lnc' = log(`conc') if `touse'

		if "`trapezoid'" == "" {
			integ `conc' `time' if `touse'
		}
		else {

			integ `conc' `time' if `touse', t
		}
		tempname auc0_tm
		scalar `auc0_tm' = r(integral)
		sort `touse' `time'

		regress `lnc' `time' in -`fit'/l
		tempname b cmax
		mat `b' = e(b)

		sum `conc' if `touse', meanonly
		scalar `cmax' = r(max)
		tempvar tmax auc_ln auc_lg auc_ex
		gen double `tmax' = `time' if `conc' == `cmax' & `touse'

		sort `tmax'
		replace `tmax' = `tmax'[1]

		tempname aucinf

		MYLine `time' `conc' `fit' `touse'
		scalar `auc_ln' = `auc0_tm' + `s(area)'
		local lpt = `s(l_point)'


		MYExp `time' `conc' `fit' `touse'
		scalar `auc_ex' = `auc0_tm' + `s(area)'
		local ept = `s(e_point)'


		sort `touse' `time'
		scalar `auc_lg' = `auc0_tm' + (`conc'[_N] / (-el(`b',1,1)))

	}

	sort `touse' `time'

	di in gr _n
	di in gr _col(5) "        Maximum concentration : " in ye %9.0g `cmax'
	di in gr _col(5) "Time of maximum concentration : " in ye %9.0g `tmax'
	di in gr _col(5) "                         Tmax : " in ye %9.0g `time'[_N]
	di in gr _col(5) "             Elimination rate : " in ye %9.4f -el(`b',1,1)
	di in gr _col(5) "                    Half Life : " in ye %9.4f ln(2) / -el(`b',1,1)
	di in gr _col(5) "  Area under Curve (0 - Tmax) : " in ye %9.2f `auc0_tm'

	di _n in gr _col(5) "                          Area Under the Curve (0 to inf.)"
	di in gr _col(5) "                | Log of Conc. | Simple Linear |  Simple Exp."
	di in gr    _col(5) _dup(16) "-" "+" _dup(14) "-" "+" _dup(15) "-" "+" _dup(15) "-"
	di in gr    _col(5) "AUC (0 to inf.) |  " in ye %9.3f `auc_lg' in gr "   |  " in ye %9.3f `auc_ln' in gr "    |  " in ye %9.3f `auc_ex'
	di in gr	_col(5) _dup(63) "-"
	di in gr	_col(5) "Fit based on last " in ye `fit' in gr " points."

	local tlab: variable label `time'
	if "`tlab'" == "" {
		local tlab = "Analysis Time"
	}
	label var `time' "`tlab'"

	local clab: variable label `conc'
	if "`clab'" == "" {
		local clab = "Concentration"
	}
	label var `conc' "`clab'"

	if "`log'" != "" {
		local lnlab = "Log Concentration"
	}
	label var `lnc' "`lnlab'"

	if "`connect'" != "" {
		local conopt = "connect(`connect')"
	}
	else {
		local conopt = "connect(l)"
	}

	local test = "`log' `exp' `line'"
	local cnt: word count `test'
	if `cnt' > 1 & "`graph'" != "" {
		noi di in red "You can only specify one of the following three options: log, exp, line"
		exit 198
	}

	if "`graph'" != "" & "`log'" == "" & "`line'" == "" & "`exp'" == "" {
		graph `conc' `time', `conopt' `options'
	}

	if "`graph'" != "" & "`log'" != "" {
		graph `lnc' `time', `conopt' `options'
	}

	if "`graph'" != "" & "`line'" != "" {
	qui {
		preserve
		local obs = _N+1
		set obs `obs'
		replace `conc' = 0 if _n == _N
		replace `time' = `lpt' if _n == _N
		graph `conc' `time', `conopt' `options'
		restore
	}
	}


	if "`graph'" != "" & "`exp'" != "" {
	qui {
		preserve
		local obs = _N+1
		set obs `obs'
		replace `conc' = 0 if _n == _N
		replace `time' = `ept' if _n == _N
		graph `conc' `time', `conopt' `options'
		restore
	}
	}



	return scalar cmax = `cmax'
	return scalar tomc = `tmax'
	return scalar tmax = `time'[_N]
	return scalar ke = -el(`b',1,1)
	return scalar half = ln(2) / -el(`b',1,1)
	return scalar auc = `auc0_tm'
	return scalar ln_auc = `auc_lg'
	return scalar exp_auc = `auc_ex'
	return scalar line_auc = `auc_ln'


end


program define MYLine, sclass
	args time conc line touse

	sort `touse' `time'
	regress `conc' `time' in -`line'/l
	tempname b
	mat `b' = e(b)

	local ans = -el(`b',1,2)/el(`b',1,1)
	tempvar time1 conc1

	sum `time' if `touse', meanonly
	gen `time1' = r(max) if `time' == r(max) & `touse'
	gen `conc1' = `conc' if `time' == r(max) & `touse'
	sort `time1'
	replace `time1' = `ans' if _n == _N
	replace `conc1' = 0 if _n == _N

	integ `conc1' `time1'

	sreturn local area = r(integral)
	sreturn local l_point = `ans'

end


program define MYExp, sclass
	args time conc line touse

	sort `touse' `time'
	ereg `conc' `time' in -`line'/l
	tempname b
	mat `b' = e(b)

	local ans = (1+el(`b',1,2)) / -el(`b',1,1)

	tempvar time1 conc1

	sum `time' if `touse', meanonly
	gen `time1' = r(max) if `time' == r(max) & `touse'
	gen `conc1' = `conc' if `time' == r(max) & `touse'
	sort `time1'
	replace `time1' = `ans' if _n == _N
	replace `conc1' = 0 if _n == _N

	integ `conc1' `time1'

	sreturn local area = r(integral)
	sreturn local e_point = `ans'

end