[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Generate new variable with an implicit formulation

From   "Austin Nichols" <>
Subject   Re: st: Generate new variable with an implicit formulation
Date   Fri, 17 Aug 2007 15:37:02 -0400

Nick Cox had some good suggestions on using an integer -forvalues-
loop and transforming it as necessary inside the loop, but I didn't
see any advice on solving the general problem.  Let me restate it as:
Solve f(x,y,n)=0 where x and n are variables and y is a variable to be

There are 2 existing methods I know of:
1. use -ridder- which can be obtained via -findit ridder- or
   net inst ssi5_4, from(
2. use -nl- and possibly a trick due to Al Feiveson:

However, a better solution might be to minimize f^2 (as suggested by
Stas Kolenikov at NASUG6) using a general optimizer; see e.g. help
mf_optimize in Stata 10 or help ml in Stata 9.

Other software might have the capability you seek built in, but it
would be instructive and fun to solve the problem in Stata, no?
Preferably in Stata 10 and writing code in Mata using mf_optimize, I

On 8/8/07, Bob Hammond <> wrote:
> I would like to define a new variable based on two current variables,
> but the new variable cannot be expressed as an explicit function of the
> current variables. The relationship is:
> x = n*y^(n-1) - (n-1)*y^n
> where I have data on x and n and would like to create a new variable, y.
> x and y are restricted to the real numbers on the open interval (0,1)
> and n is restricted to being a positive integer. Under these
> restrictions, the function is well-behaved and provides a unique
> solution, but not an explicit formulation. I can generate y with the
> following loop:
> forvalues i=0(0.001)1 {
> replace y=`i' if x - n*`i'^(n-1) + (n-1)*`i'^n < 0.0001 & x -
> n*`i'^(n-1) + (n-1)*`i'^n > -0.0001
> }
> but I am wondering if there is a more efficient and precise way. If this
> type of loop is best, what precision would you recommend for the steps
> of `i' and the tolerance (the ><0.0001)? (I'm on Intercooled Stata 9 for
> Windows if that matters.) Thanks in advance,
> Bob
*   For searches and help try:

© Copyright 1996–2022 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index