Reputation: 611
I programmed the Monte Carlo simulation down below. The simulation itself works, but I do not seem able to summarize
it. When I run my full program, the do file automatically ends after the Monte Carlo simulation and does not run the last three lines of my code. When I run it manually after the do file has stopped I get the error: 'invalid file specification'. Does anyone has experience with this?
foreach n in 30 1600 {
local j = 1
while `j' <= 1000 {
quietly {
preserve
sample `n', count
noisily di in yellow "." _continue
gen y3_m = 1 + 1*w + v3
replace y3_m = y3_m > 0
gen y2_m = 1 + 1*x + 1*z1 +1*z2 + 1*v1 + 1*v2
replace y2_m =. if y3_m <= 0
gen y1_m = 1 + 1*y2_m + 1*x + v1
replace y1_m =. if y3_m <= 0
probit y3_m w
*step 2
predict eventpr_m, xb
gen PDFprobit_m = normalden(eventpr)
gen CDFprobit_m = normprob(eventpr)
gen IMR_m = PDFprobit/CDFprobit
ivregress 2sls y1_m x IMR_m (y2 = z1 z2)
matrix b1=e(b)
svmat b1, name(Ivreg) // save the regression coefficients and collapse in
// order to only have one observation per coefficient
collapse (mean) Ivreg*
gen N = `n'
if `j' > 1 {
append using `mc`n''
}
save `mc`n'', replace
restore
local j = `j' + 1
}
}
di "`n'"
}
use `mc30', clear
append using `mc1600'
summarize
Upvotes: 0
Views: 472
Reputation: 37208
On any forum this has to be regarded as an awkward question: the dataset, or an equivalent, is nowhere explained or accessible and the code depends on local macro assignments made earlier; it appears that most of the code is tangential to the question. For future reference, please read about minimal, complete, and verifiable examples.
I couldn't read the whole of the code easily without scrolling, so I copied it and trimmed it down to understand as much as possible. Here is an abbreviated version, making cosmetic changes, including but not only
forval
rather than a while
loop. I have not tested this version for compatibility.
foreach n in 30 1600 {
quietly forval j = 1/1000 {
preserve
sample `n', count
noisily di in yellow "." _continue
gen y3_m = (1 + w + v3) > 0
gen y2_m = 1 + x + z1 + z2 + v1 + v2
replace y2_m =. if y3_m <= 0
gen y1_m = 1 + y2_m + x + v1
replace y1_m =. if y3_m <= 0
probit y3_m w
predict eventpr_m, xb
gen IMR_m = normalden(eventpr)/normprob(eventpr)
ivregress 2sls y1_m x IMR_m (y2 = z1 z2)
matrix b1=e(b)
svmat b1, name(Ivreg)
collapse (mean) Ivreg*
gen N = `n'
if `j' > 1 append using `mc`n''
save `mc`n'', replace
restore
} // forval
di "`n'"
} // foreach
use `mc30', clear
append using `mc1600'
summarize
The crunch appears to be that the code here was originally included in a do-file. The content implies that it was just the tail end of a do-file as otherwise the references within to local macros mc30
and mc1600
would seem invalid.
The problem with running the last three lines separately is immediate. Outside the context of the do-file, the local macro mc30
is not defined; local macros are just as the term implies, local to the namespace of the program in which they are defined. So Stata sees just use, clear
, which is illegal. A similar problem would bite with mc1600
, but Stata doesn't get that far.
I can't say why the code as a self-contained do-file doesn't finish, but as said there is no reproducible example here to explore. You need to insert more checks and/or slim the code down to identify the problem.
Upvotes: 4