clear all cd "C:\Users\fachana\Documents\MDM\simdat1" // changed to directory dir on your computer *** loc meth = 1 // method 1 gsem robust vce loc nsim = 2000 loc mc = 2000 loc wtp = 20000 qui mat ncl = (5,10,20,40,60,80,100) // number of clusters forval m = 1/`=colsof(ncl)' { timer on `m' loc ncluster = ncl[1,`m'] tempname simout`meth' postfile `simout`meth'' sim meth conv rc niter ncluster /// costd secostd lcostd ucostd /// qalyd seqalyd lqalyd uqalyd /// nb20k senb20k lnb20k unb20k /// using simresults`meth'.dta, replace forval i=1/`nsim' { qui insheet using simdata`ncluster'.csv, clear qui keep if simdata==`i' *** Fit random intercepts using gsem capture gsem (cost <- trt M1[cvar], family(gamma) link(log)) /// (qaly <- trt M2[cvar], family(gamma) link(log)), /// cov(M1[cvar] M2[cvar]) iterate(500) intmethod(mcaghermite) vce(robust) dif nolog *** Generate required parameter estimates if model does not crash if _rc == 0 { loc conv = e(converged) loc niter = e(ic) loc rc = _rc loc varblncost = _b[/var(M1[cvar])] loc varblnqaly = _b[/var(M2[cvar])] loc covb = _b[/cov(M1[cvar],M2[cvar])] loc costd = exp(_b[cost:trt] + _b[cost:_cons]) - exp(_b[cost:_cons]) loc qalyd = exp(_b[qaly:_cons]) - exp(_b[qaly:trt] + _b[qaly:_cons]) loc nb20k = `wtp' * `qalyd' - `costd' *** Monte Carlo sim to generate CI and SEs on cost and QALY scale clear qui set obs `mc' qui tempvar c0 c1 cdif q0 q1 qdif lnc lnq m1 m2 m12 cd qd nb qui drawnorm `cdif' `c1' `c0' `qdif' `q1' `q0' `lnc' `lnq' `m1' `m2' `m12', n(`mc') means(e(b)) cov(e(V)) qui gen `cd' = exp(`cdif' + `c0') - exp(`c0') qui gen `qd' = exp(`q0') - exp(`qdif' + `q0') qui gen `nb' = `wtp' * `qd' - `cd' *** CIs & SEs for inc. costs qui sum `cd' loc secostd = r(sd) loc lcostd = `costd' - 1.96 * r(sd) loc ucostd = `costd' + 1.96 * r(sd) *** CIs & SEs for inc. qalys qui sum `qd' //inc. qalys loc seqalyd = r(sd) loc lqalyd = `qalyd' - 1.96 * r(sd) loc uqalyd = `qalyd' + 1.96 * r(sd) *** CIs & SEs for NB at £20K/QALY threshold qui sum `nb' loc senb20k = r(sd) loc lnb20k = `nb20k' - 1.96 * `senb20k' loc unb20k = `nb20k' + 1.96 * `senb20k' *** save simulated results to data file post `simout`meth'' (`i') (`meth') (`conv') (`rc') (`niter') (`ncluster') /// (`costd') (`secostd') (`lcostd') (`ucostd') /// (`qalyd') (`seqalyd') (`lqalyd') (`uqalyd') /// (`nb20k') ( `senb20k') (`lnb20k') (`unb20k') } } postclose `simout`meth'' use simresults`meth', clear qui outsheet using gsemoutput`ncluster'.csv, comma replace timer off `m' timer list `m' }