program define gllapred
	version 6.0

	if "`e(cmd)'" ~= "gllamm6" {
	di in red  "gllamm6 was not the last command"
		exit 301
	}

	syntax newvarlist(min=1 max=1) [,XB U P]

	global HG_tplv = e(tplv)

	local nopt = ("`u'"!="") + ("`p'"!="") + ("`xb'"!="")
	if `nopt'>1 {
		disp in re "only one option allowed"
		exit 198
	}
	if `nopt'==0 {
			local xb "xb"
	}
	if "`xb'"~=""{
		predict `varlist'
	}
	else{
		if "`p'"~="" {
			local probs = 1
		}
		else{
			local probs = 0
		}
		if $HG_tplv~=2{
			disp in re "can't do residuals at for " $HG_tplv " level model"
			exit 301
		}
/* sort out clus */
		tempvar id
		local clus `e(clus)'
		gen `id'=_n
		parse "`clus'", parse(" ")
		local l= $HG_tplv
		local `l' "`id'"
		global HG_clus "`*'"

/* sort out depvar */
		local depv "`e(depvar)'"
		global ML_y1 "`depv'"
		local denom "`e(denom)'"
		if "`denom'"~=""{
			capture confirm variable `denom'
			if _rc>0{
				tempvar den
				qui gen `den'=1
				global HG_denom "`den'"
			}
		}
/* sort out weight */
		local weight "`e(weight)'"
		capture confirm variable `weight'1
		if _rc>0{
			* disp "no weight 1 var"
			tempvar wt1
			qui gen `wt1'=1
			global HG_wt1 "`wt1'"
			* disp "HG_wt1: `wt1'"
		}
		else {
			global HG_wt1 "`weight'1"
		}
		capture confirm variable `weight'2
		if _rc>0{
			tempvar wt2
			qui gen `wt2'=1
			global HG_wt2 "`wt2'"
		}
		else {
			* disp "found wt2 variable"
			global HG_wt2 "`weight'2"
		}
/* set all other global macros */
		global HG_tpff = `e(tpff)'
		global HG_tpi = `e(tpi)'
		global HG_free = e(free)
		global which = 4
		global HG_famil "`e(famil)'"
		global HG_link "`e(link)'"
		global HG_olog = `e(olog)'
		global HG_lv "`e(lv)'"
		global HG_fv "`e(fv)'"
		global HG_off "`e(offset)'"
		global HG_error = 0
		global HG_cor = `e(cor)'
		global HG_bmat = e(bmat)
		global HG_ind = e(ind)
		global HG_tprf = e(tprf)
		global HG_const = e(const)
		global HG_ngeqs = e(ngeqs)
		global HG_inter = e(inter)
		global HG_dots = 0
		matrix M_nbrf = e(nbrf)
		matrix M_nrfc = e(nrfc)
		matrix M_ip =  J(1,$HG_tprf+2,1)
		matrix M_nffc =  e(nffc)
		matrix M_znow =J(1,$HG_tprf-1,1)
		matrix M_nip = e(nip)
		capture matrix M_b=e(mb)
		*matrix M_chol = e(chol)
		local l = M_nrfc[1,1] + 1  /* loop */
		local k = M_nrfc[2,1] + 1  /* r. eff. */
		while `l'<=M_nrfc[1,2]{
			while `k'<=M_nrfc[2,2]{
				* disp "loop " `l' " random effect " `k'
				local w = M_nip[2,`k']

				/* same loc and prob as before? */
				local found = 0
				local ii=M_nrfc[2,1] + 1
				while `ii'<`k'{
					if `w'==M_nip[2,`ii']{
						local found = 1
					}
					local ii = `ii'+1
				}


				capture matrix M_zps`w' =e(zps`w')
				if `probs'{
					if $HG_free {
						if `k' == M_nrfc[1,`l']{
							disp in gr "prior probabilities"
							matrix list M_zps`w'
							disp " "
						}
					}
					else if `found'==0{
						disp in gr "probabilities for `w' quad. points"
						matrix list M_zps`w'
						disp " "
					}
				}
				* disp "M_zlc`w'"
				matrix M_zlc`w'=e(zlc`w')
				if `probs'{
					if $HG_free{
						disp in gr "locations for random effect " `w'-1
						matrix list M_zlc`w'
						disp " "
					}
					else if `found'==0{
						disp in gr "locations for `w' quadrature points"
						matrix list M_zlc`w'
						disp " "
					}

				}
				local k = `k' + 1
			}
			local l = `l' + 1
		}
/* call gllam_ll */
		tempname b lnf
		matrix `b'=e(b)
		noi gllam_ll 1 `b' "`lnf'" 2 `probs' `varlist'
		disp "log-likelihood is " `lnf'

/* delete macros */
		delmacs
	}
end

program define delmacs
	version 6.0
/* deletes all global macros and matrices*/
	tempname var
	if "$HG_tplv"==""{
		* macros already gone
		exit
	}
	local nrfold = M_nrfc[2,1]
	local lev = 2
	while (`lev'<=$HG_tplv){
		local i2 = M_nrfc[2,`lev']
		local i1 = `nrfold'+1
		local i = `i1'
		local nrfold = M_nrfc[2,`lev']
		while `i' <= `i2'{
			local n = M_nip[2,`i']
			if `i' <= M_nrfc[1,`lev']{
				capture matrix drop M_zps`n'
			}
			capture matrix drop M_zlc`n'
			local i = `i' + 1
		}
		local lev = `lev' + 1
	}
	if $HG_free==0{
		matrix drop M_chol
	}
	matrix drop M_nrfc
	matrix drop M_nffc
	matrix drop M_nbrf
	matrix drop M_ip
	capture matrix drop M_b
	matrix drop M_nip
	matrix drop M_znow
	capture matrix drop CHmat

	/* globals defined in gllam_ll */
	local i=1
	while (`i'<=$HG_tpff){
		global HG_xb`i'
		local i= `i'+1
	}
	local i = 1
	while (`i'<=$HG_tprf){
		global HG_s`i'
		local i= `i'+1
	}
	local i = 1
	while (`i'<=$HG_tplv){
		global HG_wt`i'
		local i = `i' + 1
	}
	global HG_fixe
	global HG_lev1
	global HG_bmat
	global HG_tplv
	global HG_tprf
	global HG_tpi
	global HG_tpff
	global HG_clus
	global HG_weigh
	global which
	global HG_gauss
	global HG_free
	global HG_famil
	global HG_link
	global HG_olog
	global HG_exp
	global HG_expf
	global HG_lv
	global HG_fv
	global HG_nump
	global HG_eqs
	global HG_obs
	global HG_off
	global HG_denom
	global HG_cor
	global HG_s1
	global HG_init
	global HG_ind
	global HG_const
	global HG_dots
	global HG_inter
	global HG_ngeqs
end