very general question about calling user defined functions .

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
jasierra
Posts: 2
Joined: Sun Jan 02, 2011 11:17 am

very general question about calling user defined functions .

Unread post by jasierra »

Hi,

I am new to Rats, so please be patient ...
I want to estimate linear regressions with Hansen-Hodrick (JPE, 1980) autocorrelation-consistent std errors.
I thought I would just write a function that creates the adjusted covariance matrix, and just call it every time I need it.

this is what I have writen in a file named "hansenhodrick.src" (is this file extension correct? should it be .prg instead? or .txt ? or no extension at all ?)


function hansenhodrick X U nlags
type integer nlags
type rectangular X
type rectangular U
local rectangular ru
local rectangular Theta
local rectangular Omega
local rectangular vcov_HH
compute T = %nobs
compute K = %nreg
compute ru = %zeros(T,1)
do i = 0,(nlags-1)
compute ru(i+1,1) = %scalar(tr(%xsubvec(u,nlags,T))*%xsubvec(u,nlags-i,T-i))/T
end do
compute Omega = %zeros(T,T)
do i=1,T
do j=1,T
compute a = %iabs(j-i)
compute omega(i,j) = %scalar(%xsubvec(ru,1+a,1+a))
end do
end do
compute Theta = T*inv(tr(X)*X)*tr(X)*Omega*X*inv(tr(X)*X)
compute vcov_HH = (Theta/T)
compute hansenhodrick = vcov_HH
end hansenhodrick


Anyway, these calculations, if used in the main code (after a linreg command, extracting the residuals U, using the "make" command to build the X matrix of regressors, and doing "compute nlags = 12 " in a separate line) give me the correct vcov matrix. Its just that, when I try to do it by calling the function by itself, using:

compute hh_vcov = hansenhodrick(X,u,nlags)

RATS gives me the following error:

## SX11. Identifier HANSENHODRICK is Not Recognizable. Incorrect Option Field or Parameter Order?
>>>>ov = hansenhodrick(<<<<

I would reeeally appreciate any help or clarification about what I am doing wrong.

Thank you,
Jesus
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: very general question about calling user defined functio

Unread post by TomDoan »

jasierra wrote:Hi,

I am new to Rats, so please be patient ...
I want to estimate linear regressions with Hansen-Hodrick (JPE, 1980) autocorrelation-consistent std errors.
I thought I would just write a function that creates the adjusted covariance matrix, and just call it every time I need it.

this is what I have writen in a file named "hansenhodrick.src" (is this file extension correct? should it be .prg instead? or .txt ? or no extension at all ?)


function hansenhodrick X U nlags
type integer nlags
type rectangular X
type rectangular U
local rectangular ru
local rectangular Theta
local rectangular Omega
local rectangular vcov_HH
compute T = %nobs
compute K = %nreg
compute ru = %zeros(T,1)
do i = 0,(nlags-1)
compute ru(i+1,1) = %scalar(tr(%xsubvec(u,nlags,T))*%xsubvec(u,nlags-i,T-i))/T
end do
compute Omega = %zeros(T,T)
do i=1,T
do j=1,T
compute a = %iabs(j-i)
compute omega(i,j) = %scalar(%xsubvec(ru,1+a,1+a))
end do
end do
compute Theta = T*inv(tr(X)*X)*tr(X)*Omega*X*inv(tr(X)*X)
compute vcov_HH = (Theta/T)
compute hansenhodrick = vcov_HH
end hansenhodrick


Anyway, these calculations, if used in the main code (after a linreg command, extracting the residuals U, using the "make" command to build the X matrix of regressors, and doing "compute nlags = 12 " in a separate line) give me the correct vcov matrix. Its just that, when I try to do it by calling the function by itself, using:

compute hh_vcov = hansenhodrick(X,u,nlags)

RATS gives me the following error:

## SX11. Identifier HANSENHODRICK is Not Recognizable. Incorrect Option Field or Parameter Order?
>>>>ov = hansenhodrick(<<<<

I would reeeally appreciate any help or clarification about what I am doing wrong.

Thank you,
Jesus
First off, Hansen-Hodrick standard errors can be computed directly with RATS using the options LWINDOW=FLAT and LAGS=# of lags on the LINREG or similar instruction. And the calculation of your OMEGA matrix in the function can be done much more simply with the MCOV instruction.

That being said, the problem you have is that you need to do a

SOURCE hansenhodrick.src

instruction at some point before using the function in an expression. RATS treats procedures (which mimic RATS instructions) and functions (which mimic RATS built-in functions) differently when it comes to searching for source files. It will look for a procedure name, but not a function name. So user functions either have to be included directly in the program or explicitly SOURCEd.
moderator
Site Admin
Posts: 269
Joined: Thu Oct 19, 2006 4:33 pm

Re: very general question about calling user defined functio

Unread post by moderator »

First, the file extension doesn't matter--you can use whatever you want. We use .SRC (short for "source") for procedures and functions, and that's what we would recommend for users as well. But, you are free to use any extension.

As written, the function code doesn't seem to compile properly, which would explain why a subsequent call to the procedure would not work. The error I get is

compute hansenhodrick = vcov_HH
## SX22. Expected Type REAL, Got RECTANGULAR Instead
>>>>nhodrick = vcov_HH<<<<

You need to add a TYPE instruction to define the function as a RECTANGULAR explicitly (it defaults to type REAL):

function hansenhodrick X U nlags
type rect hansenhodrick
...

Try that and let us know if it doesn't fix the problem.

Regards,
Tom Maycock
Estima
jasierra
Posts: 2
Joined: Sun Jan 02, 2011 11:17 am

Re: very general question about calling user defined functio

Unread post by jasierra »

Dear Tom Maycock and Tom Doan,

Thanks for both of your replies. I fixed the code as you suggested, and now it works.

Thanks again,
Jesus
BoC
Post Reply