Announcement

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

  • #16
    As established at length yesterday dj.y is not legal syntax.

    Comment


    • #17
      My apologies to Mr. Dapel if my posting at #14 above (about the mismatched parentheses) obscured my recommendation.

      The only way I know of to make progress on this problem is by the recommendation I made at #13 above. We must start with something small and simple and gradually increase the complexity until something fails, so we can focus on one problem at a time. We have seen that there originally were at least three syntax problems (syntax for exponentiation, mismatched parentheses, dj.y syntax) and there could be more w haven't noticed.

      Comment


      • #18
        Dear Dr Cox, Thanks for the emphasis. I will have to seek for an option. Dear William, You don't have to let that border you. I clearly understand your position.

        Comment


        • #19
          Zuhumnan,

          I believe you can use the function evaluator version of nl to obtain your estimates. Below I include some code to demonstrate.

          Code:
          cscript
          
          /* generate some data                             */
          set seed 123232651
          
          set obs 5
          
          gen int dj = _n
          local ex = 25
          expand `ex'
          sort dj
          
          gen t = _n
          tsset t
          
          scalar alpha = .5
          scalar gamma = 1.2
          scalar beta_1 = 2
          gen double x = rnormal()
          gen double e = rnormal()
          qui gen double y = .
          
          /* user's function
              nl(y=({alpha=inv_a}**dj))*dj.y +
                  {gamma=inv_g}*(1-{alpha=inv_a}**dj)/(1-{alpha=inv_a})+
                  ({beta_1= inv_b1})*(1-{alpha=inv_a}**dj)/(1-{alpha=inv_a})*x
          */
          local N = _N
          forvalues i=1/`N' {
              /* extract the current lag                    */
              local l = dj in `i'
              qui replace y = gamma*(1-alpha^dj)/(1-alpha)+       ///
                      beta_1*(1-alpha^dj)/(1-alpha)*x + e in `i'
              if `i' > `l' {
                  qui replace y = y + (alpha^dj)*L`l'.y in `i'
              }
          }
          
          tsline y
          
          program define nlts
              syntax varlist(min=3 max=3) [if], at(name)
              
              /* ignore if; requires more complicated code            */
              gettoken yhat varlist : varlist
              gettoken x dj : varlist
          
              tempname b alpha beta gamma
          
              /* extract estimates                        */
              foreach el in alpha beta gamma {
                  mat `b' = `at'[1,"`el'"]
                  scalar ``el'' = `b'[1,1]
              }
              local N = _N
              forvalues i=1/`N' {
                  local l = `dj' in `i'
                  qui replace `yhat' = `gamma'*(1-`alpha'^`dj')/(1-`alpha')+ ///
                          `beta'*(1-`alpha'^`dj')/(1-`alpha')*`x'    ///
                              in `i'
                  if `i' > `l' {
                      qui replace `yhat' = `yhat' + ///
                          (`alpha'^`dj')*L`l'.`yhat' in `i'
                  }
              }
          end
          
          scalar a = .1
          scalar g = .5
          scalar b = .5
          
          nl ts @ y x dj, parameters(alpha beta gamma) noconstant ///
              init(alpha `=a' beta `=b' gamma `=g')
          exit
          When I execute this code I get the following output

          Code:
          . scalar a = .1
          
          . scalar g = .5
          
          . scalar b = .5
          
          .
          . nl ts @ y x dj, parameters(alpha beta gamma) noconstant ///
          >         init(alpha `=a' beta `=b' gamma `=g')
          (obs = 125)
          
          Iteration 0:  residual SS =  486.6029
          Iteration 1:  residual SS =  192.2623
          Iteration 2:  residual SS =  157.9439
          Iteration 3:  residual SS =  156.5889
          Iteration 4:  residual SS =  156.5879
          Iteration 5:  residual SS =  156.5879
          Iteration 6:  residual SS =  156.5879
          
                Source |       SS       df       MS
          -------------+------------------------------         Number of obs =       125
                 Model |  1850.05189     3  616.683962         R-squared     =    0.9220
              Residual |  156.587923   122  1.28350757         Adj R-squared =    0.9200
          -------------+------------------------------         Root MSE      =   1.13292
                 Total |  2006.63981   125  16.0531185         Res. dev.     =  382.8976
          
          ------------------------------------------------------------------------------
                     y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
                /alpha |   .4820751    .038691    12.46   0.000     .4054825    .5586678
                 /beta |   1.984883   .1127831    17.60   0.000     1.761617    2.208148
                /gamma |   1.097183   .0969433    11.32   0.000     .9052741    1.289092
          ------------------------------------------------------------------------------
          I hope this helps.

          -Rich

          Comment


          • #20
            Dear Rich, Thanks a million for given your time; for providing such a detail and advanced response. I realised that instead of dj.y we should have a real number lagged, e.g. l5.y; l7.y and so on. Therefore the user's function for surveys that are 5 years apart should be
            Code:
              /* user's function
                  nl(y=({alpha=inv_a}**5))*l5.y +
                      {gamma=inv_g}*(1-{alpha=inv_a}**l5.y)/(1-{alpha=inv_a})+
                      ({beta_1= inv_b1})*(1-{alpha=inv_a}**5)/(1-{alpha=inv_a})*x
            So that
            Code:
            nl( y=({alpha=inv_a}^5 )*l5.y + {gamma= inv_g}*(1-{alpha=inv_a}^5)/(1-{alpha=inv_a}) + {beta_1= inv_b1}*(1-{alpha=inv_a }^5)/(1-{alpha=inv_a})*x) if inrange(year, 1980, 1985) & m>1    
             I'm exploring a stacked dataset where m==1 when dj==.
            . I got
            Code:
            Iteration 0:  residual SS =  19.61372
            Iteration 1:  residual SS =  5.013322
            Iteration 2:  residual SS =  5.011349
            Iteration 3:  residual SS =  5.011349
            
                  Source |       SS       df       MS
            -------------+------------------------------         Number of obs =       108
                   Model |  2.52775059     2  1.26387529         R-squared     =    0.3353
                Residual |  5.01134883   105  .047727132         Adj R-squared =    0.3226
            -------------+------------------------------         Root MSE      =  .2184654
                   Total |  7.53909942   107  .070458873         Res. dev.     =  -25.1153
            
            ------------------------------------------------------------------------------
                       y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
            -------------+----------------------------------------------------------------
                  /alpha |   .7426338   .0424689    17.49   0.000     .6584258    .8268418
                  /gamma |   2.801493   .4932631     5.68   0.000     1.823443    3.779543
                 /beta_1 |  -.0119158   .0053325    -2.23   0.028    -.0224891   -.0013424
            ------------------------------------------------------------------------------
              Parameter gamma taken as constant term in model & ANOVA table
            I was expecting the number of obs to 216 since we have 108 in each year. Another concern: I run a similar code but got no results instead
            Code:
            . nl(y=({alpha=.9948274 }^7)*l7.y + {gamma=2.260842}*(1-{alpha=.9948274}^7)/(1-{alpha=.9948274}) + {b
            > eta_1=.0062294}*(1-{alpha=.9948274}^7)/(1-{alpha=.9948274 })*x) if inrange(year, 1985, 1992) & m>1
            (obs = 540)
            
            starting values invalid or some RHS variables have missing values
            r(480);
            
            end of do-file
            
            r(480);
            . Please help!
            Thanks,
            Dapel

            Comment

            Working...
            X