Thank you Joseph.
After much headache (for the time being) and patient support from Clyde, Joseph, and others, here is the final code that executes:
I still have yet to determine how to actually draw values for the parameters used in the simulation from a distribution, but this is a welcome introduction to advanced coding for a newbie like myself.
For the issue below, I also thought I ran into the same problem initially on a mac. In my case it was a convergence issue due to small sample size. The code eventually did run.
After much headache (for the time being) and patient support from Clyde, Joseph, and others, here is the final code that executes:
Code:
capture program drop swcrt program define swcrt, rclass version 15.1 preserve clear args num_clus clussize intercept timecoeff1 timecoeff2 timecoeff3 timecoeff4 timecoeff5 intrvcoeff sigma_u3 sigma_u2 sigma_error alpha assert `num_clus' > 0 & `clussize' > 0 & `intercept' > 0 & `timecoeff1' < 0 & `timecoeff2' < 0 & `timecoeff3' < 0 & `timecoeff4' < 0 & `timecoeff5' < 0 & `intrvcoeff' > 0 & `sigma_u3' > 0 & `sigma_u2' > 0 & `sigma_error' > 0 /*Generate simulated multi—level data*/ qui set seed 12345 clear set obs `num_clus' qui gen cluster = _n //Generate cluster-level errors // qui gen u_3 = rnormal(0,`sigma_u3') expand `clussize' bysort cluster: gen individual = _n //Generate patient-level errors // qui gen u_2 = rnormal(0,`sigma_u2') expand 6 //Set up time// bysort cluster individual: gen time = _n-1 //Set up intervention variable// gen intrv = (time>=cluster) //Generate residual errors qui gen error = rnormal(0,`sigma_error') //Generate outcome y qui gen y = `intercept' + `timecoeff1'*1.time + `timecoeff2'*2.time + `timecoeff3'*3.time + `timecoeff4'*4.time + `timecoeff5'*5.time + `intrvcoeff'*intrv + u_3 + u_2 + error /*Fit multi-level model to simulated dataset*/ mixed y intrv i.time ||cluster: ||individual:, covariance(exchangeable) reml dfmethod(kroger) /*Return estimated effect size, bias, p-value, and significance dichotomy*/ tempname M matrix `M' = r(table) return scalar bias = _b[intrv] - `intrvcoeff' return scalar p = `M'[1,4] return scalar p_= (`M'[1,4] < `alpha') exit end swcrt *Postfile to store results tempname step tempfile powerresults capture postutil clear //*Closes open postfiles*// postfile `step' num_clus intrvcoeff p p_ bias using `powerresults', replace //*Declare variable names and filename of dataset where results will be saved*// *Loop over number of clusters, ICC foreach c of local num_clus{ display as text "Number of clusters" as result "`c'" /*foreach icc of local ICC{ display as text "Intra-class correlation" as result `i'*/ forvalue i = 1/`nrep'{ display as text "Iterations" as result `nrep' quietly swcrt `c' `clussize' `intercept' `timecoeff1' `timecoeff2' `timecoeff3' `timecoeff4' `timecoeff5' `intrvcoeff' `sigma_u3' `sigma_u2' `sigma_error' `alpha' post `step' (`c') (`intrvcoeff') /*(`icc')*/ (`r(p)') (`r(p_)') (`r(bias)') //*Adds new observation to declared dataset; # of variables here must match the postfile command*// } /*}*/ } postclose `step' //*Declares end to posting of observations*// *Open results, calculate power use `powerresults', clear //*Loads new dataset with posted results*// levelsof num_clus, local(num_clus) /*levelsof ICC, local(ICC)*/ /*Maybe remove or add ICC*/ matrix drop _all *Loop over combinations of clusters and ICC *Add power results to matrix foreach c of local num_clus { /*foreach i of local ICC { */ quietly ci proportions p_ if num_clus == `c' /*& float(ICC) == float(`i')*/ local power `r(proportion)' local power_lb `r(lb)' local power_ub `r(ub)' quietly ci mean bias if num_clus == `c' /*& float(ICC) == float(`i')*/ local bias `r(mean)' local bias_lb `r(lb)' local bias_ub `r(ub)' matrix M = nullmat(M) \ (`c', `intrvcoeff', `power', `power_lb', `power_ub', `bias', `bias_lb', `bias_ub') //* \ is a column join operator to be used with nullmat()*// /*}*/ } *Display the matrix matrix colnames M = c intrvcoeff power power_lb power_ub bias bias_lb bias_ub //*Arguments should match those in the previous line to avoid conformability errors*// matrix list M, noheader format(%3.2f)
For the issue below, I also thought I ran into the same problem initially on a mac. In my case it was a convergence issue due to small sample size. The code eventually did run.
Originally posted by Filipe Rodrigues
View Post
Comment