I would like to compute a summary statistic of a set of groups within my data (such as age,sex) with confidence intervals. For that purpose I use monte carlo simulation drawing values from a Poisson distribution for every row in my data and then collapsing the rows to have the summary. The whole procedure works fine if the result of the simulation is just one value (using return scalar in rclass) but as soon as I try to simulate more than one result (using ereturn matrix in eclass) it is not working (see Stata code below). I get the error message: "type mismatch error in expression: e(A)". How could I simulate a whole vector or even matrix of results without more complex loops etc.?
program bootPGW, eclass
gen id=_n
sort id
gen N=_N
by id: gen DL2=floor(rpoisson(calung))
by id:gen D02=floor(rpoisson(D0))
by id:gen Dsmoking=floor(rpoisson(smoking))
by id:gen ML2=(DL2/numpyr)*1000
by id:gen AL2=(ML2-CPSIIrate)/ML2
by id:replace AL2=0 if AL2<0
by id:gen A02=1-exp(-PWGcoef*(ML2-CPSIIrate))
by id:gen A2=(AL2*DL2+A02*D02)/(DL2+D02)
gen Adeaths=totdeath*A2
collapse (sum) Adeaths=Adeaths totdeath=totdeath Dsmoking=Dsmoking, by(edu_3cat sex country year)
gen AF_PWG=Adeaths/totdeath
gen AF_simple=Dsmoking/totdeath
mkmat AF_PWG, matrix(A)
ereturn matrix A=A
end
simulate a=e(A), reps(1000) nodots seed(123): bootPGW
program bootPGW, eclass
gen id=_n
sort id
gen N=_N
by id: gen DL2=floor(rpoisson(calung))
by id:gen D02=floor(rpoisson(D0))
by id:gen Dsmoking=floor(rpoisson(smoking))
by id:gen ML2=(DL2/numpyr)*1000
by id:gen AL2=(ML2-CPSIIrate)/ML2
by id:replace AL2=0 if AL2<0
by id:gen A02=1-exp(-PWGcoef*(ML2-CPSIIrate))
by id:gen A2=(AL2*DL2+A02*D02)/(DL2+D02)
gen Adeaths=totdeath*A2
collapse (sum) Adeaths=Adeaths totdeath=totdeath Dsmoking=Dsmoking, by(edu_3cat sex country year)
gen AF_PWG=Adeaths/totdeath
gen AF_simple=Dsmoking/totdeath
mkmat AF_PWG, matrix(A)
ereturn matrix A=A
end
simulate a=e(A), reps(1000) nodots seed(123): bootPGW
Comment