Announcement

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

  • Bootstrapped Maximum likelihood estimation

    Dear Statalist users,

    I am trying to write a code in order to estimate a linear regression with boostrapped maximum likelihood estimator like this:
    Code:
    program lfols_lf
                 args lnf lnsigma
                 local y "$ML_y1"
                 replace `lnf' = ln(normalden(`y', `xb',exp(`lnsigma')))
    end
    
    program  lfols
        syntax varlist
        gettoken y rhs : varlist
        ml model lf lfols_lf (`y'=`rhs')
        ml max
    end
    
    bootstrap _b: lfols y x1 x2 x3
    I would like to know where the errors are.

    Many thanks in advance for your reply.

    Best ,
    Emna

  • #2
    This post probably would have gotten more notice in the general part of statalist.

    Here is my version of Emna's code:

    Code:
    program lfols_lf
            args lnf xb lnsigma
            quietly ///
            replace `lnf' = lnnormalden($ML_y1, `xb', exp(`lnsigma'))
    end
    
    program lfols, eclass
            if replay() {
                    ml display `0'
                    exit
            }
    
            syntax varlist(numeric)
    
            gettoken y rhs : varlist
            ml model lf lfols_lf (`y'=`rhs') /lnsigma, max
    
            ereturn local cmd lfols
    end    
    
    * try with a dataset everyone has
    sysuse auto
    
    * try model fit without -bootstrap- to make sure it works
    lfols mpg turn trunk displ
    
    * replay
    lfols, level(90)
    
    * try with bootstrap prefix
    bootstrap _b: lfols mpg turn trunk displ

    Comment


    • #3
      Jeff Pitblado (StataCorp),

      Thank you so much for your response, but I tried your code and I get error at the end. Stata does not recognize 'lfols' command. I use Stata16 version.

      Code:
       program lfols_lf
        1.
      .         args lnf xb lnsigma
        2.
      .         quietly ///
        3.
      .         replace `lnf' = lnnormalden($ML_y1, `xb', exp(`lnsigma'))
        4.
      . end
      
      .
      .
      .
      . program lfols, eclass
        1.
      .         if replay() {
        2.
      .                 ml display `0'
        3.
      .                 exit
        4.
      .         }
        5.
      .
      .
      .         syntax varlist(numeric)
        6.
      .
      .
      .         gettoken y rhs : varlist
        7.
      .         ml model lf lfols_lf (`y'=`rhs') /lnsigma, max
        8.
      .
      .
      .         ereturn local cmd lfols
        9.
      . end    
      
      .
      .
      .
      . * try with a dataset everyone has
      
      .
      . sysuse auto
      (1978 Automobile Data)
      
      .
      .
      .
      . * try model fit without -bootstrap- to make sure it works
      
      .
      . lfols mpg turn trunk displ
      
      / is not a valid command name
      r(199);
      
      .
      .
      .
      . * replay
      
      .
      . lfols, level(90)
      last estimates not found
      r(301);
      
      .
      .
      .
      . * try with bootstrap prefix
      
      .
      . bootstrap _b: lfols mpg turn trunk displ
      (running lfols on estimation sample)
      / is not a valid command name
      an error occurred when bootstrap executed lfols
      r(199);
      Last edited by Emna Trabelsi; 23 Aug 2020, 14:47.

      Comment


      • #4
        Jeffs code works for me (and that does not surprise me). Maybe you tried to run the code line by line instead of all in one go? Or maybe you tried to paste the code in the command window instead of the do-file editor?
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Originally posted by Maarten Buis View Post
          Jeffs code works for me (and that does not surprise me). Maybe you tried to run the code line by line instead of all in one go? Or maybe you tried to paste the code in the command window instead of the do-file editor?
          Dear Maarten,

          Thank you so much for your reply. Yes, now it works perfectly.


          Best,
          Emna

          Comment


          • #6
            Jeff Pitblado (StataCorp) and Maarten Buis,

            I want to repeat the code 48 times to create 48 regressions. Where is the error in the code below?

            Code:
            program lfols_lf
                    args lnf xb lnsigma
                    quietly replace `lnf' = lnnormalden($ML_y1, `xb', exp(`lnsigma'))
            end
            
            
            program lfols, eclass
                    if replay() {
                            ml display `0'
                            exit
                    }
            
                    syntax varlist(numeric)
                    
                    gettoken y rhs : varlist
                    
                    forvalues j=1/48 {
                    ml model lf lfols_lf (`y'=`rhs')  /lnsigma, max if subject==`j'
            }
                    ereturn local cmd lfols
            end    
            
            * try with a dataset everyone has
            use "C:\Users\Emna\Desktop\Revision\Data & program_rev\databases_original\panel regression data_weights&payoff_individual.dta", clear
            
            * try model fit without -bootstrap- to make sure it works
            
            lfols y x1 x2 x3 displ
            
            * replay
            lfols, level(90)
            
            * try with bootstrap prefix
            bootstrap _b: lfols y x1 x2 x3 displ
            
            
            
            esttab using C:\Users\Emna\Desktop\bootmlA.rtf, nodepvar nogaps nomtitles compress  b(%12.4f) z(3) parentheses order(x1 x2 x3)
            Thanks a lot for your response.

            Emna
            Last edited by Emna Trabelsi; 26 Aug 2020, 07:04.

            Comment


            • #7
              The if condition should be specified before the comma.

              However, I would change program lfols to

              Code:
              program lfols, eclass
                      if replay() {
                              ml display `0'
                              exit
                      }
              
                      syntax varlist(numeric) [if] [in]
              
                      gettoken y rhs : varlist
                      ml model lf lfols_lf (`y'=`rhs') /lnsigma `if' `in', max
              
                      ereturn local cmd lfols
              
                      ml display
              end
              Now we can use an if condition or in range with any dataset, and even with bootstrap.

              Also, I originally forgot to call ml display to show the fitted estimation results.

              For example,

              Code:
              sysuse auto
              lfols mpg turn trunk displ if foreign==0
              bootstrap _b if foreign: lfols mpg turn trunk displ

              Comment


              • #8
                Jeff Pitblado (StataCorp),

                Thank you so much for your response. It works marvellously. Now, if I want to repeat the code 48 times just as in my case, one can write:

                Code:
                use "C:\Users\Emna\Desktop\Revision\Data & program_rev\databases_original\panel regression data_weights&payoff_individual.dta", clear  
                
                forvalues j=1/48 {
                
                lfols y x1 x2 x3 if subject==`j'  
                
                eststo: bootstrap _b if subject: lfols y x1 x2 x3 displ
                
                }
                 esttab using C:\Users\Emna\Desktop\bootmlA.rtf, star(* 0.1 ** 0.05 *** 0.01) nodepvar nogaps nomtitles compress  b(%12.4f) z(3) parentheses order(x1 x2 x3)
                Last edited by Emna Trabelsi; 27 Aug 2020, 05:33.

                Comment


                • #9
                  I think the call to bootstrap should be

                  Code:
                  eststo: bootstrap _b if subject==`j': lfols y x1 x2 x3

                  Comment


                  • #10
                    Originally posted by Jeff Pitblado (StataCorp) View Post
                    I think the call to bootstrap should be

                    Code:
                    eststo: bootstrap _b if subject==`j': lfols y x1 x2 x3
                    Dear Jeff,

                    Yes, you're right. I corrected the code. Thank you!

                    Comment

                    Working...
                    X