6. Stata loop (foreach) combined with local and levelsof functionality

Поділитися
Вставка
  • Опубліковано 19 лис 2024

КОМЕНТАРІ • 21

  • @muddisneakers
    @muddisneakers 2 роки тому +1

    People like you make the world a better place! Thank you so much for this video. I've been struggling with a solution and your levelsof explanation was just what I needed to solve the problem. 😊

  • @sayakdasgupta5309
    @sayakdasgupta5309 7 місяців тому +2

    Hi, I am facing troubles with the tab type command. STATA is showing "variable type not found" (r111). I think it'll work if you let us know the variables in your updated file. Maybe, my auto2_updated file does not consist of all the variables that you have in your dta. Thanks in advance.

  • @margretkiizza7890
    @margretkiizza7890 Рік тому +1

    Thank you for the video. I am trying to run cross validation with kfold. However, I want to use the first 13 observations to train the poisson model and then validate it with the last 5 observations. and then make the predictions. However my do file is giving me errors. This is my dofile: local num_folds = 3
    forval fold = 1/'num_folds' {
    if `fold' 1 qui keep in 1/13
    else if `fold' 2 qui keep in 1/13
    else if `fold' 3 qui keep in 14/18
    poisson NumberofAccidents Entrywidthe Entryangle Exitwidthex ExitRadius Cyclelanewidth InscribedDiameterD InAADTMajV lnAADTMajP lnAADTMinV lnAADTMinP, exposure(exposure)
    predict estimatedaccidents if `fold' 3
    list estimatedaccidents if e(sample), noobs}

    • @learndataanalyticswithus5437
      @learndataanalyticswithus5437  Рік тому +2

      As I don't have the data so I can't test your do file. However, the thing that can be a possible cause of error is that you are trying to run mode on very few observations.

  • @shilpakrishnan7674
    @shilpakrishnan7674 Рік тому +1

    could you explain a little bit on global macros in stata? how to write data path using global and to include global path while writing the path of dictionary file in stata?

    • @learndataanalyticswithus5437
      @learndataanalyticswithus5437  Рік тому +2

      Thanks Shilpa for your reply.
      The given below code is a good example of how file paths can be used through globals.
      // setting up globals
      global projdir "C:/Users/amolr/Desktop/session_2"
      global raw_data "${projdir}/01_data/01_raw_data"
      global final_data "${projdir}/01_data/03_final_data"
      // importing the vaccination dataset as of 2021.04.18
      use "${raw_data}/covid_vaccination_2021_04_18.dta", clear

    • @shilpakrishnan7674
      @shilpakrishnan7674 Рік тому +1

      THANK YOU

    • @learndataanalyticswithus5437
      @learndataanalyticswithus5437  Рік тому +1

      You are welcome!

  • @shilpakrishnan7674
    @shilpakrishnan7674 Рік тому +1

    how to add the command replace variable x if y== some value within a loop? so that both x and y gets added by 1 in each iteration?

    • @learndataanalyticswithus5437
      @learndataanalyticswithus5437  Рік тому +1

      Can you explain your question with a numeric example? Sorry I am a bit unclear about your question. Thanks

    • @shilpakrishnan7674
      @shilpakrishnan7674 Рік тому +1

      @@learndataanalyticswithus5437 I think I need to use a nested loop. IDK how. The code without loop may look like this. How can I write a loop for this? Var_y is defined for Var_x>=5.
      gen var_z =.
      replace var_z=1 if Var_x ==1
      replace var_z=1 if Var_x ==2
      replace var_z=2 if Var_x ==3
      replace var_z=3 if Var_x ==4
      replace var_z=4 if Var_x == 5 & Var_y ==1
      replace var_z=4 if Var_x == 5 & Var_y ==2
      replace var_z=5 if Var_x == 6 & Var_y ==1
      replace var_z=5 if Var_x == 6 & Var_y ==2

    • @learndataanalyticswithus5437
      @learndataanalyticswithus5437  Рік тому

      Maybe
      replace 'i' if var_x>=5
      In above eq i will be contacting variable y

  • @varunmiglani110
    @varunmiglani110 3 роки тому +1

    i want to get mean of two variable i run following command and i get error invalid syntax r(198);

    foreach c inagcro ainc v_dairyinc {
    tabstat `c'
    }