Announcement

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

  • Seeking help with a Monte Carlo script

    Hello,

    I have been working on a monte carlo script (see code below) and was encountering an issue where the simulation keeps returning red "X"'s after the first run and output is all missing values. Strangely, if I run the code before "program MC_FCS, rclass", then proceed to run the code inside the monte carlo loop (from "program MC_FCS, rclass" to "end", non-inclusive) the code runs! Adding "trace" as an option also throws up no errors and as far as I can tell, the code is running just fine. This has me a bit confused as to why the monte carlo loop is not running, any suggestions/tips would be very much appreciated!

    I have used an older script as a template and as far as I can tell, there seems to be no issue with the syntax, but I could be wrong...

    Code:
    *-------------------------------------------------------------------------------
    *----- This is a monte carlo script for analyzing validity of FCS over time ----
    *-------------------------------------------------------------------------------
    
    **********************************SETTINGS**************************************
    clear all
    set more off, permanent
    set varabbrev off
    
    **********************************GLOBALS***************************************
    global graph 0
    global cutoff_poor 20
    global cutoff_border 40
    global scalefactor 1.1
    
    
    
    
    **********************************DATA READ IN**********************************
    use "C:\Users\15102\Desktop\Monte_FCS\MonteCarloCode\bl_fd_migrant_2019shock_old.dta", clear
    keep hhid week cs_*
    
    **********************************SETUP PANEL***********************************
    xtset hhid week
    
    **********************************SETUP DATA************************************
    ****FCS CATEGORIES
    *STAPLES (weight=2)
    cap egen cs_staples=rowtotal(cs_ricegrains cs_wheatflour)
    *Pulses (weight=3)
    cap egen cs_pulses=rowtotal(cs_pulsespice cs_tubers)
    *Veg (weight=1)
    cap egen cs_veg=rowtotal(cs_veg)
    *Fruit (weight=1)
    cap egen cs_fruit=rowtotal(cs_fruit)
    *Meat and other protiens (weight=4)
    cap egen cs_meat=rowtotal(cs_fishshrimp cs_poultry cs_eggs cs_redmeat)
    *Milk (weight=4)
    cap egen cs_dairy=rowtotal(cs_diary)
    *Sugar and oil (weight=0.5*2=1)
    cap egen cs_packaged=rowtotal(cs_packaged)
    
    ****setup assumed values to convert to consumption count:
    foreach v in staples pulses veg fruit meat dairy packaged {
    qui sum cs_`v' if cs_`v'>0
    scalar temp_`v'=(r(mean)/5)*${scalefactor}
    global v_`v' temp_`v'
    di "price of ${v_`v'}:"${v_`v'}
    }
    
    ****convert to number of days:
    foreach v in staples pulses veg fruit meat dairy packaged {
    gen days_`v'=cs_`v'/${v_`v'}
    sum days_`v'
    replace days_`v'=7 if days_`v'>=7
    }
    
    ****Graphical check
    if ${graph}==1 {
    kdensity days_staples
    kdensity days_pulses
    kdensity days_veg
    kdensity days_fruit
    kdensity days_meat
    kdensity days_dairy
    kdensity days_packaged
    }
    
    **********************************MONTE CARLO***********************************
    program MC_FCS, rclass
    
    
    
    *Step1. Calculate random time to "do first survey"(wave1)
    gen RNG1=uniform()
    egen RNG1_max=max(RNG1) if week<=12
    gen start=0
    replace start=1 if RNG1==RNG1_max
    sum week if start==1
    gen wave1=r(mean)
    sum wave1
    
    
    *Step2. calculate (roughly) alternative waves 2-4
    gen wave2=wave1+12
    gen wave3=wave2+12
    gen wave4=wave3+12
    sum wave*
    
    *Step3 calculate FCS for each week
    gen FCS=2*days_staples+3*days_pulses+days_veg+days_fruit+4*days_meat+4*days_dairy+days_packaged
    if ${graph}==1 {
    kdensity FCS
    }
    
    *Step4 calculate "4 wave FCS"
    forvalues v=1(1)4 {
    gen FCS_w`v'=.
    bys hhid: replace FCS_w`v'=FCS if week==wave`v'
    }
    forvalues v=1(1)4 {
    bys hhid: egen temp`v'=max(FCS_w`v')
    drop FCS_w`v'
    rename temp`v' FCS_w`v'
    }
    
    *step5 calculate average FCS
    bys hhid: egen avg_FCS=mean(FCS)
    bys hhid: gen est_FCS=(FCS_w1+FCS_w2+FCS_w3+FCS_w4)/4
    
    *step5 drop duplicates and calculate checking states
    *preserve
    *duplicates drop hhid, force
    *var1: level difference
    gen delta_level=est_FCS-avg_FCS
    *var2: % difference
    gen delta_pct=delta_level/avg_FCS
    *classification
    gen avg_FCS_poor=0
    gen avg_FCS_borderline=0
    gen avg_FCS_accept=0
    replace avg_FCS_poor=1 if avg_FCS<=${cutoff_poor}
    replace avg_FCS_borderline=1 if avg_FCS>${cutoff_poor} & avg_FCS<=${cutoff_border}
    replace avg_FCS_accept=1 if avg_FCS>${cutoff_border}
    gen est_FCS_poor=0
    gen est_FCS_borderline=0
    gen est_FCS_accept=0
    replace est_FCS_poor=1 if est_FCS<=${cutoff_poor}
    replace est_FCS_borderline=1 if est_FCS>${cutoff_poor} & est_FCS<=${cutoff_border}
    replace est_FCS_accept=1 if est_FCS>${cutoff_border}
    *var3: misclass
    gen misclass=0
    replace misclass=1 if avg_FCS_poor!=est_FCS_poor
    replace misclass=1 if avg_FCS_borderline!=est_FCS_borderline
    replace misclass=1 if avg_FCS_accept!=est_FCS_accept
    
    
    drop RNG1 RNG1_max start wave1 wave2 wave3 wave4 FCS FCS_w1 FCS_w2 FCS_w3 FCS_w4 avg_FCS est_FCS delta_level delta_pct avg_FCS_poor avg_FCS_borderline avg_FCS_accept est_FCS_poor est_FCS_borderline est_FCS_accept misclass
    *restore
    end
    
    
    *****The key concern here is that the list of things to be simulated changes depending on choices at the UI level (need fix)!
    *preserve
    simulate ///
    r(avg_FCS) r(est_FCS) r(delta_level) r(delta_pct) r(misclass), trace dots reps(10): MC_FCS
    
    
    gen avg_FCS=_sim_1
    gen est_FCS=_sim_2
    gen delta_level=_sim_3
    gen delta_pct=_sim_4
    gen misclass=_sim_5
    
    sum avg_FCS est_FCS delta_level delta_pct misclass

  • #2
    Kindly Ignore...I was missing "return scalar", keeping this post here untilI I can figure out how to delete this post.

    Comment


    • #3
      You can't delete a post. You can edit it for a brief time after original posting, but deletions are not allowed.

      This is a forum for community learning. Others can learn from your problem and solution. That's why you should leave what you did up. In fact, my response to #2 is: thank you for closing the thread by showing the solution. Others coming here with similar problems in the future will be grateful.

      Comment

      Working...
      X