Dear all,
I'm having a problem with simulate command. I have written an r-class program. When I run the program and use "return list", I get the estimates I need. However, when I use simulate to store those estimates, simulate returns an error and it does not store those values. I have tried using other estimates and it works fine but when trying to extract pval and ub it simply does not work. To try to solve the problem, I tried to extract the result, store it in a local, then generate a variable with that value and then use the r(mean) value of that variable - however this does not work. This is really odd and spent hours trying to figure out why it does not work without success.
You can find below the code. Any help will be very much appreciated.
I'm having a problem with simulate command. I have written an r-class program. When I run the program and use "return list", I get the estimates I need. However, when I use simulate to store those estimates, simulate returns an error and it does not store those values. I have tried using other estimates and it works fine but when trying to extract pval and ub it simply does not work. To try to solve the problem, I tried to extract the result, store it in a local, then generate a variable with that value and then use the r(mean) value of that variable - however this does not work. This is really odd and spent hours trying to figure out why it does not work without success.
You can find below the code. Any help will be very much appreciated.
Code:
ssc install survsim
cap prog drop sim_model
prog define sim_model, rclass
clear
set obs 300
gen trt = rbinomial(1,0.5)
survsim time fail, lambdas(0.07) distribution(exponential) covariates(trt -.28768207) maxtime(48)
stset time, fail(fail)
gen cured = rbinomial(1,0.20)
replace time=48 if cured==1
replace fail=0 if cured==1
drop cured
stset time, fail(fail)
fmm: (pointmass fail) (streg trt, distribution(exponential))
mat a=r(table_m2)
local pval=el(a,4,1)
gen pval=`pval'
sum pval
return scalar pval=r(mean)
local pval=r(mean)
local ubln=el(a,6,1)
local ub=exp(`ubln')
gen ub=`ub'
sum ub
return scalar ub=r(mean)
local ub=r(mean)
********************
gen reject_ub=.
replace reject_ub=1 if `ub'<1
replace reject_ub=0 if reject_ub==.
sum reject_ub
return scalar reject_ub=r(mean)
gen reject_pvalue=.
replace reject_pvalue=1 if `pval'<0.05
replace reject_pvalue=0 if reject_pvalue==.
sum reject_pvalue
return scalar reject_pvalue=r(mean)
end
sim_model
return list
simulate ub=r(ub) pval=r(pval) reject_pvalue=r(reject_pvalue) reject_ub=r(reject_ub), reps(5): sim_model

Comment