{smcl} {bf:Mata course: Translating from FORTRAN, example 2} {hline} {title:The problem} {p 8 12 2} 1. Calculate spearman rank correlation {p 8 12 2} 2. Calculate significance using Statalib routine 89 {title:Step 1: Obtain FORTRAN code} {p 8 12 2} 1. I visited Statlib site, found routine, and copied it to {bf:{view prho.f}}. {title:Step 2: Translate to Mata} {p 8 12 2} 1. Copy {bf:prho.f} to {bf:prho.do} {p 8 12 2} 2. Edit to create Mata program -- see {bf:{view prho1.do}}. {p 12 16 2} a. Included {cmd:mata set matastrict on} {title:Step 3: Run do-file (compile), see what happens} {txt} {com}. do prho1 {txt} {com}. clear {txt} {com}. . mata: {txt}{hline 29} mata (type {cmd:end} to exit) {hline} {com}: : mata set matastrict on : : : real scalar prho(real scalar n, real scalar is, real scalar ifault) > {c (-} > {it:(output omitted)} > {c )-} {err}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) {txt}{hline} {search r(3000):r(3000);} end of do-file {search r(3000):r(3000);}{txt} {p 4 4 2} Comments: {p 8 12 2} 1. I checked that variables were not typographical errors. {p 8 12 2} 2. I declared them: {cmd:real scalar js, nfac, i, ifr, m, ise, n1, mt, nn} {p 8 12 2} 3. I compiled again. {com}. do prho2 {txt} {com}. clear {txt} {com}. . mata: {txt}{hline 29} mata (type {cmd:end} to exit) {hline} {com}: : mata set matastrict on : : : real scalar prho(real scalar n, real scalar is, real scalar ifault) > {c -(} > {it:(output omitted)} > {c )-} {txt}note: variable z unused {com}: : end {txt}{hline} {p 8 12 2} 3. I checked that variable {cmd:z} really was not used, then removed its declaration, and compiled again. {com}. do prho3 {txt} {com}. clear {txt} {com}. . mata: {txt}{hline 29} mata (type {cmd:end} to exit) {hline} {com}: : mata set matastrict on : : : real scalar prho(real scalar n, real scalar is, real scalar ifault) > {c -(} > {it:(output omitted)} > {c )-} {com}: : end {txt}{hline} {p 4 4 2} Notice, there are no messages. {title:Step 4: Make prho() easier to test} {p 8 12 2} 1. Rename {cmd:rpho()} and sl89_prho()} {p 8 12 2} 2. Add new routine {cmd:scalar_prho()} {cmd}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) }{txt} {p 8 12 2} 3. Result in {bf:{view prho4.do}} {title:Step 5: Tested result interactively} {p 8 12 2} 1. But just a little. {title:Step 6. Make an ado-file} {p 8 12 2} 1. See {bf:{view prho.ado}} {p 8 12 2} 2. Try it with . {cmd:clear} . {cmd:set seed 29393} . {cmd:set obs 100} . {cmd:gen u1 = uniform()} . {cmd:gen u2 = uniform()} . {cmd:prho u1 u2} {p 12 12 2} Result: {com}. prho u1 u2 {txt} Number of obs = {res} 100 {txt}Spearman's rho = {res} -0.0345 {txt}Test of Ho: u1 and u2 are independent Prob > |t| = {res} 0.7335 {txt} New test = {res} 0.7330{txt} {hline} {bf:{view talk.smcl:Top}}