Hi,
I am new to Mata and trying to let Mata works on the following code. But, it doesn't work and I couldn't find the error. The idea of the following code is that I have generated two variables - d and dbar (mean value of d) - and use the function st_view to create a matrix from the Stata dataset. These variables are then used in for loop. For every 'k' value, which is the lag value, I will compute the g_k and sum the autocovariance at every 'k' to get g_s. The 'k' value depends on 'h', which is a local macro from Stata. When I run the following code, I get the following error. Both 'd' and 'dbar' series appear on the result window when I enter mata: X. Similarly, when I enter mata: d, the 'd' series does appear too. Only when I enter mata: dbar, the following error pop out.
<istmt>: 3499 dbar not found
Following are my code:
Another issue is that the first observation of 'd' appears on the second row, not on the first row when I check d[215+k+1::588,1]. Does anyone know what's the problem here? Is it due to the observation range I specified in the matrix is inappropriate?
I would greatly appreciate if anyone could point out errors in my code. Many thanks in advance!
I am new to Mata and trying to let Mata works on the following code. But, it doesn't work and I couldn't find the error. The idea of the following code is that I have generated two variables - d and dbar (mean value of d) - and use the function st_view to create a matrix from the Stata dataset. These variables are then used in for loop. For every 'k' value, which is the lag value, I will compute the g_k and sum the autocovariance at every 'k' to get g_s. The 'k' value depends on 'h', which is a local macro from Stata. When I run the following code, I get the following error. Both 'd' and 'dbar' series appear on the result window when I enter mata: X. Similarly, when I enter mata: d, the 'd' series does appear too. Only when I enter mata: dbar, the following error pop out.
<istmt>: 3499 dbar not found
Following are my code:
Code:
local h = 3 local T = 373-`h'+1 egen dbar = mean(d) in 216/588 egen v = mean((d-dbar)^2) scalar g_0 = v mata: h = strtoreal(st_local("h")) T = strtoreal(st_local("T")) st_view(X=., ., ("d", "dbar")) for (k = 1; k<=`h'-1 ; k++) { g_s = J(`h'-1,1,.) g_k = (1/`T')*((d[215+k+1::588,1]-dbar[215+k+1::588,2])'*(d[216::588-k,1]-dbar[216::588-k,2])) g_s[k,1] = gamma_k g = st_numscalar("g_0") + 2*sum(g_s) } end
I would greatly appreciate if anyone could point out errors in my code. Many thanks in advance!
Comment