Hallo Everyone:
I have compiled a function in Mata and I can run it in the interactive mode (lasts about 30 minutes). This Mata function needs a series of matrices which I have already put into Mata using the "putmata" syntax. My problem is that I cannot execute the function if I call it in the "nlgrid" program. I tried to put the program and the mata into different files but that still did not work. The major part of my do-file is as follows. Thanks in advance for your comments and suggestions.
putmata estdate myid_bodqrt0 = (myid_bodqrt1 myid_bodqrt2 myid_bodqrt3 myid_bodqrt4) ageqrt0 = (ageqrt1 ageqrt2 ageqrt3 ageqrt4) Eqrt0 = (Eqrt1 Eqrt2 Eqrt3 Eqrt4) EBqrt0 = (EBqrt1 EBqrt2 EBqrt3 EBqrt4) denomqrt0 = (denomqrt1 denomqrt2 denomqrt3 denomqrt4) deposum omega myid cntynumb, replace
cap mata: mata drop eqexpbr()
version 14.0
mata:
void eqexpbr(real scalar k)
{
real matrix Bu, myid_bodqrt0, myid_blc, UU, estdate, ageqrt0, denomqrt0, eqall, C1, cntynumb, C2, S, eqlogmat, Eqrt0, omega, EB
Bu = uniqrows(myid_bodqrt0)
myid_blc = vec(Bu)
UU = J(rows(Bu),4,.)
for (q=1; q<=4; q++) {
for (i=1; i<=rows(estdate); i++) {
if (ageqrt0[i,q]>90) {
for (j=90; j<=ageqrt0[i,q]-1; j++) {
denomqrt0[i,q] = denomqrt0[i,q] + (ageqrt0[i,q]-j)^k
}
}
C1 = eqall[,1..4]
C2 = J(rows(C1),cols(C1),cntynumb[i])
S = C1-C2
Sprod = S[,1]:*S[,2]:*S[,3]:*S[,4]
pos = Sprod:==0 // pos = 1 if the countyfp is matched.
if (sum(pos) != 0) {
eqlogmat = select(eqall[,5..6],pos)
}
for(l=1; l<=rows(eqlogmat); l++) {
if (eqlogmat[l,2]>=estdate[i] & denomqrt0[i,q]>0 & Eqrt0[i,q]>=0) {
Eqrt0[i,q] = Eqrt0[i,q]+(eqlogmat[l,2]-estdate[i])^k/denomqrt0[i,q]*eqlogmat[l,1]
}
}
}
for (u=1; u<=rows(Bu); u++) {
UU[u,]=colsum(omega:*Eqrt0:*rowsum(myid_bodqrt0:==Bu[u,]))
}
}
EB = vec(UU)
stata("getmata EB, id(myid_blc) replace")
}
end
capture program drop nleqexp
program nleqexp
version 14.0
syntax [varlist(default=none)] if, at(name)
tokenize `varlist'
marksample touse
local n : word count `varlist'
local n = `n'-1 // no intercept in these cases
tempname a b k
local atn = colsof(`at') // includes expl var | learning-from-experience prelim coeff | intercept
local pn = `atn'-`n' // # of learning-from-experience parameters: b, c, k
scalar `k' = `at'[1,`n']
scalar `a' = 0
qui replace `1' = `a' `if' // token `1' is the depvar, here start replacing it with fitted value
forvalues i=2/`n' {
tempname ipar
scalar `ipar' = `at'[1,`i'-1] // retrieve parameter values
qui replace `1' = `1' + `ipar'*``i'' `if' // calculates function value
}
end
capture program drop nlgrid
program nlgrid, eclass
version 14.0
syntax varlist [if] [, KLOW(real 1) KSTEP(real 1) KHI(real 1)]
marksample touse
gettoken depvar explvar: varlist
tempname rss b minrss binit binitmod kmin v vinit
local parnl "k"
forvalues k = `klow'(`kstep')`khi' {
mata: eqexpbr(`k')
qui reg `varlist' if `touse', noconstant
scalar `rss' = e(rss)
matrix `b' = e(b)
matrix `v' = vecdiag(e(V))
if `k' == `klow' {
scalar `minrss' = `rss'
matrix `binit' = `b'
matrix `vinit' = `v'
scalar `kmin' = `k'
}
else if `rss' < `minrss' {
scalar `minrss' = `rss'
matrix `binit' = `b'
matrix `vinit' = `v'
scalar `kmin' = `k'
}
di as text "k " as result `k'
di as text "rss " as result `rss'
}
di as text "k " as result `kmin'
di as text "rss " as result `minrss'
local nvar = colsof(`binit')
local pvar : colfullnames(`binit') // extract variable names
tokenize `pvar' // this way variables that reg above dropped are also dropped
local explvar ""
qui replace `touse' = e(sample)
forvalues h = 1/`nvar' {
matrix `binitmod' = nullmat(`binitmod'),`binit'[1,`h']
local explvar "`explvar' ``h''"
}
matrix `binit' = `binitmod',`kmin'
nl eqexp @ `depvar' `explvar' if `touse', variables(`explvar') parameters(`explvar' `parnl') initial(`binit') // delta(4e-4)
end
eststo r1: xi: nlgrid EquityRatio rcon2170 ROA ImputedIntRateTT RERatio NonHazPortion Bankage i.YearReporting if (EquityRatio ~=.), klow(0.8) kstep(0.4) khi(4)
In addition, the related error messages are:
eststo r1: xi: nlgrid EquityRatio rcon2170 ROA ImputedIntRateTT RERatio NonHazPortion Bankage
> i.YearReporting if (EquityRatio ~=.), klow(0.8) kstep(0.4) khi(4)
i.YearReporting _IYearRepor_1994-2013(naturally coded; _IYearRepor_1994 omitted)
vector or matrix myid_blc not found
stata(): 3598 Stata returned error
eqexpbr(): - function returned error
<istmt>: - function returned error
I have compiled a function in Mata and I can run it in the interactive mode (lasts about 30 minutes). This Mata function needs a series of matrices which I have already put into Mata using the "putmata" syntax. My problem is that I cannot execute the function if I call it in the "nlgrid" program. I tried to put the program and the mata into different files but that still did not work. The major part of my do-file is as follows. Thanks in advance for your comments and suggestions.
putmata estdate myid_bodqrt0 = (myid_bodqrt1 myid_bodqrt2 myid_bodqrt3 myid_bodqrt4) ageqrt0 = (ageqrt1 ageqrt2 ageqrt3 ageqrt4) Eqrt0 = (Eqrt1 Eqrt2 Eqrt3 Eqrt4) EBqrt0 = (EBqrt1 EBqrt2 EBqrt3 EBqrt4) denomqrt0 = (denomqrt1 denomqrt2 denomqrt3 denomqrt4) deposum omega myid cntynumb, replace
cap mata: mata drop eqexpbr()
version 14.0
mata:
void eqexpbr(real scalar k)
{
real matrix Bu, myid_bodqrt0, myid_blc, UU, estdate, ageqrt0, denomqrt0, eqall, C1, cntynumb, C2, S, eqlogmat, Eqrt0, omega, EB
Bu = uniqrows(myid_bodqrt0)
myid_blc = vec(Bu)
UU = J(rows(Bu),4,.)
for (q=1; q<=4; q++) {
for (i=1; i<=rows(estdate); i++) {
if (ageqrt0[i,q]>90) {
for (j=90; j<=ageqrt0[i,q]-1; j++) {
denomqrt0[i,q] = denomqrt0[i,q] + (ageqrt0[i,q]-j)^k
}
}
C1 = eqall[,1..4]
C2 = J(rows(C1),cols(C1),cntynumb[i])
S = C1-C2
Sprod = S[,1]:*S[,2]:*S[,3]:*S[,4]
pos = Sprod:==0 // pos = 1 if the countyfp is matched.
if (sum(pos) != 0) {
eqlogmat = select(eqall[,5..6],pos)
}
for(l=1; l<=rows(eqlogmat); l++) {
if (eqlogmat[l,2]>=estdate[i] & denomqrt0[i,q]>0 & Eqrt0[i,q]>=0) {
Eqrt0[i,q] = Eqrt0[i,q]+(eqlogmat[l,2]-estdate[i])^k/denomqrt0[i,q]*eqlogmat[l,1]
}
}
}
for (u=1; u<=rows(Bu); u++) {
UU[u,]=colsum(omega:*Eqrt0:*rowsum(myid_bodqrt0:==Bu[u,]))
}
}
EB = vec(UU)
stata("getmata EB, id(myid_blc) replace")
}
end
capture program drop nleqexp
program nleqexp
version 14.0
syntax [varlist(default=none)] if, at(name)
tokenize `varlist'
marksample touse
local n : word count `varlist'
local n = `n'-1 // no intercept in these cases
tempname a b k
local atn = colsof(`at') // includes expl var | learning-from-experience prelim coeff | intercept
local pn = `atn'-`n' // # of learning-from-experience parameters: b, c, k
scalar `k' = `at'[1,`n']
scalar `a' = 0
qui replace `1' = `a' `if' // token `1' is the depvar, here start replacing it with fitted value
forvalues i=2/`n' {
tempname ipar
scalar `ipar' = `at'[1,`i'-1] // retrieve parameter values
qui replace `1' = `1' + `ipar'*``i'' `if' // calculates function value
}
end
capture program drop nlgrid
program nlgrid, eclass
version 14.0
syntax varlist [if] [, KLOW(real 1) KSTEP(real 1) KHI(real 1)]
marksample touse
gettoken depvar explvar: varlist
tempname rss b minrss binit binitmod kmin v vinit
local parnl "k"
forvalues k = `klow'(`kstep')`khi' {
mata: eqexpbr(`k')
qui reg `varlist' if `touse', noconstant
scalar `rss' = e(rss)
matrix `b' = e(b)
matrix `v' = vecdiag(e(V))
if `k' == `klow' {
scalar `minrss' = `rss'
matrix `binit' = `b'
matrix `vinit' = `v'
scalar `kmin' = `k'
}
else if `rss' < `minrss' {
scalar `minrss' = `rss'
matrix `binit' = `b'
matrix `vinit' = `v'
scalar `kmin' = `k'
}
di as text "k " as result `k'
di as text "rss " as result `rss'
}
di as text "k " as result `kmin'
di as text "rss " as result `minrss'
local nvar = colsof(`binit')
local pvar : colfullnames(`binit') // extract variable names
tokenize `pvar' // this way variables that reg above dropped are also dropped
local explvar ""
qui replace `touse' = e(sample)
forvalues h = 1/`nvar' {
matrix `binitmod' = nullmat(`binitmod'),`binit'[1,`h']
local explvar "`explvar' ``h''"
}
matrix `binit' = `binitmod',`kmin'
nl eqexp @ `depvar' `explvar' if `touse', variables(`explvar') parameters(`explvar' `parnl') initial(`binit') // delta(4e-4)
end
eststo r1: xi: nlgrid EquityRatio rcon2170 ROA ImputedIntRateTT RERatio NonHazPortion Bankage i.YearReporting if (EquityRatio ~=.), klow(0.8) kstep(0.4) khi(4)
In addition, the related error messages are:
eststo r1: xi: nlgrid EquityRatio rcon2170 ROA ImputedIntRateTT RERatio NonHazPortion Bankage
> i.YearReporting if (EquityRatio ~=.), klow(0.8) kstep(0.4) khi(4)
i.YearReporting _IYearRepor_1994-2013(naturally coded; _IYearRepor_1994 omitted)
vector or matrix myid_blc not found
stata(): 3598 Stata returned error
eqexpbr(): - function returned error
<istmt>: - function returned error
Comment