Announcement

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

  • Converting variables in matrix

    Dear Statalists,

    I have already created the following variables, in which I regressed Alphas and t statistics for each time section.

    Code:
    gen alpha = .
    levelsof id, local(levels)
    foreach x of local levels {
        regress mean_alpha lbhml lbmom lbmktrf lbsmb if id == `x'
        mat table = r(table)
        replace alpha = table[1,5] if id == `x'
    }
    
    gen ttest95 = .
    levelsof id, local(levels)
    foreach x of local levels{
    ttest alphatl == 0 if id == `x', level(95)
    replace ttest95 = r(t) if id ==`x' 
    }
    In the following I am trying to combine the variables to one matrix.
    Moreover is there any way to test for multiple significance levels (1, 5, 10) in one ttest.
    Unfortuantely I have not succeeded in finding any solution yet.

    Can anyone please help me?

    Thank you in advance!

  • #2
    I don't get what you are trying to do here.

    The first loop runs separate regressions for each value of id, and stores the 95% lower confidence limit of the estimated coefficient of lbhml in a new variable called alpha. So far, I'm with you.

    The rest of the code escapes me. Once again, you are doing things separately for each value of id--OK. But now you are trying to do a one-sample ttest of some variable alphat1 against a null value of 0. I don't see how this has any relationship to what is done in the first loop. Where does alphat1 come from? I suppose it's some pre-existing variable. Anyway, now you create a new variable storing the t-statistic, and you call it ttest95. OK, I get that.

    Then we get murky. Your t-statistic has nothing to do with the -level()- option in the -ttest- command. All that -level- does is tell Stata what multiplier to use on the standard error to compute confidence intervals. The t-statistic itself has no particular confidence level. There is a p-value associated with it, which you can grab as r(p) if you need it. But if you somehow take this -ttest- containing loop and embedded it inside a loop over different values of level (or the inverse of level at 1, 5, or 10) all you will do is repeat the identical calculations three times.

    Also you ask about a matrix, but there is no mention of matrices anywhere in the code you show. And if you are asking how to create one, I would ask you why you want to do that. In Stata, matrices are really not very helpful unless you are actually going to be doing matrix algebra on their contents. As a means of holding results, they are awkward at best. Sets of systematic results are best stored in data sets (which can be created using frames in version 16, or postfiles in earlier versions of Stata).

    Finally, I note that you are using the S-word. The American Statistical Association has recommended that the concept of statistical significance be abandoned. See https://www.tandfonline.com/doi/full...5.2019.1583913 for the "executive summary" and https://www.tandfonline.com/toc/utas20/73/sup1 for all 43 supporting articles. Or https://www.nature.com/articles/d41586-019-00857-9 for the tl;dr.

    Comment


    • #3
      Thank you for your detailed answer! I really appreciate it.

      Code:
      //Double sorting in 5 x 5 quintiles
      
      by calmt, sort: egen quantilelRsqu = xtile(lRsqu), n(5)
      by calmt quantilelRsqu, sort: egen quantileAl = xtile(alphatl), n(5)
      
      // Generate ID for each portfolio
      
      foreach x in quantilelRsqu{
          foreach x in quantileAl{
              generate id = string(quantilelRsqu) + string(quantileAl)
          }
          gen id2 = real(id)
          drop id
          rename id2 id
      }
      // Generate Mean
      by id calmt, sort: egen mean_alpha = mean(alphatl)
      
      //Regress Alphas over FFC Factors
      
      gen alpha = .
      levelsof id, local(levels)
      foreach x of local levels {
          regress mean_alpha lbhml lbmom lbmktrf lbsmb if id == `x'
          mat table = r(table)
          replace alpha = table[1,5] if id == `x'
      }
      //generate t statistics for each id
      
      gen ttest95 = .
      levelsof id, local(levels)
      foreach x of local levels{
      ttest alphatl == 0 if id == `x'
      replace ttest95 = r(t) if id ==`x'
      }
      In first place I created 5 x 5 quintiles, first sorted by R² and then within each quintile by alphatl (lagged alpha). Then I generated an id for each portfolio based on the quintiles. In the next step I calculated the means for each id and calender month (calmt). After that I regressed the mean alpha on the lagged FFC variables (lbhml lbmom lbmktrf lbsmb) for each id seperately. Then I tried to store the regressions intercept with the help of a matrix. In the following I generated t statistics for each id based on alphatl. We used alphatl, because if I used alpha I would get a missing value.

      In the end I am trying to create a table in stata, as you can see below, but with more quintiles.
      In each cell I want to store the regressions intercept and also their t statistics.
      Click image for larger version

Name:	nnn.PNG
Views:	1
Size:	5.1 KB
ID:	1519271

      Comment


      • #4
        I am trying to run loop within loop and get the p values in the matrix. I want to assign random treatment (post) to the non treated countries in the same year and run the regression for 3 different outcome variables for 50 times in a loop. And then get the value of p in the matrix. Can anyone please help me.

        I am doing the following. I am either getting the matrix table with missing p values (.) or getting conformability error. please help !!


        drop random
        drop post
        set more off
        set matsize 800
        set seed 6000
        unab y : wheat rice lentils
        local l : word count `y'
        matrix p = J(50,`l',.)
        matrix colnames p =`y'
        matlist p

        foreach i of numlist 1/50 {

        generate random=uniform()
        sort year random
        generate post=0
        replace post=1 in 1 in 1/29 // for year 2010
        replace post=1 in 296/337 // for year 2011
        replace post=1 in 578/619 // for year 2012

        foreach var of local y {
        quietly reg `var' post i.country_id i.year i.country#c.line_time_trend [aw=ypop], cluster( country_id )
        matrix p[1,`i++'] = 2*ttail(e(df_r), abs(_b[post]/_se[post]))
        }
        drop random
        drop post
        }
        matlist p

        Comment


        • #5
          Duplicates post. Asked and answered at https://www.statalist.org/forums/for...-output-matrix.

          Comment

          Working...
          X