# RE: st: string functions on the name of a variable

 From "Dev Vencappa" To Subject RE: st: string functions on the name of a variable Date Fri, 25 Nov 2005 18:36:02 +0000

```>
Nick, thanks for the solution you provided. I have tried it but got stuck and on second thoughts I realised it may be better to explain more clearly what I want exactly.

Suppose I have the following equation to estimate:

to=f(clli, clnl, ca, te, re)

We want to estimate a flexible fourier representation of the above. The final equation will have the first five terms, translog terms and trigonometric terms, as follows:

to= clli + clnl + ca + te + re + Translog terms + Fourier terms
where Translog terms are:  (0.5*clli^2)+(0.5*clnl^2) + (0.5*ca^2)+(0.5*te^2)+(0.5*ro^2)+ (clli * clnl) +  (clli * ca)+ (clli * te) + (clli * ro) +  (clnl * ca)+ (clnl * te) + (clnl * ro)+ (ca*te) + (ca*ro) + (te*ro)

where Fourier terms are

sin(clli) + cos (clli)+ sin (clnl) + cos (clnl) + sin(ca) + cos (ca) + sin (te) + cos (te) + sin (ro) + cos (ro)

By flexible fourrier I mean that one can have more layers of trigonometric terms that combine these variables in different ways, eg. combining two variables as follows:

sin(clli+ clnl) + cos (clli+clnl)+ sin (clli+ca) + cos (cli+ca) + sin (clli+te) + cos (clli+te) + sin (clli+ro) + cos (clli+ro) + sin (clnl+ca) + cos (clnl+ca) + sin (clnl+te) + cos (clnl +te) + sin(clnl+ro) + cos (clnl + ro) + sin (ca+te) + cos (ca+te) + sin (ca+ro) + cos (ca+ro) + sin (te+ro) + cos (te+ro)

Another layer of this function would be combining three variables in the same way, four variables and five variables (five would be the highest combination of variables one can create because there are only five right hand side variables)

Now, each of these right hand side variables is rescaled using a particular formulae (e.g. see  paper by A.  Kasnan (2002) Central Bank Review 1, pp. 1-20). I have copied below how I created the variables and the rescaling used.

gen to=lntoen
gen pro=lnprof
gen clli=lnclli
gen clnl=lnclnl
gen ca=lnca
gen te=lntech
gen ro=lnroin
gen pril=lnpril
gen prinl=lnprinl

local var "to pro clli clnl ca te ro"
foreach j of local var{
sum `j'
gen x=(1.6*_pi)/(r(max)-r(min))
gen z=(0.2*_pi)-(x*r(min))+(x*`j')
*z is the rescaling to be applied to each variable
gen R`j'=z
*create trigonometric terms for these rescaled variables
gen sin`j'=sin(R`j')
gen cos`j'=cos(R`j')
*create squared terms of the rescaled variable
gen `j'sqr=0.5*R`j'^2
drop x z
}

local z "clnl ca te ro"
foreach x of local z{
gen clli`x'=Rclli*R`x'
gen sinclli`x'=sin(Rclli+R`x')
gen cosclli`x'=cos(Rclli+R`x')
}

local z "ca te ro"
foreach x of local z{
gen clnl`x'=Rclnl*R`x'
}

gen cate=Rca*Rte
gen caro=Rca*Rro
gen tero=Rte*Rro

local z "prinl ca te ro"
foreach x of local z{
gen pril`x'=Rpril*R`x'
gen sinpril`x'=sin(Rpril+R`x')
gen cospril`x'=cos(Rpril+R`x')
}

local z "ca te ro"
foreach x of local z{
gen prinl`x'=Rprinl*R`x'
gen sinprinl`x'=sin(Rprinl+R`x')
gen cosprinl`x'=cos(Rprinl+R`x')
}

*create third layer of FF function
local z "sin cos"
foreach x of local z{
gen `x'clliclnlca=`x'(Rclli+Rclnl+Rca)
gen `x'clliclnlte=`x'(Rclli+Rclnl+Rte)
gen `x'clliclnlro=`x'(Rclli+Rclnl+Rro)
gen `x'cllicate=`x'(Rclli+Rca+Rte)
gen `x'cllicaro=`x'(Rclli+Rca+Rro)
gen `x'clnlcate=`x'(Rclnl+Rca+Rte)
gen `x'clnlcaro=`x'(Rclnl+Rca+Rro)
gen `x'clnltero=`x'(Rclnl+Rte+Rro)
gen `x'catero=`x'(Rca+Rte+Rro)
gen `x'prilprinlca=`x'(Rpril+Rprinl+Rca)
gen `x'prilprinlte=`x'(Rpril+Rprinl+Rte)
gen `x'prilprinlro=`x'(Rpril+Rprinl+Rro)
gen `x'prilcate=`x'(Rpril+Rca+Rte)
gen `x'prilcaro=`x'(Rpril+Rca+Rro)
gen `x'prinlcate=`x'(Rprinl+Rca+Rte)
gen `x'prinlcaro=`x'(Rprinl+Rca+Rro)
gen `x'prinltero=`x'(Rprinl+Rte+Rro)
*create fourth layer of FF function
gen `x'clliclnlcate=`x'(Rclli+Rclnl+Rca+Rte)
gen `x'clliclnlcaro=`x'(Rclli+Rclnl+Rca+Rro)
gen `x'clliclnltero=`x'(Rclli+Rclnl+Rte+Rro)
gen `x'cllicatero=`x'(Rclli+Rca+Rte+Rro)
gen `x'clnlcatero=`x'(Rclnl+Rca+Rte+Rro)
*fifth layer
gen `x'clliclnlcatero=`x'(Rclli+Rclnl+Rca+Rte+Rro)
}

To make it easier for estimation, I store the Translog terms in a global macro, and same for each of the layers of the fourier terms (i.e. first layer, second layer, etc)

My first question is how I find a shortcut way of creating these different combinations of fourier terms?
Clearly, somehow, Stata will need to be told that if two variables are combined once through a sine and cosine of their additions, then they cannot be combined a second time.

Second, suppose I have estimated an equation. Suppose I want to differentiate the above with respect to all variables that have clli in their names and add all these partial derivatives. This would be the addition of all these terms retrieved from the estimated coefficients. When there are interaction of terms,I am not too sure how to handle this in an automated function in Stata.

Suppose I have a translog term cllica which is the product of clli and ca. differentiating lntoen with respect to clli will be the coefficient of cllica multiplied by ca (I will evaluate this at mean values of the variables). Any thoughts on how I would go about doing this automatically please?

It gets a little bit more complicated for me when I try with the fourrier terms. Suppose my variable is

sincllica =sin (Rclli+Rca). The derivative with respect to clli is _b[sincllica]*cos(clli+ca)

or if

coscllica =cos (Rclli+Rca). The derivative with respect to clli is _b[coscllica]* - sin(Rclli+Rca)

How would I tell Stata to automatically deal with that (i.e recognise where there are sin and cosine variables (I guess wildcards will provide the solution here but I can't figure out how to write this)? The solution to this will be the solution for the third layers, fourth layers and fifth layers as well.

Apologies for this long email, but I would really be grateful if a shortcut solution to this problem were available as I am finding myself writing a very long do file to work this out.

Dev

>>> n.j.cox@durham.ac.uk 11/21/05 11:14 am >>>
I don't see that you need a function here.

The set of variables with "clli" as part of
their names is given by the wildcard  *clli*
and so can be put into a macro by

unab clli : *clli*

Presumably you know what variables appear
on the RHS of your regression and can manufacture
a local macro containing them, say rhs.

The intersection is then

local cllirhs : list clli & rhs

and you can cycle over these with

foreach v of local cllirhs {
... = ... + _b[`v']
}

Assuming that by Fourrier you mean Fourier, I don't see why
handling a set of Fourier terms need be messy. The utility
-fourier- from -circular- on SSC provides one way of getting
a bunch of them.

Alternatively, in one recent program I needed to generate a bunch
of them on the fly. Here is the code generalised mildly:

// regression in terms of sin j theta, cos j theta
local sc
local terms = <plug in your own>

forval j = 1/`terms' {
tempvar S C
gen `S' = sin(`j' * `theta')
gen `C' = cos(`j' * `theta')
local sc `sc' `S' `C'
}

// regression will fail if data points too few
// or response missing
// assumes a `touse' marking observations to be used

capture regress <response to be plugged in> `sc' if `touse'
if _rc gen `sm' = . if `touse'
else predict `sm' if `touse'

drop `sc'

On the other hand, quite what a "Flexible" Fourier analysis
is I don't know.

Nick
n.j.cox@durham.ac.uk

Dev Vencappa

>  I have the following problem and would be grateful if you
> could provide some help:
>
> Suppose I have a regression equation with lntoen as my
> dependent variable and a lot of right hand side variables
> called lncllisqr lncllica lncllite lncaclli (and there are
> more variables like these which contain the word "clli" in
> their name as well as other variables named differently).
> After estimation, I am trying to add the coefficients of all
> variables in an estimation that have "clli" in their names.
> While I can do this manually by using _b[variablename],  I am
> faced with a problem as I am estimating a Flexible Fourrier
> function, which tends to be very messy in terms of the
>
> I am trying to differentiate lntoen with respect to all
> variables that contain "clli" in their names. I am trying to
> of all variables that contain "clli" in their names.
>
> Does anybody know if there is an equivalent to one of the
> list of string functions (such as e.g. substr) that can allow
> me to write some condition to check if the variable contains
> the characters "clli" in its name and then add their
>
> Apologies if a similar posting has been up on the list before
> and escaped my attention.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

This message has been checked for viruses but the contents of an attachment
may still contain software viruses, which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```