0

When using the post command, I get the following error:

post command requires expressions be bound in parenthesis

My program generates a matrix which stores regression coefficients for each simulation, and then uses the post command to declare as float and place the output of the matrix in parenthesis (betas).

A sample of the code:

*Priors
set more off
global nmc=10
global l = 4  /* number of lags */ 
global cnt=150 /* number of countries */ 
set seed 10101

* Gen empty beta matrix 
matrix betas = J(153,$nmc+1,.) 

*** THIS IS WHERE MONTECARLO STARTS*** 
program bootStrapCH5, rclass 
tempname sim
postfile `sim' betas using results, replace  /* As trial I'll create only the betas matrix for now.  */ 
*postfile `sim' betas alpha_mean b1_mean b2_mean b3_mean b4_mean se_alpha se1 se2 se3 se4 using results, replace 

quietly {
forvalues i = 1/$nmc {

    * Fixed effects regression. 
    reg gdp_growth_wb L(1/4).gdp_growth_wb i.id
    matrix B1= e(b)
    mat li B1
    predict g_hat,xb
    gen e_hat= gdp_growth_wb - g_hat
    *gen flag=e(sample)


    * Generate the "wild" errors for the forecasts 
    gen eta=rnormal()
    gen e_star=e_hat*eta

        **RECURSION
    levelsof id, local(codes)

    capture noisily replace y_star= _b[_cons] + _b[L.gdp_growth_wb]*L.y_star + ///
        _b[L2.gdp_growth_wb]*L2.y_star + _b[L3.gdp_growth_wb]*L3.y_star + ///
        _b[L4.gdp_growth_wb]*L4.y_star + e_star if (id==1 & Dini4forward==1)

    forvalues cc= 2(1)150 {
        capture noisily replace y_star= _b[_cons] + _b[`cc'.id] + _b[L.gdp_growth_wb]*L.y_star + ///
        _b[L2.gdp_growth_wb]*L2.y_star + _b[L3.gdp_growth_wb]*L3.y_star + ///
        _b[L4.gdp_growth_wb]*L4.y_star + e_star if (id==`cc' & Dini4forward==1)

            }

    *Regression with new sample: y_star
    reg y_star L(1/4).y_star i.id
    matrix b= e(b)' 
    matrix betas= (betas , b)
    matrix list betas

    post `sim' float (betas)

    }
    }
    postclose `sim'
    end

    *Execute program 
    bootStrapCH5
    use results, clear
    summarize 

I also tried an alternative:

post `sim'  (betas)

And got the error:

> type mismatch
post:  above message corresponds to expression 1, variable betas

Any ideas on how to fix this are very much appreciated.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
k1000x
  • 65
  • 10
  • If you are storing 1Xm matrix of regression coefficients for each simulation run, you may be better off initializing a rXm storage matrix (where r is the number of replications) above the simulations and filling a row of this matrix in after each replication. Then use a single `svmat` command at the end to put your results in the main stata dataset, or use `putexcel` depending on what you want to do with the results. – lmo May 11 '16 at 13:11
  • Thank you. I will use svat command. mucha appreciated. – k1000x May 12 '16 at 14:19

1 Answers1

1

I'm not very familiar with postfile, but I think one issue could be that you are trying to insert a kx2 matrix into a single variable inside of your loop with post.

When you initiate postfile using:

postfile `sim' betas using results

you have declared a Stata dataset with a single variable, betas.

So, instead of using

post `sim' float (betas)

you might try:

tempname sim
postfile `sim' float (betas1 betas2) using results, replace
forvalues i = 1/$nmc {

    * Some code. . . 
    local rows = rowsof(betas)
    forvalues i = 1/`r' {
        post `sim' (betas[`i',1]) (betas[`i',2])
    }
    * some other code. . .
}

or something similar to declare a file with the proper number of variables which you intend on posting to the dataset.

Further, I'm not sure that you can post a matrix directly anyway (I could be wrong about this). If you can't, then you could nest a forvalues loop inside of the loop you currently have to iterate through the elements of betas and post them individually - as I have done in the example above.

Finally, you are trying to cast the values of betas as data type float in your post command. I believe the storage types need to be declared in the postfile command (but again, I could be wrong about this). The first error you cite (expressions bound in parenthesis) is a direct result of including float in the post command.

Bottom line - I suspect the first error is due to declaring the data type when you try to post the data, and the second error (type mismatch) is a result of trying to insert an kx2 matrix into a variable. See below for an example of type mismatch when trying to (incorrectly) create data from a matrix:

clear *
mat a = (1\2)
set obs 2
gen x = a

Although I admittedly would have expected the error to be more analogous to this:

mat a = (1\2)
set obs 2
gen x = a*2

matrix operators that return matrices not allowed in this context

Also look at svmat for creating data from matrices.

ander2ed
  • 1,318
  • 1
  • 11
  • 19
  • 1
    The key idea is indeed that each call to `post` adds a single observation to the dataset being produced. You can post values of several variables, but for each variable you can only add a single value. There is no sense in which you can post an entire matrix at once, as the original code was trying to do. Also, declarations are quite alien to `post`, so the `float` call is illegal. – Nick Cox May 11 '16 at 06:25
  • Thanks @ander2ed @NickCox. This was indeed the issue. The solution I have in mind is the following. The final goal is to create a dataset which stores the regression coefficients for each iteration. My idea is to, for each simulation, 0. create a stata dataset calle BetaFinal (empty for now). 1. create the regression coef. matrix (which is a column vector), then 2. temporarily convert to a stata dataset call it beta_`mc' (mc is the Montecarlo iteration number) and 3. append the corresponding dataset to BetaFinal set. Any ideas/suggestions? Thanks! – k1000x May 16 '16 at 22:43
  • 1
    Well, sure. But nothing that fits well into a comment or as a comment on this question. One general solution is to continue largely as you have, then employ `svmat` to create a temp data set from the coef. matrix (using `tempfile`) and `append` this coef. data set to the final data set. Be sure to look closely at the help files for `mkmat` and `macro`. I'll leave the details up to you. If you get stuck, feel free to post a new question with a reproducible example, including a description of the issue, sample data, and the desired result. – ander2ed May 17 '16 at 13:51
  • Thank you @ander2ed . Doing it now. Let's see how this goes. I'll post again if I encounter more problems. – k1000x May 19 '16 at 16:13