Announcement

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

  • Entering results as rows one by one into a frame

    I’m running many statistical models (in Mplus) and decided to do so from Stata (with -runmplus-). I’m struggling with replicating what I would do in R: collecting a selected part of the results into a new data frame.

    For each of the many models, I want to collect `r(WaldTest_P)' and put its value along with an identifier into a frame.

    Code:
    // 1. and 2. are solved:
    // 1. I create an empty frame in Stata, called "Wald_tests"
    frame create Wald_tests
    
    // 2. I repeatedly run specific models, each of which gives me a value for `r(WaldTest_P)'
    // [code hidden]
    
    // --- Then I'm stuck. I would like to do this: ---
    
    // 3. Add a new row to the frame Wald_tests, one column contains the value for `r(WaldTest_P)',
    //    another contains an identifyer for that specific value, e.g. with "`Wald_mod`i'_item`j''"
    
    // 4. Do 2 and 3 for each of the many models/tests
    
    // 5. Then export the frame as a Stata data file, like this:
    frame Wald_tests: save "Wald_tests.dta"
    I can't use Stata's -table- or similar commands, since I want to manipulate the resulting data frame with R and R Markdown.

  • #2
    Your difficulty begins with your -frame create Wald_tests- command. That's fine for creating an empty frame, but an empty frame is not suitable for your stated purposes. You want to sequentially post values of r(WaldTest_P) into the frame, so you have to start with a frame that is prepared to receive those. You said you also associate those with some identifier. You don't say what that identifier is, nor where it comes from in your code. So I'll just assume it is in some local macro called identifier. The following is a blend of pseudocode in italics and Stata code in normal typeface.

    Code:
    // BEGIN BY CREATING AN APPROPRIATE FRAME FOR POSTING RESULTS
    frame create wald_test_results str32(identifier) float(wald_test_p)
    
    // HERE WE NEED AN ITERATIVE STRUCTURE THAT SEQUENTIALLY RUNS THE WALD TESTS
    // FILL IN THE DETAILS
    foreach whatever.....   {
        ...
        local identifier something
        ...
        command that creates the wald tests
        frame post wald_test_results (`"`identifier'"') (`r(WaldTest_P)')
        ...
    }
    
    //  THE FRAME NOW CONTAINS ALL THE RESULTS
    frame change wald_test_results
    perhaps other calculations on the results, formatting, etc.

    Comment


    • #3
      does
      Code:
      help frame_post
      help?

      Comment


      • #4
        Thanks, Bjarte and Clyde. Indeed, frame post was part of the solution.
        The code provided by Clyde Schechter helped med with a solution, something similar to:

        Code:
        frame create res_wald str12(item_id) float(wald_p)
        [...]
        frame post res_wald ("Mod`i'_item_`j'") (`r(WaldTest_P)')
        It can be tricky to go from one language to another, and Clyde spotted my error. (In R, one can declare columns and rows when generating a data frame -- it will speed up things -- but one does not have to in the first step. But of course, then one needs to do so in the next step, so no excuse -- I made an elementary error.)
        Clyde Schechter: I assume there is a typo in your code? Or is

        Code:
        (`"`identifier'"')
        correct?

        Comment


        • #5
          [double post]
          Last edited by Christopher Bratt; 18 Oct 2022, 13:12.

          Comment


          • #6
            Originally posted by Christopher Bratt View Post
            I assume there is a typo in your code? Or is

            Code:
            (`"`identifier'"')
            correct?
            Well, as I indicated in #2, I didn't know where the identifier was coming from, and assumed it was being stored in a local macro called identifier. On that assumption, it was correct. Local macro identifier was intended to be a stand-in for whatever the real identifier was, for the purposes of illustrating the approach. Looking at your subsequent implementation in #5, you have indices i and j that define the identifier as the string expression "Mod_`i'_item_`j'", and you appropriately substituted it for `identifier' in that place. Had I known about i and j, I would have written that line the way you did.

            Comment


            • #7
              Thanks for the clarification.
              (The first post said … e.g. with "`Wald_mod`i'_item`j''"
              but that was easy to overlook, given how I mixed it with ordinary text.)

              Comment

              Working...
              X