Announcement

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

  • How to use -frame- to do pair t test?

    Code:
    frame copy default pre_panela
    frame pre_panela {
          keep if inrange(date, mdy(10, 18, 2005) - 27, mdy(10, 18, 2005)) //  four weeks before October 19, 2005
          foreach v of varlist addition deletion total{
          egen pre`v' = total(`v'), by(id)
          }
          collapse preaddition predeletion pretotal, by(id)
    }      
    
    frame copy default post_panela
    frame post_panela {
          keep if inrange(date, mdy(11, 1, 2005), mdy(11, 1, 2005) + 27) // four weeks after October 31, 2005
          foreach v of varlist addition deletion total{
          egen post`v' = total(`v'), by(id)
          }
          collapse postaddition postdeletion posttotal, by(id)
    }
    My next step is to do the
    Code:
    ttest preaddition == postaddition
    ttest predeletion == postdeletion
    ttest pretotal == posttotal
    I don't know how to do this.

  • #2
    And if you have ideas about improving my current codes, just tell me. I appreciate! The current data frame name is default. I didn't change. I just create 2 new data frame:
    pre_panela and
    post_panela

    Comment


    • #3
      I don't believe you can run commands with variables from different frames. Either temporarily save the data from one frame and merge it to the other.
      Code:
      tempfile postdata
      frame post_panela: save `postdata'
      frame pre_panela: merge 1:1 id using `postdata'
      Then perform your tests in pre_panela. An easier way might have been creating an indicator variable for pre/post, then egen by id/indicator, collapse by id/indicator, then reshape by i(id) j(indicator).

      Comment


      • #4
        Thank you Daniel. I think my question becomes can we run commands with variables from different frames? I love frame from Stata 16. I personally think it's much better than preserve and restore.

        Comment


        • #5
          Actually, you may be able to link variables between frames. I haven't tried it, but look into frlink and the associated commands frget and frval. Should be in the help for frames.

          Comment


          • #6
            Yes, frlink will be helpful. But to use frlink, I have to save the data for one frame. Do I use both -tempfile postdata- and -save `postdata'- ?

            Comment


            • #7
              Going completely by the help file for frval, try this:
              Code:
              frame change pre_panela
              frlink 1:1 id, frame(post_panela) generate(linkid)
              ttest preaddition == frval(linkid, postaddition)

              Comment


              • #8
                I also did it by my codes. And save temp file is not required.

                My codes are:
                Code:
                frame change pre_panela
                frlink 1:1 id, frame(post_panela)
                frget postaddition postdeletion posttotal, from(post_panela)
                ttest preaddition== postaddition
                It works. But after frlink, I got a new variable in my pre_panela frame: post_panela. I don't know what this variable is.
                Last edited by Yao Zhao; 11 Mar 2020, 14:12.

                Comment


                • #9
                  It's the link variable generated by default. help frlink:
                  linkvar₁ is the name to be given to the new variable that frlink creates. The variable is added to the dataset in frame
                  1. The variable contains all the information needed to link the frames.
                  Also, frget copies variables from another frame to the current frame. If I'm not mistaken, frval allows you to call the variable from another frame without needing to copy the variable over.

                  Comment

                  Working...
                  X