Next Upcoming Google+ Hangout: Tuesday, August 27 @ 7PM (CST) - To Participate CLICK HERE

Search For Topics/Content
Help Me, Help You...
This form does not yet contain any fields.

    Misc. Stats Topics Discussion > Programming 2-stage instrument regressing

    Dear Statistician:

    I am trying to program ivregress 2sls function in Mata and i
    have following mata code, but i get error that number of names and number of
    cols do not match:

    Is it possible to program ivregress 2sls with this syntax, meaning passing
    all dep, indep and instrument variables together, or do i need to pass
    instrument variable separately.


    capture mata mata drop m_ivols()
    version 10
    mata
    void m_ivols(string scalar varlist, string scalar touse)
    {
    real matrix M, X, V,Pz, PzX, PzY
    real colvector y, b, Z
    real scalar n, k, s2


    M = X = y = Z = .
    st_view(M, ., tokens(varlist), touse) // Whole data set
    st_subview(y,M,.,1) // 1st column dep variable
    st_subview(Z,M,.,2) // 2nd column instrument variable (only 1
    instrument)
    st_subview(X,M,.,(3\.)) // 3rd to last column all indep variables

    n=rows(X)
    k=cols(X)+1
    Pz = Z*invsym(Z'*Z)*Z'
    PzX = Pz*X
    PzY = Pz*y
    XX=cross(X,1,PzX,1)
    Xy=cross(X,1,Pzy,0)
    b=cholsolve(XX,Xy)
    e=y-(X,J(n,1,1))*b
    s2=(e'e)/(n-k)
    V=s2*cholinv(XX)


    st_eclear()
    st_matrix("r(b)",b)
    st_matrix("r(V)",V)
    st_numscalar("r(N)",n)
    }
    end

    ***************************************************************************

    capture program drop m_ivols()
    program define ivols, eclass
    version 10.0
    syntax varlist(numeric) [if] [in]
    gettoken depvar indepvar: varlist
    marksample touse


    mata: m_ols("`varlist'","`touse'")


    tempname b V
    matrix `b'=r(b)'
    matrix `V'=r(V)
    local N=r(N)


    matname `b' `indepvar' _cons, c(.)
    matname `V' `indepvar' _cons
    ereturn post `b' `V', depname (`depvar') obs(`N') esample(`touse')
    ereturn local cmd="ols"
    ereturn display
    end


    I will really appreciate if some one can help me with this problem.

    Thanks Harshil

    October 24, 2011 | Unregistered Commenterharshil

    Dear Harshil,

    First of all, allow me to apologize for the extremely delayed nature of my response. Unfortunately, my forum notifications were being sent to an inactive email, so I didn't become aware of new posts for a long time.

    With that said, I'm afraid your question is beyond the scope of this discussion forum, as I'm not familiar with Mata and it seems like as much of a programming question as stats question. Best of luck.

    February 23, 2012 | Registered CommenterJeremy Taylor