Announcement

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

  • using Stata frame to store coefficients in a loop

    Dear Statausers,

    I would like to know if it is possible to use the new Stata frame command to store the coefficients of a regression within a loop.

    Any help or guidance will be much appreciated.

    All the very best,

    JP


  • #2
    Here's an example:

    Code:
    frame change default
    frame drop output
    frame create output str20(varname) OR lb ub pvalue
    
    foreach var of varlist var* { // regression loop, modify as needed
    dis "`var'"
    quietly logistic control `var'
    local or = r(table)[1,1]
    local lb = r(table)[5,1]
    local ub = r(table)[6,1]
    local p = r(table)[4,1]
    
    frames post output ("`var'") (`or') (`lb') (`ub') (`p')
    
    }

    Comment


    • #3
      Welcome to Statalist.

      You will find that the output of help frame post will describe a capability that mimics the capabilities of the older estpost command.
      Code:
      sysuse auto, clear
      frame create coeffs foreign b_length b_weight b_cons
      forvalue f=0/1 {
          regress price length weight if foreign==`f'
          frame post coeffs (`f') (_b[length]) (_b[weight]) (_b[_cons])
      }
      frame change coeffs
      list, clean
      Code:
      . list, clean
      
              foreign    b_length   b_weight      b_cons  
        1.          0   -120.5376    6.19526    9163.626  
        2.          1    14.76399   4.937235   -7537.909

      Comment


      • #4
        Many thanks both William and Daniel.

        This is useful.

        I was wondering if there was a more direct way, but I guess frame does accept a matrix, so I need to find a way to loop through the elements.

        The estpost suggestion is good as it probably allows greater flexibility.

        All the very best,

        JP

        Comment


        • #5
          Your post #4 leads me to suggest the estimates commands for saving the complete set of estimates results from a model, if that is what you need. Daniel and I both assumed you wanted them in stored in a dataset because of your reference to frame, but if what you need are the results, not necessarily stored as a Stata dataset, then see
          Code:
          help estimates
          In post #3 where I wrote estpost I should have written postfile, the underlying command which frame post mimics.

          Comment


          • #6
            William Lisowski, am I right in believing that frame post will not accept e-class matrices, such as in

            Code:
            frame post framename (e(matname))
            ?

            [edit] of course it should be

            Code:
            "`e(matname)'"
            but I still can't understand if this would be possible. Does frame post expects one variable per (exp) or is able to understand when (exp) contains an array of results?
            Last edited by Matteo Pinna Pintor; 13 Jun 2024, 04:01.
            I'm using Stata/MP 17

            Comment


            • #7
              I'm not fluent in frame, but my understanding is that each (exp) should be one expression that evaluates to a single value -- not an array.
              exp may be an expression -- not necessarily a variable.
              Actually, I suppose that any variable should be subscripted -- else you'll get the value from observation 1.

              It's just like -post-. See help post.

              HTH
              --David

              Comment


              • #8
                William Lisowski, unfortunately, is no longer active on Statalist. This is one of the hazards of addressing a question to a specific forum member. It is best to do that only if you are continuing an ongoing dialog begun earlier in the thread.

                As for your question, no, you cannot use a matrix in -frame post- (nor in -post-ing to a tempfile). The expressions used in that command must evaluate as scalars.

                If you want to put the contents of e(matname) into that frame, you could do that with the -svmat- command instead, like this:
                Code:
                clear*
                sysuse auto
                
                regress price mpg headroom
                matrix V = e(V)
                
                frame create new_frame
                frame new_frame: svmat V
                Added: Crossed with #7.

                Comment

                Working...
                X