Announcement

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

  • Combinations of variables

    Hi everyone,
    I am a new Stata user and have a problem with commands.
    I am working with a dataset of deaths caused by natural disasters (floods, droughts and typhoons) which has 4 variables: Deaths (deaths toll, wich ranges from 0 to 22), Floods, Droughts and Typhoons (in the three cases the variable is the probability of having X deaths due to the specific type of natural disaster).
    What I want to do is to compute conditional probabilities for each death toll (0-22) considering all types of disasters. For example the probability of having 1 death is: Floods(=1)*Droughts(=0)*Typhoons(=0) + Floods(=0)*Droughts(=1)*Typhoons(=0) + Floods(=0)*Droughts(=0)*Typhoons(=1). And so on up to 22..
    I really do not know how to do this in Stata not even which command to use.
    Dos anyone could help me with this? Any sugerence is welcome.
    Thanks in advance
    Last edited by Fernando Gonzalez; 18 Jun 2019, 11:50.

  • #2
    As you did not show example data, I have written code that works for a data set that is compatible with your somewhat loose description, but might in fact be quite different. The first part of the code creates a demonstration data set (with the probabilities as just some random numbers). The second part does the calculation you asked for.

    Code:
    //    CREATE A DEMONSTRATION DATA SET
    set obs 22
    gen events = _n-1
    
    set seed 1234
    foreach x in p_floods p_droughts p_typhoons {
        gen double `x' = runiform()
        summ `x', meanonly
        replace `x' = `x'/`r(sum)' // SUM OF PROBABILITIES = 1
    }
    
    //    STOP HERE AND VERIFY THAT THIS DATA IS LAID OUT SIMILARLY TO
    //    YOUR DATA SET
    
    
    //  IF SO, THE FOLLOWING CODE DOES WHAT YOU ASKED FOR
    
    tempfile copy
    save `copy'
    
    use `copy'
    keep events p_floods
    rename events n_floods
    cross using `copy'
    keep n_floods p_floods events p_droughts
    rename events n_droughts
    cross using `copy'
    rename events n_typhoons
    
    egen events = rowtotal(n_*)
    gen path_prob = p_floods*p_droughts*p_typhoons
    collapse (sum) prob_this_many_events = path_prob, by(events)
    Note: If you have between 0 and 22 droughts, and 0 and 22 floods, and 0 and 22 typhoons, then the total number of events must range between 0 and 63, not 0 and 22.

    In the future, when asking for help with code, show example date, and when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Thanks very much Clyde. It worked. I will consider your recommendations hereon. Thanks again for your valuable response

      Comment

      Working...
      X