Announcement

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

  • Exporting R-squared values from a loop

    Hello,
    I am running a forvalues loop regression from years 1990 to 2010 (so 21 regressions in total at the same time)

    Looks like this:

    forvalues i=1990(1)2010{
    regress mkval ni if fyear==`i' & high_tech==1
    }

    Basically I am running these 21 regressions on two groups (high_tech sample which is equal to 1 in this case and non high-tech sample where I would write high_tech==0).
    My goal is to store the adjusted R-squared values of all these 21 regressions and then export them to an Excel file.

    I tried the following command:

    forvalues i=1990(1)2010{
    regress mkval ni if fyear==`i' & high_tech==1
    estimates store result `i'
    }

    However, I get the error 1990 invalid name. I am aware that 1990 is the first year in my sample and that I am running a loop on years but I don't really understand how to resolve this problem.
    Once this is done, I would like to use the xml_tab command to export the R-squared values.

    Thank you in advance for any help
    Last edited by Dovydas Zalneriunas; 17 Jun 2017, 04:59.

  • #2
    There is no need for a loop here, type help statsby and follow the examples.

    Comment


    • #3
      Originally posted by Oded Mcdossi View Post
      There is no need for a loop here, type help statsby and follow the examples.
      I managed to get the output into a different STATA file. My next issue is how to aggregate the output in that exact same file. For example, I run the following code:

      Code:
      statsby e(r2_a), by(fyear) saving("output.dta"): regress mkval ni if high_tech==1, r
      statsby e(r2_a), by(fyear) saving("output.dta"): regress mkval ni if high_tech==0, r

      I am going to run at least 20 similar regressions, however, I want all the adjusted R-squared values to be stored in the same output.dta file, in different columns. Obviously, here I will get the error that output.dta already exists and if I put the option replace, the previously stored values will be replaced with the ones obtained most recently.

      What is the command here so that I do not need to create many different STATA files?

      Comment


      • #4
        Welcome to Statalist.

        Oded's advice is well stated. But since you've gotten this far with the looping approach, let me explain what the problem is with your estimates store command, so when you do need a loop you'll avoid the current problem.

        The first time through the loop, Stata will replace `i' with 1990 and will try to run the command
        Code:
        estimates store result 1990
        But help estimates store tells us that the command syntax is
        Code:
        estimates store   name [, nocopy]
        So I'm not sure what Stata was trying to do, but it looks like it thought you gave two names for the stored estimates - "result" and "1990". And in general, Stata does not allow names to start with a digit, as help naming conventions informs us.

        What you probably wanted was to name the stored estimates something like "result1990" but the space following "result" on your command made that not happen. I expect that if you change the command in your loop to
        Code:
        estimates store result`i'
        you will achieve that objective.
        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . forvalues f=0/1 {
          2. quietly regress mpg weight if foreign==`f'
          3. estimates store result`f'
          4. }
        
        . estimates dir
        
        -------------------------------------------------------
                name | command      depvar       npar  title 
        -------------+-----------------------------------------
             result0 | regress      mpg             2  
             result1 | regress      mpg             2  
        -------------------------------------------------------

        Comment


        • #5
          If you want to add the statsby results back to your data file all you have to do is to merge the two datasets. In the following example I create virtual dataset and append it to the master data. After the code finish to run the statsby data vanished and you get it in your master data. To get it work you need to run the code in one time and not line by line. Just highlight the lines and run it. One more thing that I found in your code is it seems that you run the same regression twice by high_tech. If high_tech has only two values then you can add it to your by() option in statsby.
          Code:
          sort fyear high_tech
          tempfile output
          statsby e(r2_a), by(fyear high_tech) saving(`output'): regress mkval ni, r
          merge m:1 fyear high_tech using `output', keep(1 3) nogen
          rename _stat_1 r2_a

          Comment

          Working...
          X