* ME / June 2011
* Department of Biostatistics
* Aarhus University
* Author: Mogens Erlandsen

program define regxcii

version 10

	gettoken yobs 0 : 0, parse(" ,")
	confirm number `yobs'

	syntax [, Level(real 95)]

	scalar y = e(depvar)

	mat bbb=e(b)
	mat vvv=e(V)

	summarize y, meanonly
	scalar ybar=r(mean)

	scalar n=e(N)
	scalar m=1
	scalar df_r=e(df_r)
	scalar rmse=e(rmse)

	scalar b1=bbb[1,1]

	scalar xhat=(`yobs'-bbb[1,2])/b1

	scalar prob=(100-`level')/200

	scalar t=invttail(df_r,prob)
	scalar A=b1*b1-t*t*vvv[1,1]

	scalar ssx=(e(rss)/df_r)/vvv[1,1]
	scalar L=(t*rmse/A)*sqrt(A*(1/n+1/m)+(`yobs'-ybar)*(`yobs'-ybar)/ssx)

	scalar cl1=xhat+L
	scalar cl2=xhat-L

	if cl1<cl2 {
		scalar Llim=cl1
		scalar Ulim=cl2
	}
	else if cl1>cl2 {
		scalar Ulim=cl1
		scalar Llim=cl2
	}

	display _newline "xhat (CI) = " xhat "  (" Llim "; " Ulim ")   [Level = " `level' %5.1f "%]"

end