Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 72793

Testing nlcom for each value of a variable

$
0
0
Dear all

My nonlinear model contains twice the same variable [var1] with two different coefficients. In order to estimate the true marginal effect, I calculate a derivation with respect to var1. The obtained formula serves to estimate the respective nonlinear combination using the nlcom command. Since the derivation depends on var1, I have to chose a certain value to compute the nlcom. To show the result graphically, I want to recompute the nlcom at every value I actually observe for var1 (i.e. that I have in my data). The results (coefficient and standard error) shall be saved in two new variables.

My idea goes as follows:

*model estimation, where the coefficients (b1 and b2) are stored. Then

local i=1
local nobs = e(N)
matrix P = J(`nobs',1,.) //create the matrix
levelsof var1, local(i_var1) //create the local with all values of var1
foreach x of local i_var1 {
nlcom (_b[/b1]*(`x')/_b[/b2]) //test the nonlinear combination for each value contained in var1
local matrix s = r(b) //write the coefficient into the [1x1] matrix s
matrix P[`i',1] = s //write the value of s into the final matrix P on row i
local ++i //go to the next row of matrix P
}

matrix list P, format(%2.1f)

Of course, afterwards I will have to do the same for the variance and save the matrix P as variable.

Two problems arise:
1. After the loop is finished, the matrix P is still empty. However, if I do it by hand (just for the first observation for example) the procedure works.
2. The values in the local i_var1 are automatically sorted ascending and so they will be in the matrix P. So, if I save the matrix of results as a variable afterwards, they are not assigned to the correct observation.

Can you tell me, where the mistakes are in my code? Probably the entire procedure can even be done much easier.

Best regards

Viewing all articles
Browse latest Browse all 72793

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>