## Stata 15 help for twoway__function_gen

```
Title

[G-2] twoway__function_gen -- twoway function subroutine

Syntax

twoway__function_gen [[y] =] f(x) [if exp] ,
range(range)
xis(name)
[ n(#) generate(yvar xvar [, replace ]) droplines(numlist) ]

where f(x) is a "mathematical" expression with only one "mathematical"
variable that is identified in xis().

Description

Suppose that you wanted to plot y = f(x), where by f(x) we mean some
function of x.  x is not really a Stata variable (neither is y), but
twoway__function_gen can be used to generate Stata variables (usually
tempvars) that may represent x and y on an evenly spaced grid of values
over a specified range.

twoway__function_gen was written to help in parsing and generating
variables for graph twoway function; see [G-2] graph twoway function.
The expression in f(x) will only be evaluated if at least one of the
generate() or droplines() options is specified.

Options

range(range) specifies the range of values for x.  Here range can be a
pair of numbers identifying the minimum and maximum, or range can be
a variable.  If range is a variable, the range is determined by the
values of r(min) and r(max) after

. summarize range if exp, meanonly

xis(name) specifies a valid Stata name for x in f(x).

n(#) specifies the number of evaluation points.  The default is n(1).

generate(yvar xvar [, replace]) specifies the names of the variables to
generate.  The grid of values is placed in xvar, and the values of
f(xvar) are placed in yvar.  The replace option indicates that these
variables may be replaced if they already exist.

droplines(numlist) builds a list of x y pairs on the function f(x), where
each x is an element of numlist and y is the value of f(x).  The list
of x y pairs are returned in r(dropxy).  This option facilitates the
droplines() option of twoway graph function; see [G-2] graph twoway
function.

Examples

. clear

. set obs 10
number of observations (_N) was 0, now 10

. twoway__function_gen y = sin(c(pi)*x), r(-2 2) x(x) gen(y x, replace)
n(`c(N)')

. list y x

+-------------------------+
|          y            x |
|-------------------------|
1. |  2.449e-16           -2 |
2. |  .98480775   -1.5555556 |
3. |  .34202014   -1.1111111 |
4. |  -.8660254   -.66666667 |
5. | -.64278761   -.22222222 |
|-------------------------|
6. |  .64278761    .22222222 |
7. |   .8660254    .66666667 |
8. | -.34202014    1.1111111 |
9. | -.98480775    1.5555556 |
10. | -1.133e-15            2 |
+-------------------------+

. sysuse auto, clear
(1978 Automobile Data)

. sum mpg if for

Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
mpg |        22    24.77273    6.611187         14         41

. return list

scalars:
r(N) =  22
r(sum_w) =  22
r(mean) =  24.77272727272727
r(Var) =  43.70779220779221
r(sd) =  6.611186898567625
r(min) =  14
r(max) =  41
r(sum) =  545

. twoway__function_gen y = normden(x,`r(mean)',`r(sd)') if for, r(mpg)
x(x) gen(y x, replace) n(5)

. list y x in 1/5

+-------------------+
|         y       x |
|-------------------|
1. | .01599807      14 |
2. | .05014576   20.75 |
3. | .05542139    27.5 |
4. | .02159718   34.25 |
5. | .00296752      41 |
+-------------------+

Stored results

twoway__function_gen stores the following in r():

Scalars:

r(n)        number of evaluation points
r(min)      minimum of range()
r(max)      maximum of range()
r(delta)    distance between grid points

Macros:

r(yis)      y or "y" if not specified
r(xis)      x from xis(x) option
r(exp)      the expression f(x)
r(range)    range from range(range) option
r(yformat)  from yformat() option
r(xformat)  from xformat() option
r(dropxy)   x y pairs from droplines() option

```