Announcement

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

  • Nonlinear Least Squares Estimating multiple β{b} and ρ{rho} using nl command

    First things first. This is my first post on the forum. So hello every one! I could use some help with stata. I'm a novice user and I have some questions. I'm a postgraduate student on economics and what I want to do is the following.
    I'm studying Gibrat's Law Chesher's(1979) approach and for a data set of 1.644 firms I want to estimate if the Law holds using Νon-linear regression procedure by Marquardt (1963) to obtain (asymptotic) standard errors for β{b} and ρ{rho}.The law is valid for β{b}=1 and ρ{rho}=0. I use the nl command to do so. The form of the command I used is :

    Code:
     nl (ln_s14 = ({b}+{rho})*(ln_s13)+({-b}*{rho})*(ln_s12)), variables(ln_s13 ln_s12)
    where ln_s14,ln_s13,ln_s12 stands for sales in logarithm for the given time

    My problem is that the command gives me output only for one pair of {b} and {rho} and what i need is 1.644 pairs.
    How can I do this?
    I use stata v14.0
    Last edited by George Miller; 26 Jan 2017, 07:12.

  • #2
    first, the update to 14.2 is free - do it

    second, your question is not completely clear to me: do you want a separate regression for each of your 1,644 firms? if yes, you want a loop; see:
    Code:
    h foreach
    and
    h levelsof
    -levelsof- may or may not be useful here - depends on your data structure and you don't tell us anything about that; if you want to show some data please follow the FAQ and install (using ssc install dataex) the dataex command and use it with CODE blocks (again, see the FAQ)

    Comment


    • #3
      You need to loop over your firms. See http://www.stata.com/support/faqs/da...ach/index.html for some technique.

      P.S, did

      Code:
      {-b}
      really work?

      Comment


      • #4
        @Rich Goldstein
        Thank you for your reply
        Definitely a seperate regretiion of those firms. As for the data stracture
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float ln_s12
         10.02857
         5.908061
          5.70538
          6.62952
        10.302718
        10.315928
         8.959732
         7.033526
         6.456756
         5.508992
        10.024347
         8.855943
          6.55944
         5.478483
          9.32524
         6.224982
           5.4458
        10.555714
         7.264856
         7.290013
          7.38959
         9.607975
         8.113018
         6.155992
         5.799302
          5.83558
         5.836352
         8.321043
         8.530309
         6.578663
         7.059475
         5.605641
         6.971338
         7.878705
         5.204095
         5.411908
         8.929377
          8.03625
         8.555994
         5.187248
        10.399433
         8.893559
         8.461454
         7.307406
        10.005816
         5.428608
           9.2367
         6.948266
         8.718111
         6.718151
         9.641473
         7.640512
         9.578173
         9.796053
         7.666898
          6.75643
        8.1530285
         4.896253
         6.050833
         5.749599
         6.078146
         8.361631
         6.135565
         5.512299
         7.392225
         6.999604
         6.526322
         6.477747
         8.473249
         9.916848
         7.441507
         6.464341
         7.208895
         9.834056
         9.798836
         7.416975
          7.97602
        10.742975
         9.190015
          7.80828
         7.632401
         7.705888
         7.834815
        9.4423275
         2.635771
         7.645644
         7.795424
         8.087346
         4.022744
        2.0967982
        4.4470463
         7.624253
         6.033401
         5.713439
         8.551958
         9.989598
         10.31339
         5.065632
         6.128706
         6.404954
        end
        Last edited by George Miller; 26 Jan 2017, 07:53.

        Comment


        • #5
          @Nick Cox
          Thank you for your reply. I'll work on that
          I think it did. Because of the results and below ln_s14 box i get Coeff
          Code:
           /b |   1.013519 &  /rho |  -2.02e-06  & /-b |          0
          Last edited by George Miller; 26 Jan 2017, 07:09.

          Comment


          • #6
            what you show in #4 cannot be your "real" data structure as there is no identifier for firms; please clarify

            Comment


            • #7
              @Rich Goldstein

              I have three string columns. One with the name of the firms,another one for the country origin of the firm , and another one for the sector the firms belong to.

              I also use seven more columns for integers. One is named ID and it just counts from 1 to 1644.

              I use 3 more columns indicating the Sales for the years 2012, 2013 and 2014.
              I also use 3 columns for normalized Sales data and 3 columns with log(Sales)

              I have this function
              Code:
               z_{t,i} = (\beta + \rho)z_{t-1,i} +(-\beta\rho)z_{t-2,i} + v_{t,i}
              what i want to is to estimate β&ρ for those firms.

              A simple regression doesn't help me much because it calculates only one coeff. Am I right?

              The theory proposes non-linear regression procedure by Marquardt (1963) to obtain (asymptotic) standard errors for β&ρ but while I use the nl command as I showed earlier in #1 I get only one pair of β&ρ

              I tried with the foreach command but it wont work as I get this invalid numlist has too many elements.
              (or for lower instances e.g 100 I get if ID == 1 not allowed)

              For levelsof command i get this if ID == l not allowed

              I'm not sure if I cleared things up but if there is something I can do please let me know.

              @Nick Cox
              In a closer look I noticed this
              Parameter -b taken as constant term in model & ANOVA table
              Last edited by George Miller; 26 Jan 2017, 11:01.

              Comment


              • #8
                In Stata columns in the dataset are always called variables.

                I can't diagnose what you did wrong with foreach or levelsof because you didn't show the commands you tried. My guess is that you should be using forvalues in any case.

                As Rich pointed out, we still lack an explicit data example, but what you give is helpful.

                Here's a sketch. As I can't test it on your data, it's not guaranteed.

                Code:
                
                gen beta = .
                gen rho = .
                
                quietly forval i = 1/1644 {
                    capture nl (ln_s14 = ({b}+{rho})*(ln_s13)-({b}*{rho})*(ln_s12)) if ID == `i', variables(ln_s13 ln_s12)
                    if _rc == 0 {
                        mat b = e(b)
                        replace beta = b[1,1] if ID == `i'
                        replace rho = b[1,2] if ID == `i'
                    }
                }

                Comment


                • #9
                  @Nick Cox
                  I appriciate your immediate responce and thank you for your code.
                  It worked as far it concerns the loop but with no data at all wich troubles me (only dots).
                  For the explicit data example. Is there something I write on the console? I use an excel file and my code below.

                  Code:
                  clear
                  clear all
                  
                  *import final data
                  import excel "Z:\home\\stata14\1.xls", sheet("Sheet1") firstrow
                  *Ln Sales
                  gen ln_s12=ln(Sales2012)
                  gen ln_s13=ln(Sales2013)
                  gen ln_s14=ln(Sales2014)
                  
                  *Create Normalized Data
                  egen float zsales2012 = std(Sales2012), mean(0) std(1)
                  egen float zsales2013 = std(Sales2013), mean(0) std(1)
                  egen float zsales2014 = std(Sales2014), mean(0) std(1)
                  
                  * nick's code
                  gen beta = .
                  gen rho = .
                  
                  quietly forval i = 1/1644 {
                      capture nl (ln_s14 = ({b}+{rho})*(ln_s13)-({b}*{rho})*(ln_s12)) if ID == `i', variables(ln_s13 ln_s12)
                      if _rc == 0 {
                          mat b = e(b)
                          replace beta = b[1,1] if ID == `i'
                          replace rho = b[1,2] if ID == `i'
                      }
                  }
                  ]

                  Comment


                  • #10
                    So, the code works then!

                    How many observations do you have per firm? I've found that nl can go very astray unless you work out and specify good starting values for its iterations. Small sample sizes wouldn't help. Perhaps a fit to the entire dataset would give you an idea, or something may be said on theoretical grounds.

                    Friendly digression: I don't know the context here and a reference like Chesher (1979) implies that we are all economists working in your subfield! Not so.

                    In fact only about 1 in the 10 most frequent posters (3 out of 20) here are economists, which doesn't match the distribution of questions....

                    See http://www.statalist.org/forums/help#references for advice on this point.

                    Comment


                    • #11
                      Observations per firm to be honest I'm not sure.I use Sales variables for the 2012-2014 period.
                      If it's not the nl command perhaps something wrong with the function.

                      Thank you for the friendly digression.Let me respond in the same friendly way. I've been looking for an answer for my problem for a while . And that is why I decided to register to this forum. I used all of this information only as I thought it would be helpfull. I understand that people on this forum doesn't work only on economics.
                      I asked for your help because of your knowledge on this software. If there is nothing to be done to understand my mistake thats ok. But if there is something I can do then I would like to give a shot.

                      Comment


                      • #12
                        To help diagnose the problem better, I suggest changing -capture- to -capture noisily-. That won't fix your problem, but you will see the output of your individual -nl-'s; it looks like they are not actually running, or are producing no results. Perhaps you are encountering the kind of -nl- difficulties Nick pointed out in #10. Or, it may be that your data structure is not suitable to what you are doing here. Do you have multiple observations for each firm, or do you just have one? If you have only one observation per firm, you will not be able to estimate two parameters in a regression using only one observation. It's mathematically impossible. By looking at the -nl- output from each iteration, it will become clear what is happening.

                        If you show a small representative sample of your data (one that includes a few firms worth of data, and all of the relevant variables), and an excerpt of the output you are getting, people will be able to give you more concrete advice. Up to this point, though, we're all playing a guessing game.

                        Comment


                        • #13
                          We're trying to help, but as Clyde says we are still on the dark on your data, so no amount of goodwill can push this further if certain questions remain unanswered.

                          I am puzzled that you don't seem to understand the question How many observations do you have per firm?

                          Perhaps that is because you are not yet accustomed to Stata terminology. Observations in Stata are in spreadsheet terms rows in the dataset. How many observations in your dataset? How many per firm?

                          What function (perhaps meaning command) are you referring to? Again, we are grumpy curmudgeons and expect Stata terminology in a Stata forum.

                          EDIT: Here is something I should have spotted earlier! Your sales for 2012, 2013 and 2014 are three different variables. In that case, as Clyde was guessing, you probably only have one observation per firm and each fit is futile. Even if you reshape your data, getting two parameters out of three observations would be a real stretch to put it mildly.
                          Last edited by Nick Cox; 26 Jan 2017, 13:35.

                          Comment


                          • #14
                            Thank you both Clyde and Nick.
                            I have one observation per firm. When i mentioned the function I was refering to a mathematical function and not a stata command. For stata terminology I'll do my best. (grumpy curmudgeons/// I'm still laughing!!!)

                            I followed Clyde's advice to use
                            Code:
                            capture noisily
                            and the result was
                            Code:
                            cannot have fewer observations than parameters
                            (obs = 1)
                            Finally a pic of my excel
                            Click image for larger version

Name:	1.png
Views:	1
Size:	193.5 KB
ID:	1371799


                            Last edited by George Miller; 26 Jan 2017, 13:55.

                            Comment


                            • #15
                              Screenshots are recommended against explicitly in http://www.statalist.org/forums/help#stata -- which explains how best to show data examples. But this one I can read well enough.

                              That's clear then, You just can't do this on individual firms. You have one data point in your variable space and fitting a surface just won't play. The best you can do is pool by sector or country or something else of interest.

                              I have to say, candidly but without ranco[u]r, that if you had first followed the advice in the section just cited above and shown us your data, we would have spotted this a lot faster.

                              Comment

                              Working...
                              X