Mata course: Translating from FORTRAN, example 2

The problem

1.
Calculate spearman rank correlation

2.
Calculate significance using Statalib routine 89

Step 1: Obtain FORTRAN code

1.
I visited Statlib site, found routine, and copied it
to .

Step 2: Translate to Mata

1. Copy prho.f to prho.do

2. Edit to create Mata program -- see .

a. Included mata set matastrict on

Step 3: Run do-file (compile), see what happens

. do prho1

. clear

.
. mata:

mata (type end to exit)

:
: mata set matastrict on

:
:
: real scalar prho(real scalar n, real scalar is, real scalar ifault)
>
> (output omitted)
> }
variable js undeclared
variable nfac undeclared
variable i undeclared
variable ifr undeclared
variable m undeclared
variable ise undeclared
variable n1 undeclared
variable mt undeclared
variable nn undeclared
(2 lines skipped)

r(3000);

end of do-file
r(3000);

Comments:

1. I checked that variables were not typographical errors.

2. I declared them:

real scalar js, nfac, i, ifr, m, ise, n1, mt, nn

3. I compiled again.

. do prho2

. clear

.
. mata:

mata (type end to exit)

:
: mata set matastrict on

:
:
: real scalar prho(real scalar n, real scalar is, real scalar ifault)
> {
> (output omitted)
> }
note: variable z unused

:
: end

3. I checked that variable z really was not used, then
removed its declaration, and compiled again.

. do prho3

. clear

.
. mata:

mata (type end to exit)

:
: mata set matastrict on

:
:
: real scalar prho(real scalar n, real scalar is, real scalar ifault)
> {
> (output omitted)
> }

:
: end

Notice, there are no messages.

Step 4: Make prho() easier to test

1.
Rename rpho() and sl89_prho()}

2.
Add new routine scalar_prho()

real scalar scalar_prho(real scalar n, real scalar r)
{
real scalar ifault
real scalar res

res = sl89_prho(n, (n^3-n)*(1-r)/6, ifault)
if (ifault) res = .
return(res)
}

3. Result in

Step 5: Tested result interactively

1. But just a little.

Step 6. Make an ado-file

1. See

2. Try it with

. clear
. set seed 29393
. set obs 100
. gen u1 = uniform()
. gen u2 = uniform()
. prho u1 u2

Result:

. prho u1 u2

Number of obs = 100
Spearman's rho = -0.0345

Test of Ho: u1 and u2 are independent
Prob > |t| = 0.7335
New test = 0.7330

Top