Search code examples
stata

How to allow for missing values in a summation in non-linear estimation


I am trying to do a non-linear estimation in Stata where some observations do not need all of the variables. The following is a made up example

nl (v1 = ({alpha=1})^({beta=1}*v2) + ({alpha})^({beta}*v3))

some times there is a value of v3, sometimes there isn't. If it is unneeded in the data, it is coded as missing (although its not missing in the sense the data is lacking, the data is perfect). When v3 is missing, I want Stata to treat the above expression as if the term with the v3 isnt there, so in these cases I would just want it to treat the expression for these observations as:

v1 = ({alpha=1})^({beta=1}*v2)

When I run this, stata says:

starting values invalid or some RHS variables have missing values

I know the starting values are fine,

As you can see, simply recoding the missing values to zero will not work. Because it doesn't zero out the term.

Is there something I can do with a sigma summation notation where it only adds the terms for which there are non-missing values?

-Thanks!


Solution

  • Something like this should work:

    cls
    sysuse auto, clear
    gen nm_rep78 = cond(missing(rep78),1,0)
    recode rep78 (.=0), gen(z_rep78)
    tab nm_rep78 z_rep78
    nl (price = ({alpha=1})^({beta=1}*mpg) + nm_rep78*({alpha})^({beta}*z_rep78))
    

    The idea is that you use an indicator variable to zero out the second term.

    There might be a way to get nl to use factor variable notation to simplify this, but I've been testing a new cocktail recipe all afternoon and should not attempt this.