On Mon, 4 Jan 2021, Allin Cottrell wrote:
On Mon, 4 Jan 2021, Sven Schreiber wrote:
> Am 04.01.2021 um 09:47 schrieb Artur Bala:
>>> Use the argname() function.
>>>
>>> Allin
>>
>> Yeah, that's quite clear but argname() generates a string not a
>> series. And my point is different. Let's have the following:
>> <\hans>
>> function matrix myfunction (series depY, list X)
>> # some coding
>> ols depY X
>> # some coding
>> return something
>> end function
>> <hans>
>>
>> Now, when I run my function, let's say "mymatrix = myfunction(Price,
>> xlist)", OLS printing will show up: "Dependent variable: depY"
instead
>> of "Dependent variable: Price".
>> Is there a way to have the actual name of depY displayed?
>
> You could copy the input using argname, for example:
> ID = genseries(argname(depY), depY)
>
> Then you should be able to use the ID number also in the OLS command.
> Of course the copying doesn't make it 100% efficient, but the loss
> should be minor.
Or if you don't mind using string substitution, you could use "rename":
<hansl>
function matrix myfunction (series depY, list X)
string realname = argname(depY)
rename depY @realname
ols @realname X
...
end function
</hansl>
The problem with these approaches is that what you pass to the function
may not be a valid identifier, for example
myfunction(log(y), X)
and anything based on argname would break down in that case. If what you
want is be sure that what your function prints makes sense I would suggest
to fine-tune the printout using tools like printf, sprintf() and
modprint, which give you complete control on what to print and how. For
example, here's a completley silly function that returns the OLS
coefficient, halved.
<hansl>
set verbose off
function matrix OLS_half(const series y, list X)
sname = argname(y)
if strlen(sname) == 0
sname = "(anonymous series)"
endif
parnames = varnames(X)
ols y X --quiet
bs = ($coeff ~ $stderr) / 2
printf "Silly output for %s\n", sname
modprint bs parnames
return bs
end function
open data4-1.gdt
list X = const sqft
ols price X
OLS_half(price, X)
OLS_half(log(price), X)
</hansl>
-------------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Scienze Economiche e Sociali (DiSES)
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------