Hi All,
I have a problem when a mata block is called within a -capture- block, which is itself nested within three Stata -for- loops.
The following simple code is to:
- iterate over various time series variables
- for each variable, estimate ARMA models for different lags
- store AIC and BIC for each model in a matrix
- show to the user the models that minimise the IC
The code works well, but I'm having issues in handling the loop when the -arima- estimation does not converge.
To handle such a scenario, I'm relying on the block:
However, -capture- does not behave as I'd expect only when it contains a mata block.
In particular:
1 - if the -arima- estimation converges, no problem.
2 - if the -arima- estimation ends with a convergence error, my expectation is that the code that follows -arima- and is within the =capture- block is not executed. However, I'm finding two issues:
2a) the Stata code within the -capture- block that follows the mata block is executed anyway
2b) the local macro `L_auth' becomes blank
Below is the output under scenario 2. Note that:
- the message "Debug B)..." is displayed, even if it shouldn't
- the message "Debug D) ..." does not show the name of the local authority, even if it should.
(- the last error 3499 is a consequence of the previous one: the macro `L_auth' became unexpectedly blank so it cannot find the matrix)
And below is the output still after an -arima- convergence error, but I've now simply deleted the mata block contained within the ARMA loops.
Note that the output is now perfectly sensible to me:
- the message "Debug B)..." is not displayed. Correct.
- the message "Debug D) ..." shows the name of the local authority. Correct.
(- the error <istmt> at the very end is expected as I've deleted the aforementioned mata block, so no problem)
In a nutshell, it is as if the mata block causes issues within the loops / blocks.
Any suggestion would be greately appreciated.
Thank you.
I have a problem when a mata block is called within a -capture- block, which is itself nested within three Stata -for- loops.
The following simple code is to:
- iterate over various time series variables
- for each variable, estimate ARMA models for different lags
- store AIC and BIC for each model in a matrix
- show to the user the models that minimise the IC
Code:
mata: mata clear local ar_max = 3 local ma_max = 3 // list of time series variables for analysis: local varlist = "lond_r_rea east_r_rea seas_r_rea swes_r_rea" foreach L_auth of local varlist { // Initialise empty matrix to store AIC, BIC of ARMA models mata { cnt = 0 IC_`L_auth' = J((`ar_max')*(`ma_max'), 4, .) } // Loops over the ARMA models: forvalues i = 1(1)`ar_max' { forvalues j = 1(1)`ma_max' { // -capture- handles convergence issues in -arima- estimation capture noisily { display "Debug 0) This message shows the Local Authority: `L_auth'" arima `L_auth', ar(1/`i') ma(1/`j') estimates stats display "Debug A) This message should NOT be displayed if -arima- didn't converge: `L_auth'" // Saving AIC BIC (with ARMA order) in a single matrix, for each local authority: mata { cnt = cnt + 1 IC_`L_auth'[cnt,] = (`i',`j',st_matrix("r(S)")[1,5..6]) IC_`L_auth' } display "Debug B) This message should NOT be displayed if -arima- didn't converge: `L_auth'" } display "Debug C) This message should display the Local Authority: `L_auth'" } } display "Debug D) This message should display the Local Authority: `L_auth'" // (the following part of the code is not problematic) mata { // Retrieve row where AIC = min and return the corresponding ARMA(p,q) order to Stata AIC_row = select((1::rows(IC_`L_auth')), (IC_`L_auth'[,3] :== colmin(IC_`L_auth'[,3]))) st_numscalar("`L_auth'_AIC_ar", IC_`L_auth'[AIC_row,1]) st_numscalar("`L_auth'_AIC_ma", IC_`L_auth'[AIC_row,2]) // Retrieve row where BIC = min and return the corresponding ARMA(p,q) order to Stata BIC_row = select((1::rows(IC_`L_auth')), (IC_`L_auth'[,4] :== colmin(IC_`L_auth'[,4]))) st_numscalar("`L_auth'_BIC_ar", IC_`L_auth'[BIC_row,1]) st_numscalar("`L_auth'_BIC_ma", IC_`L_auth'[BIC_row,2]) } } scalar list
To handle such a scenario, I'm relying on the block:
Code:
capture noisily { ... mata { ... } }
In particular:
1 - if the -arima- estimation converges, no problem.
2 - if the -arima- estimation ends with a convergence error, my expectation is that the code that follows -arima- and is within the =capture- block is not executed. However, I'm finding two issues:
2a) the Stata code within the -capture- block that follows the mata block is executed anyway
2b) the local macro `L_auth' becomes blank
Below is the output under scenario 2. Note that:
- the message "Debug B)..." is displayed, even if it shouldn't
- the message "Debug D) ..." does not show the name of the local authority, even if it should.
(- the last error 3499 is a consequence of the previous one: the macro `L_auth' became unexpectedly blank so it cannot find the matrix)
Code:
... Iteration 247: log likelihood = 860.05825 (backed up) flat log likelihood encountered, cannot find uphill direction Debug B) This message should NOT be displayed if -arima- didn't converge: lond_r_rea Debug C) This message should display the Local Authority: lond_r_rea Debug D) This message should display the Local Authority: <istmt>: 3499 IC_ not found r(3499); end of do-file r(3499); .
Note that the output is now perfectly sensible to me:
- the message "Debug B)..." is not displayed. Correct.
- the message "Debug D) ..." shows the name of the local authority. Correct.
(- the error <istmt> at the very end is expected as I've deleted the aforementioned mata block, so no problem)
Code:
Iteration 247: log likelihood = 860.05825 (backed up) flat log likelihood encountered, cannot find uphill direction Debug C) This message should display the Local Authority: lond_r_rea Debug D) This message should display the Local Authority: lond_r_rea st_numscalar(): 3204 matrix found where scalar required <istmt>: - function returned error r(3204); end of do-file r(3204); .
Any suggestion would be greately appreciated.
Thank you.
Comment