Announcement

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

  • why always (invalid name

    Here is my code as shown below, when I run it, it always report error "( invalid name". Somebody please help me with this.

    tempname myresults
    postfile `myresults' threshold intercept gradient se using myresults.dta

    forval x=1(1)65{
    capture noisily xi:regress lnSale lnYearlyIndOut fyear if mi==`x',robust
    post `myresults' (`x') (`=_b[_cons]') (`=_b[lnYearlyIndOut]') (`=_se[lnYearlyIndOut]')
    }


  • #2
    Clean up your code.
    Code:
    tempname myresults
    postfile `myresults' byte threshold double intercept double gradient double se using myresults
    
    forvalues x = 1/65 {
        quietly count if mi == `x'
         if r(N) > 1 { // Ought to be >2 if you want to assure both regression coefficients, and ought to be >3 if you want to assure their standard errors aren't missing-valued
            regress lnSale c.(lnYearlyIndOut fyear) if mi == `x'
            post `myresults' (`x') (`=_b[_cons]') (`=_b[lnYearlyIndOut]') (`=_se[lnYearlyIndOut]')
         }
    }

    Comment


    • #3
      Originally posted by Joseph Coveney View Post
      Clean up your code.
      Code:
      tempname myresults
      postfile `myresults' byte threshold double intercept double gradient double se using myresults
      
      forvalues x = 1/65 {
      quietly count if mi == `x'
      if r(N) > 1 { // Ought to be >2 if you want to assure both regression coefficients, and ought to be >3 if you want to assure their standard errors aren't missing-valued
      regress lnSale c.(lnYearlyIndOut fyear) if mi == `x'
      post `myresults' (`x') (`=_b[_cons]') (`=_b[lnYearlyIndOut]') (`=_se[lnYearlyIndOut]')
      }
      }
      Thank you Joseph! I tried your code, but it still doesn't work. Here is the error reported :
      ( invalid name
      program error: code follows on the same line as close brace
      r(198);

      It doesn't make sense to me, the braces are obviously closed.

      Comment


      • #4
        First, you don't need to macro-evaluate the regression output; you can just put them in as-is. See below.

        .ÿ
        .ÿversionÿ15.1

        .ÿ
        .ÿclearÿ*

        .ÿ
        .ÿquietlyÿsysuseÿauto

        .ÿ
        .ÿtempnameÿfile_handle

        .ÿtempfileÿtmpfil0

        .ÿ
        .ÿpostfileÿ`file_handle'ÿbyteÿregionÿdoubleÿslopeÿdoubleÿslope_seÿusingÿ`tmpfil0'

        .ÿ
        .ÿforvaluesÿregionÿ=ÿ0/1ÿ{
        ÿÿ2.ÿÿÿÿÿÿÿÿÿquietlyÿregressÿgear_ratioÿc.mpgÿifÿforeignÿ==ÿ`region'
        ÿÿ3.ÿÿÿÿÿÿÿÿÿpostÿ`file_handle'ÿ(`region')ÿ(_b[mpg])ÿ(_se[mpg])
        ÿÿ4.ÿ}

        .ÿpostcloseÿ`file_handle'

        .ÿ
        .ÿexit

        endÿofÿdo-file


        .


        Second, for the close-brace problem, try
        Code:
        set trace on
        to see where the error arises.

        Last, I forgot to mention this, but there's another thing that you might need to trap. Insert
        Code:
        drop if mi(lnSale, lnYearlyIndOut, fyear)
        before the loop.

        Comment


        • #5
          Originally posted by Joseph Coveney View Post
          First, you don't need to macro-evaluate the regression output; you can just put them in as-is. See below.

          .ÿ
          .ÿversionÿ15.1

          .ÿ
          .ÿclearÿ*

          .ÿ
          .ÿquietlyÿsysuseÿauto

          .ÿ
          .ÿtempnameÿfile_handle

          .ÿtempfileÿtmpfil0

          .ÿ
          .ÿpostfileÿ`file_handle'ÿbyteÿregionÿdoubleÿslopeÿdoubleÿslope_seÿusingÿ`tmpfil0'

          .ÿ
          .ÿforvaluesÿregionÿ=ÿ0/1ÿ{
          ÿÿ2.ÿÿÿÿÿÿÿÿÿquietlyÿregressÿgear_ratioÿc.mpgÿifÿforeignÿ==ÿ`region'
          ÿÿ3.ÿÿÿÿÿÿÿÿÿpostÿ`file_handle'ÿ(`region')ÿ(_b[mpg])ÿ(_se[mpg])
          ÿÿ4.ÿ}

          .ÿpostcloseÿ`file_handle'

          .ÿ
          .ÿexit

          endÿofÿdo-file


          .


          Second, for the close-brace problem, try
          Code:
          set trace on
          to see where the error arises.

          Last, I forgot to mention this, but there's another thing that you might need to trap. Insert
          Code:
          drop if mi(lnSale, lnYearlyIndOut, fyear)
          before the loop.
          That works! Thanks Joseph!
          But another question, in the loop there are some regressions where the coefficient of lnYearlyIndOut is 0(omitted), so there is no variable to be stored. Thus Stata reported no observations
          no variables defined
          no variables defined
          invalid syntax

          I want the loop to continue and just skip the coefficients that does not exist, and save all those that exist. Could you help me with that?

          Comment


          • #6
            Originally posted by Johnny YANG View Post
            in the loop there are some regressions where the coefficient of lnYearlyIndOut is 0(omitted)
            As I illustrated earlier: trap the conditions when your operation cannot be performed, and whenever those conditions are detected, instruct the loop to continue (skip to the next iteration).

            I recommend avoiding the casual use of capture for such a task in production code.

            Comment


            • #7
              Originally posted by Joseph Coveney View Post
              As I illustrated earlier: trap the conditions when your operation cannot be performed, and whenever those conditions are detected, instruct the loop to continue (skip to the next iteration).

              I recommend avoiding the casual use of capture for such a task in production code.
              Nice! Thank you!

              Comment


              • #8
                There might be some commitment to writing your own loops for some other reason, but it may be worth flagging that there are other approaches to repeating regressions over subsets of observations.

                For example with rangestat (SSC) by Robert Picard and friends, the example in #4 from Joseph Coveney can be written like this:

                Code:
                . sysuse auto, clear 
                (1978 Automobile Data)
                
                . rangestat (reg) gear_ratio mpg, interval(foreign 0 0) 
                
                . tabdisp foreign, c(reg_nobs    reg_r2   b_mpg se_mpg)   
                
                --------------------------------------------------------------------------------------
                 Car type |          reg_nobs             reg_r2              b_mpg             se_mpg
                ----------+---------------------------------------------------------------------------
                 Domestic |                52           .3202138          .04007944          .00825853
                  Foreign |                22          .21641755          .02089239          .00888933
                --------------------------------------------------------------------------------------
                rangestat views choice of minimum acceptable sample size differently from some other commands. It won't fall over with too few observations to do what is asked (even zero), but you can always review results later and ignore those based on what you think is too few for comfort or to appease your peers.

                Comment


                • #9
                  Originally posted by Nick Cox View Post
                  There might be some commitment to writing your own loops for some other reason, but it may be worth flagging that there are other approaches to repeating regressions over subsets of observations.

                  For example with rangestat (SSC) by Robert Picard and friends, the example in #4 from Joseph Coveney can be written like this:

                  Code:
                  . sysuse auto, clear
                  (1978 Automobile Data)
                  
                  . rangestat (reg) gear_ratio mpg, interval(foreign 0 0)
                  
                  . tabdisp foreign, c(reg_nobs reg_r2 b_mpg se_mpg)
                  
                  --------------------------------------------------------------------------------------
                  Car type | reg_nobs reg_r2 b_mpg se_mpg
                  ----------+---------------------------------------------------------------------------
                  Domestic | 52 .3202138 .04007944 .00825853
                  Foreign | 22 .21641755 .02089239 .00888933
                  --------------------------------------------------------------------------------------
                  rangestat views choice of minimum acceptable sample size differently from some other commands. It won't fall over with too few observations to do what is asked (even zero), but you can always review results later and ignore those based on what you think is too few for comfort or to appease your peers.
                  Thank you Nick! This is a very good way! Will note it!

                  Comment


                  • #10
                    Originally posted by Nick Cox View Post
                    There might be some commitment to writing your own loops for some other reason, but it may be worth flagging that there are other approaches to repeating regressions over subsets of observations.

                    For example with rangestat (SSC) by Robert Picard and friends, the example in #4 from Joseph Coveney can be written like this:

                    Code:
                    . sysuse auto, clear
                    (1978 Automobile Data)
                    
                    . rangestat (reg) gear_ratio mpg, interval(foreign 0 0)
                    
                    . tabdisp foreign, c(reg_nobs reg_r2 b_mpg se_mpg)
                    
                    --------------------------------------------------------------------------------------
                    Car type | reg_nobs reg_r2 b_mpg se_mpg
                    ----------+---------------------------------------------------------------------------
                    Domestic | 52 .3202138 .04007944 .00825853
                    Foreign | 22 .21641755 .02089239 .00888933
                    --------------------------------------------------------------------------------------
                    rangestat views choice of minimum acceptable sample size differently from some other commands. It won't fall over with too few observations to do what is asked (even zero), but you can always review results later and ignore those based on what you think is too few for comfort or to appease your peers.
                    Hi Nick, I tried the rangestat today, it works really smooth, but the problem is that, I ran the regression by hand, the results are different from the results coming from rangestat

                    Comment


                    • #11
                      Concrete detail is needed: your data, your code, your results.

                      Comment


                      • #12
                        Originally posted by Nick Cox View Post
                        Concrete detail is needed: your data, your code, your results.
                        egen sic_id=group(sic)
                        tab sic_id
                        rangestat (reg) sale nopio, interval(sic_id 1 414)
                        tabdisp sic_id, c(reg_nobs b_nopio se_nopio)

                        my data is the fundamental annual data from compustat, 2007-2018.
                        the results from rangestat is as follows.

                        group(sic
                        ) reg_nobs b_nopio se_nopio

                        1 408992 23.583062 .05666472
                        2 408790 23.582863 .05667833
                        3 408619 23.582729 .05668994
                        4 408455 23.582569 .05670099
                        5 408419 23.582531 .05670342
                        6 400388 24.219794 .05772926
                        7 385781 24.304457 .05884326
                        8 385318 24.304009 .05887762
                        9 384057 24.303508 .05897171
                        10 383432 24.303918 .05901932
                        11 383261 24.303913 .05903223
                        12 366745 24.310357 .06124795
                        13 365266 24.364617 .06140111
                        14 364510 24.364858 .06146397
                        15 363376 24.382495 .0615424


                        But if I run the regression when sic_id is 1, the result is as follows,
                        sale Coef. Std. Err. t P>t [95% Conf. Interval]

                        nopio -19.72358 4.948205 -3.99 0.000 -29.43763 -10.00954
                        _cons 764.5555 70.53242 10.84 0.000 626.0901 903.0209

                        totally different.

                        Comment


                        • #13
                          Indeed. Your rangestat regressions all pool many identifiers. See how the sample size is always around 400000. You have misread the options for rangestat.

                          Comment


                          • #14
                            Originally posted by Nick Cox View Post
                            Indeed. Your rangestat regressions all pool many identifiers. See how the sample size is always around 400000. You have misread the options for rangestat.
                            so the rangestat is only applicable for small sample, right?

                            Comment


                            • #15
                              The results for the first identifier are for identifiers 2 up, those for the second 3 up, and so on; not what you want at all. See again #8 and the help. I guess that you want interval(sic_id 0 0).

                              Comment

                              Working...
                              X