Announcement

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

  • Model validation using foreach and forvalues

    Hi everyone,

    I am trying to validate my model. I have 11 models from -50 to +50 in steps of 10. The columns with the model output are named from 'mi50_oc77' to 'mo50_oc81', which means mi (minus) 50 oc (occurrence continuous value from 0 to 1) 77 (year of 1977).

    Basically, I have some field data that I will validate my model. The field data contain site name, location, year and status, then I have multiple columns with the output (status) of my model for every location and year.

    Here is an example of my data is organised:

    location_name latitude longitude year (goes from 1977 to 1981) status mi50_oc77 mi50_oc78 mi50_oc79 up until column mo50_oc81 yearshort loc_1 loc_2 loc_3 loc_4 loc_5 loc_6 loc_7 loc_8 (loc_ is a dummy for location) status_spr (is a dummy for status) location_code


    I generated:

    gen mi50_correct_yes=. // correct output of my model and the field data (both present)
    gen mi50_correct_no=. // correct output of my model and the field data (both absent)
    gen mi50_overprediction=. // model overprediction
    gen mi50_underprediction=. // model underprediction

    I would like to select columns mi50_oc77 up until column mo50_oc81 as my valist.

    Then for every single value of valist I would like to apply this code:


    forvalues loc=1/8 {
    forvalues yr=77/81 {
    replace mi50_correct_yes=1 if status==1 & `loc'==location_code & mi50_oc`yr'>0.5 & `yr'==yearshort
    replace mi50_correct_no=1 if status==0 & `loc'==location_code & mi50_oc`yr'<0.5 & `yr'==yearshort
    replace mi50_overprediction=1 if status==1 & `loc'==location_code & mi50_oc`yr'<0.5 & `yr'==yearshort
    replace mi50_underprediction=1 if status==0 & `loc'==location_code & mi50_oc`yr'>0.5 & `yr'==yearshort
    replace mi50_correct_yes=0 if mi50_surandap_yes==.
    replace mi50_correct_no=0 if mi50_surandap_no==.
    replace mi50_overprediction=0 if mi50_surandap_yes_no==.
    replace mi50_underprediction=0 if mi50_surandap_no_yes==.
    }
    }

    Currently, I am repeating this loop for every single model (11 times, as I have 11 models). Surely there is a more efficient way to do this.

    Could anyone please help with the code? I believe I will have to use foreach going through the valist...

    MANY thanks!!!!

  • #2
    You're not new here, so please give data examples using dataex and properly formatted code. Nobody (at least not me) can follow this without those two things, data, and code.

    Comment


    • #3
      maybe,
      Code:
      foreach model of varlist mi50_oc77-mo50_oc81 {
          
      local step = substr("`model'",1,4)
      
      gen `step'_correct_yes    = cond(`step'_surandap_yes    == ., 0, .)
      gen `step'_correct_no     = cond(`step'_surandap_no     == ., 0, .)
      gen `step'_overprediction = cond(`step'_surandap_yes_no == ., 0, .)
      gen `step'_underpediction = cond(`step'_surandap_no_yes == ., 0, .)
      
      forval loc = 1/8 {
      forval yr = 77/81 {
      replace `step'_correct_yes    = 1 if status==1 & location_code==`loc' & yearshort==`yr' & `step'_oc`yr'>.5
      replace `step'_correct_no     = 1 if status==0 & location_code==`loc' & yearshort==`yr' & `step'_oc`yr'<.5
      replace `step'_overprediction = 1 if status==1 & location_code==`loc' & yearshort==`yr' & `step'_oc`yr'<.5
      replace `step'_underpediction = 1 if status==0 & location_code==`loc' & yearshort==`yr' & `step'_oc`yr'>.5
      }
      }
      
      }

      Comment


      • #4
        Genial! It worked! Really appreciate your help, as I had been working on that code for a while...

        Comment

        Working...
        X