Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: how to write byable programs that call mata?
From
Tirthankar Chakravarty <[email protected]>
To
[email protected]
Subject
Re: st: how to write byable programs that call mata?
Date
Mon, 6 Aug 2012 01:58:26 -0700
This is not hard to do. The only thing that you need to check is
whether the variables that you will be using to store the results are
already defined. Here is a program and script that does what you want
*-------------------------------------------------
clear*
// define the program
cap program drop linproj
program define linproj, byable(recall)
version 12
syntax varlist(min=1) [if] [in], GENerate(string ) ///
[VARiance(varname )]
foreach x of varlist `varlist' {
cap confirm new variable `generate'`x'
if !_rc {
g `generate'`x' = .
}
local genvarlist "`genvarlist' `generate'`x'"
}
marksample touse
if ("`variance'"~= "") {
mata: fnLinProj(st_local("varlist"), ///
st_local("touse"), st_local("genvarlist"), ///
st_local("variance"))
}
else {
mata: fnLinProj(st_local("varlist"), ///
st_local("touse"), st_local("genvarlist"))
}
end
cap mata mata drop fnLinProj()
mata
// define the Mata function
function fnLinProj(string scalar sVarList, string scalar sToUse,
string scalar sGenVarList, | string scalar sVarianceVar) {
st_view(mX=., ., sVarList, sToUse)
if (args()==4) {
st_view(vVarianceVar, ., sVarianceVar, sToUse)
mVariance = diag(1:/vVarianceVar)
mXProj = mVariance*mX*invsym(mX'*mVariance*mX)
}
else {
mXProj = mX*invsym(mX'*mX) // the required matrix
}
st_store(., tokens(sGenVarList), sToUse, mXProj)
}
end
// generate some dummy data
set obs 100
drawnorm x1-x5
forvalues i=1/5 {
gen g`i' = rbinomial(1, 0.7)
}
egen guniq = group(g1-g5)
g variance = rchi2(10)
// test the program
bys guniq: linproj x1-x5, generate(proj_)
li proj_* guniq
bys guniq: linproj x1-x5, generate(proj2_) variance(variance)
li proj2_* guniq
*-------------------------------------------------
T
On Thu, Aug 2, 2012 at 4:52 PM, Pradipto Banerjee
<[email protected]> wrote:
> I'm new to mata and I have a problem for which I want to explore mata. I want to write a program for the following:
>
> 1.Inputs: (a) Stata variables are X1, X2, ..., Xn, (b) optional: a Stata variable S, (c) grouping variables (i.e. _by()): G1, G2, ..., Gn
>
> 2. If S is not supplied, within each combination of G1, G2, ..., Gn, i.e. "by G1 G2 ... Gn:"
> (a) within Mata, construct a matrix X = [X1 X2, ..., Xn]
> (b) calculated a new matrix Y = X * inverse ( transpose(X) * X)
> (c) let Y = [Y1, Y2, ..., Yn]
> (d) return back to Stata, new variables Y1, Y2, ..., Yn
>
> 3. If S is supplied, within each combination of G1, G2, ..., Gn, i.e. "by G1 G2 ... Gn:"
> (a) within Mata, construct a matrix X = [X1 X2, ..., Xn]
> (b) construct a matrix V whose diagonal elements are same as S, i.e. V_{i,i} = S_i, V_{i,j} = 0
> (c) invert matrix V, i.e. define V2 such that V2_{i,j} = 1/V_{i,j}
> (d) calculate a new matrix Y = V2 * X * inverse ( transpose(X) * V2 * X)
> (e) let Y = [Y1, Y2, ..., Yn]
> (f) return back to Stata, new variables Y1, Y2, ..., Yn
>
> Can someone assist me with this?
>
> Many thanks.
>
>
> This communication is for informational purposes only. It is not intended to be, nor should it be construed or used as, financial, legal, tax or investment advice or an offer to sell, or a solicitation of any offer to buy, an interest in any fund advised by Ada Investment Management LP, the Investment advisor. Any offer or solicitation of an investment in any of the Funds may be made only by delivery of such Funds confidential offering materials to authorized prospective investors. An investment in any of the Funds is not suitable for all investors. No representation is made that the Funds will or are likely to achieve their objectives, or that any investor will or is likely to achieve results comparable to those shown, or will make any profit at all or will be able to avoid incurring substantial losses. Performance results are net of applicable fees, are unaudited and reflect reinvestment of income and profits. Past performance is no guarantee of future results. All!
f!
> inancial data and other information are not warranted as to completeness or accuracy and are subject to change without notice.
>
> Any comments or statements made herein do not necessarily reflect those of Ada Investment Management LP and its affiliates. This transmission may contain information that is confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is strictly prohibited. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format.
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
--
Tirthankar Chakravarty
[email protected]
[email protected]
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/