Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Call a scalar in stata()

    Hello everyone,
    I am new to using Mata within Stata and have the following problem. I am trying to run multiple spline regressions using a loop and (probably) need both stata and mata commands for that. However, I have a problem using a scalar sca_a1 in the
    Code:
    stata("mkspline t1 `=scalar(`sca_a1')' t2=t")
    My complete loop over matrix X looks as follows:
    Code:
    for (i=1; i<=50; i++) {
        if (X[i,3]==1 & X[i+1,3]==1) {
        st_numscalar("sca_a1", i)
        stata("mkspline t1 `=scalar(`sca_a1')' t2=t")
        ...
        stata("drop t1 t2")
        }
    }
    
    end
    t is the time variable in years in the original stata dataset.
    Google told me that the problem is probably that the stata() command still uses the scalars within Mata. So maybe there is a way to directly use i in the stata() command?
    Any help how I could implement this? I somewhere saw a post by Nick Cox saying that it is probably best not to pass locals and scalars between mata and stata, but how do I code it then? My ultimate goal is to save the F-statistics of the regressions and choose the date with the maximum F-stat as the break year.
    Thank you very much!

    P.S.: I crosslisted on https://stackoverflow.com/questions/...calar-in-stata
    Last edited by Max Bertoldi; 29 Sep 2016, 11:25.

  • #2
    Have you tried
    Code:
    stata("mkspline t1 `=scalar(sca_a1)' t2=t")
    E.g. without the quotes around the scalar name? AFAIK you never refer to a scalar with quotes. That said, which part of your estimation makes you require mata? It might be possible to just stay in Stata.

    Comment


    • #3
      Hi Jesse,
      thanks for answering. When I write
      Code:
      stata("mkspline t1 `=scalar(sca_a1)' t2=t")
      I get the error message
      't2' found where integer expected
      . I think I need mata, because I only want to run the spline regression under the if-condition. So I have a variable in my stata dataset that is equal to 1 if t is a possible break-point. Now I have multiple possible break points and want to identify the ultimate one using the F-stat of the spline regressions. I don't want to do it by hand, because in the end I would like to loop this code over a panel data set.

      Comment


      • #4
        Stata also has if-conditions, see help ifcmd.

        Comment


        • #5
          I agree that Mata seems like more than is needed. But given Mata, how about
          Code:
              stata(`"mkspline t1 " `=sca_a1' " t2=t""')

          Comment


          • #6
            Thanks everyone. I did not manage to code it in mata but instead used the matrices within stata. The code now looks as follows (including a loop over the countries):
            Code:
            forvalues crtry=1/138 {
             mat def mat`crtry'=J(50,1,0)
             forvalues startyear=1/50 {
                mkspline t1 `startyear' t2=t if countryno==`crtry'
                qui regress lngdp t1 t2 if countryno==`crtry'
                mat def mat`crtry'[`startyear',1]=e(F)
                drop t1 t2
             }
            }

            Comment

            Working...
            X