Announcement

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

  • reshape inside a bootstrap program

    Dear Statalist,

    I need to use the reshape command inside a bootstrap program.

    My question is: how can I run the reshape command inside the program?


    WORKING EXAMPLE
    Code:
    sysuse auto, clear
    
    gen bene_id = _n
    
    capture program drop myboot
    
    program define myboot, eclass
    
    capture keep price weight length bene_id __000000 mpg
    
    gen random_id = runiform()
    sort random_id
    
    replace length = length * 1.1 if random_id < 0.5
    
    areg price weight length, absorb(mpg)
    
    matrix temp = e(b)
    ereturn post temp
    end
    
    bootstrap, reps(100) seed(200): myboot
    NOT WORKING EXAMPLE (I add the reshape command)
    Code:
    sysuse auto, clear
    
    gen bene_id = _n
    
    capture program drop myboot
    
    program define myboot, eclass
    
    capture keep price weight length bene_id __000000 mpg
    
    gen month_1 = 1
    gen month_2 = 2
    gen month_3 = 3
    gen month_4 = 4
    gen month_5 = 5
    gen month_6 = 6
    
    reshape long month_, i(bene_id) j(months)
    
    gen random_id = runiform()
    sort random_id
    
    replace length = length * 1.1 if random_id < 0.5 & month_ <= 3
    
    areg price weight length, absorb(mpg)
    
    matrix temp = e(b)
    ereturn post temp
    end
    
    bootstrap, reps(100) seed(200): myboot

    The purpose of this code is solely to demonstrate the current issue I am facing in using the reshape command inside a program with a similar (not identical) structure. I cannot post the original because of its length.

    So, I answer two obvious objections preemptively.
    Q1) Do you know that, by reshaping and then sorting by random_id, each individual's observations will be sorted randomly, and so the 'working example' and the 'non working example' are two inherently different approaches? A1) Yes I know it, this is just a demo code.
    Q2) Why don't you run the reshape command outside of the program? A2) I cannot, because what I am doing requires this to happen inside the bootstrap program (take my word on this).

    So, my question is simply how can I run the reshape command inside the bootstrap program above, without getting this error message that I get:

    Code:
    Bootstrap replications (100)
    ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx    50
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx   100
    insufficient observations to compute bootstrap standard errors
    no results will be saved
    Thanks
    Last edited by dimitris karletsos; 29 May 2023, 07:29. Reason: bootstrap

  • #2
    Hi people, I solved it. The answer to "how to use reshape inside a bootstrap program?" is "do not use reshape, use tolong instead".

    God knows what was happening in the background in the reshape command that made my bootstrap program fail. If anyone else apart from God knows it, please post it below for knowledge sharing.

    In the meantime, I post the working code with tolong.

    Code:
    sysuse auto, clear
    
    gen bene_id = _n
    
    capture program drop myboot
    
    program define myboot, eclass
    
    capture keep price weight length bene_id mpg __000000
    
    gen month_1 = 1
    gen month_2 = 2
    gen month_3 = 3
    gen month_4 = 4
    gen month_5 = 5
    gen month_6 = 6
    
    tolong month_, i(bene_id) j(months)
    
    gen random_id = runiform()
    sort random_id
    
    replace length = length * 1.1 if random_id < 0.5 <= 3 & month_ <= 3
    
    areg price weight length, absorb(mpg)
    
    matrix temp = e(b)
    ereturn post temp
    
    end
    
    bootstrap, reps(100) seed(200): myboot

    Comment

    Working...
    X