Announcement

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

  • Save locals in a table

    Hi! I'm testing that the distribution of two variables is the same across groups using - ksmirnov -. Since I have many groups, I set up a local that counts how many comparisons give a positive outcome (equal distributions). Here is an example of what I do:

    Code:
    sysuse auto2.dta, clear
    
    * Duplicate obs to create a panel
    gen dup=2
    expand dup
    gen time=1
    bysort make: replace time=time+1 if _n==2
    
    * Create comparison groups
    tab rep78, nolabel
    
    gen rep78_1=(rep78==1) if rep78==1 | rep78==2 // Compare group1 with group2
    gen rep78_2=(rep78==1) if rep78==1 | rep78==3 // Compare group1 with group3
    gen rep78_3=(rep78==1) if rep78==1 | rep78==4 // and so on. 
    gen rep78_4=(rep78==1) if rep78==1 | rep78==5
    gen rep78_5=(rep78==2) if rep78==2 | rep78==3
    gen rep78_6=(rep78==2) if rep78==2 | rep78==4
    gen rep78_7=(rep78==2) if rep78==2 | rep78==5
    gen rep78_8=(rep78==3) if rep78==3 | rep78==4
    gen rep78_9=(rep78==3) if rep78==3 | rep78==5
    gen rep78_10=(rep78==4) if rep78==4 | rep78==5
    
    
    * Perform pairwise comparisons of distribution of mpg, for each time period
    forvalues t=1/2 {
    local count=0
    local pos_count=0
    foreach i of numlist 1/10 {
    ksmirnov mpg if time==`t', by(rep78_`i')
    local count=`count'+1
    if `r(p)'>0.05 {
    local pos_count=`pos_count'+1
    }
    }
    dis `count'
    dis `pos_count'
    }
    where count is the number of pairwise comparisons performed, and pos_count is the number of positive outcomes (equal distribution).

    I also have different specifications I want to test (e.g. besides mpg, I want to perform the same test on price) and I would like to report the results in a table (that I want to later export to latex) that has the variables mpg and price on the rows, and the macros pos_count and count on the columns (columns repeated for each year).

    Any help on how to do it would be appreciated.

    Thanks!


  • #2
    Rosella, your example does not run.
    Here is how you can save locals into a matrix and output to a TeX table (requires outtable by Kit Baum).
    Code:
    set more off
    sysuse auto, clear
    matrix R=J(40,2,.)
    forval i=1/40 {
      quietly count if mpg<`i' & foreign
      local n=r(N)
      matrix R[`i',1]=`n'
      quietly count if mpg<`i' & !foreign
      local n=r(N)
      matrix R[`i',2]=`n'  
    }
    
    matrix list R
    
    outtable using "C:\temp\table", replace mat(R)
    Help for outtable contains more examples.

    Best, Sergiy Radyakin

    Comment


    • #3
      Strange, because it runs in Stata 13 for me... Anyway, I'll try outtable.

      Thanks Sergiy!

      Comment


      • #4
        I can confirm that Rossella's code runs in Stata 14 without modifications.

        Comment

        Working...
        X