Hi,
I have a seemingly trivial problem with a regression function in Mata. I'm using rangestat in conjunction with a Mata function (myreg) to estimate as series of cross-sectional regressions in a panel data set. The function works fine until I include a calculation for the residual, which I call ehat in the function. The lines of code I run are:
rangestat (myreg) lead_in2nav_w ln_nav, by(date_ym) interval(date_ym . date_ym) casewise
The error I get is:
myreg(): 3200 conformability error
do_flex_stats(): - function returned error
<istmt>: - function returned error
r(3200);
Obviously something is incorrect, but I have checked the relevant matrices for conformability and cannot find anything to indicate that they are not conformable.
The Mata function is a modified version of one I found in this forum. I've included a sample of my data set as well. The panel is unbalanced, but the function works when I do not try to calculate ehat.
Any suggestions are welcome. It's probably a silly error on my part.
MATA FUNCTION
* Linear regression in Mata using quadcross()
mata:
mata clear
real rowvector myreg(real matrix Xall)
{
real colvector y, b, Xy, ehat
real matrix X, XX
real scalar ymean, tss, mss, r2, r2a, rss
y = Xall[.,1]
X = Xall[.,2::cols(Xall)]
X = X,J(rows(X),1,1)
XX = quadcross(X, X)
Xy = quadcross(X, y)
b = invsym(XX) * Xy
ymean = mean(y)
tss = sum((y :- ymean) :^ 2)
mss = sum( (X * b :- ymean) :^ 2)
rss = sum( (y - X * b ) :^ 2)
r2 = mss / tss
r2a = 1 - (1 - r2) * (rows(X) - 1) / (rows(X) - cols(X))
ehat = y - X * b
return(rows(X), r2, r2a, b', tss, mss, rss, ehat)
}
end
SAMPLE FROM DATA SET
I have a seemingly trivial problem with a regression function in Mata. I'm using rangestat in conjunction with a Mata function (myreg) to estimate as series of cross-sectional regressions in a panel data set. The function works fine until I include a calculation for the residual, which I call ehat in the function. The lines of code I run are:
rangestat (myreg) lead_in2nav_w ln_nav, by(date_ym) interval(date_ym . date_ym) casewise
The error I get is:
myreg(): 3200 conformability error
do_flex_stats(): - function returned error
<istmt>: - function returned error
r(3200);
Obviously something is incorrect, but I have checked the relevant matrices for conformability and cannot find anything to indicate that they are not conformable.
The Mata function is a modified version of one I found in this forum. I've included a sample of my data set as well. The panel is unbalanced, but the function works when I do not try to calculate ehat.
Any suggestions are welcome. It's probably a silly error on my part.
MATA FUNCTION
* Linear regression in Mata using quadcross()
mata:
mata clear
real rowvector myreg(real matrix Xall)
{
real colvector y, b, Xy, ehat
real matrix X, XX
real scalar ymean, tss, mss, r2, r2a, rss
y = Xall[.,1]
X = Xall[.,2::cols(Xall)]
X = X,J(rows(X),1,1)
XX = quadcross(X, X)
Xy = quadcross(X, y)
b = invsym(XX) * Xy
ymean = mean(y)
tss = sum((y :- ymean) :^ 2)
mss = sum( (X * b :- ymean) :^ 2)
rss = sum( (y - X * b ) :^ 2)
r2 = mss / tss
r2a = 1 - (1 - r2) * (rows(X) - 1) / (rows(X) - cols(X))
ehat = y - X * b
return(rows(X), r2, r2a, b', tss, mss, rss, ehat)
}
end
SAMPLE FROM DATA SET
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str10 fundid float date_ym double(lead_in2nav_w ln_nav) "FS00008KNP" 612 . . "FS00008KNP" 613 . . "FS00008KNP" 614 . . "FS00008KNP" 615 . . "FS00008KNP" 616 1.290202 -1.609438 "FS00008KNP" 617 .3964209 1.178369 "FS00008KO8" 618 .2737324 -.7218018 "FS00008KO8" 619 .4026174 -.5599073 "FS00008KO8" 620 .582642 -.3225676 "FS00008KO8" 621 .0090997 .2766691 "FS00008KO8" 622 .0074663 .2921792 "FS00008KO8" 623 . .3017958 "FS00008KSC" 612 . . "FS00008KSC" 613 . . "FS00008KSC" 614 . . "FS00008KSC" 615 . . "FS00008KSC" 616 . . "FS00008KSC" 617 . . "FS00008KSC" 618 .0009254 1.686963 "FS00008KSC" 619 .0007928 1.618535 "FS00008KSC" 620 .0043706 1.520827 "FS00008KSC" 621 .0107276 1.616162 "FS00008KSC" 622 .0202638 1.616135 "FS00008KSC" 623 .0041988 1.609716 "FS00008L07" 616 . . "FS00008L07" 617 . . "FS00008L07" 618 .5208364 2.399778 "FS00008L07" 619 .1198361 2.837028 "FS00008L07" 620 .050663 2.744303 "FS00008L07" 621 .0505302 2.890874 "FS00008L07" 622 .0598396 2.915028 "FS00008L07" 623 .0480914 2.848323 end format %tm date_ym
Comment