Announcement

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

  • Looping with condition

    I am using Stata 15 for generating my data.
    I would like to generate my Outcome y (as continuous variable) and then split them into 5 item-variables.
    After that I would like to re-check if sum_item(after split) is equal to y, if not, then Looping should run again until the condition meet (y=sum_item)

    The Problem is I will Always get at least 1 case that diff (y-sum_item) is not 0, so it means my Looping command doesn't work in this condition
    and I don't know where is the failure on my Code.


    Code:
      *Example data
    
      . list y item1 item2 item3 item4 item5 sum_item in 1/5
    
         +-------------------------------------------------------+
         |  y   item1   item2   item3   item4   item5   sum_item |
         |-------------------------------------------------------|
      1. | 14       4       2       4       3       1         14 |
      2. | 16       2       1       4       4       5         16 |
      3. | 17       3       4       3       4       3         17 |
      4. | 17       3       1       4       4       5         17 |
      5. | 18       5       4       3       5       1         18 |
         +-------------------------------------------------------+
    
      *Generate Outcome y
      generate y = round(rnormal(20, 5))
     
      *Generate each item
       forvalues k = 1(1)5 {
        gen item`k' = runiform(1, 5)
        replace item`k' = round(item`k')
        }
        
       egen sum_item=rowtotal(item1 item2 item3 item4 item5)  
       gen diff = y - sum_item
      
      *Looping check if y=sum_item
    
      forvalues a = 1(1)`=_N'  {
       if run[`a']==`a' {
         forvalues k = 1(1)5 {
          replace sum_item=. if sum_item!=y[_n]
          replace item`k' =. if sum_item==.
          
          replace item`k' = runiform(1, 5) if item`k'==.
          replace item`k' = round(item`k')
          replace sum_item= item1 + item2+item3+item4+item5 if sum_item==.
          replace diff = y - sum_item
          }
         }
        }
    
    *Result after running Looping
    . tab diff
    
           diff |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              0 |        494       98.80       98.80
              6 |          1        0.20       99.00
              8 |          2        0.40       99.40
             10 |          1        0.20       99.60
             12 |          1        0.20       99.80
             14 |          1        0.20      100.00
    ------------+-----------------------------------
          Total |        500      100.00

  • #2
    First, the expected value of the sum of 5 variables each having mean 5 will be 25, not 20. So this is way off to start with.

    But putting that aside, there is no reason to expect that the sum of the five items will equal y when the five items are sampled independently of each other and of y. The only way to make that work is to sample four parts and then just set the fifth to the difference between y and the sum of the first four items.

    Comment


    • #3
      input str15 pri_occu_mem1 str16(pri_occu_mem2 sec_occu_mem1 sec_occu_mem2) long(income1_mem1 income1_mem2)
      "Agriculture" "Agriculture" "No_occu" "No_occu" 25000 0
      "Agriculture" "Homemaker" "" "" 30000 0
      "Homemaker" "" "" "" 0 .
      "Agriculture" "Homemaker" "No_occu" "No_occu" 25000 0
      "Agriculture" "Homemaker" "Wage_labor" "" 5000 0
      "Homemaker" "Private_job" "" "" 0 120000
      "Private_job" "Homemaker" "" "" 300000 0
      "Agriculture" "Agriculture" "" "" 25000 0
      "Homemaker" "Agriculture" "" "" 0 50000
      "Agriculture" "Agriculture" "" "" 70000 0
      "Private_job" "Homemaker" "" "" 100000 0
      "Wage_labor" "Homemaker" "" "" 40000 0
      "Agriculture" "Homemaker" "" "" 50000 0
      "Agriculture" "Gov_job" "Private_job" "" 60000 30000
      "Agriculture" "Agriculture" "" "" 30000 0
      "Private_job" "Homemaker" "" "" 20000 0
      "Agriculture" "Homemaker" "" "" 25000 0
      "Agriculture" "Homemaker" "" "Agriculture" 30000 0
      "Agriculture" "Agriculture" "" "" 30000 0
      "Agriculture" "Agriculture" "No_occu" "No_occu" 40000 0
      "Agriculture" "Homemaker" "" "" 20000 0
      "Agriculture" "Homemaker" "" "" 30000 0
      "Agriculture" "Agriculture" "" "" 50000 0
      "Agriculture" "Homemaker" "" "" 10000 0
      "Agriculture" "Homemaker" "" "" 15000 0
      "Agriculture" "Homemaker" "" "" 35000 0
      "Agriculture" "Agriculture" "" "" 50000 0
      "Agriculture" "Homemaker" "" "No_occu" 21000 0
      "Wage_labor" "Homemaker" "" "" 60000 0
      "Agriculture" "Homemaker" "No_occu" "No_occu" 21000 0
      "Agriculture" "Homemaker" "" "" 20000 0
      "Agriculture" "Homemaker" "" "" 20000 0
      "Agriculture" "Homemaker" "" "" 15000 0
      "Agriculture" "Homemaker" "" "" 15000 0
      "Private_job" "Homemaker" "" "" 30000 0
      "Wage_labor" "Homemaker" "" "" 300000 0
      "Agriculture" "Homemaker" "" "" 25000 0
      "Agriculture" "Agriculture" "No_occu" "No_occu" 35000 0
      "Agriculture" "Homemaker" "" "" 30000 0
      "Agriculture" "Homemaker" "" "" 20000 0
      "Agriculture" "Homemaker" "" "" 25000 0
      "Wage_labor" "Homemaker" "" "" 40000 0
      "Agriculture" "Agriculture" "" "" 30000 0
      "Wage_labor" "Homemaker" "" "" 50000 0
      "Agriculture" "Homemaker" "" "" 27000 0
      "No_occu" "No_occu" "" "" 0 0
      "Homemaker" "" "Agriculture" "" 0 .
      "Agriculture" "Homemaker" "" "" 150000 0
      "Wage_labor" "Homemaker" "" "" 20000 0
      "Agriculture" "Homemaker" "" "Agriculture" 60000 0
      "Agriculture" "Homemaker" "" "" 40000 0
      "Agriculture" "Agriculture" "" "" 50000 0
      "Agriculture" "Homemaker" "" "" 20000 0
      "Agriculture" "Homemaker" "" "Agriculture" 20000 0
      "Agriculture" "Homemaker" "No_occu" "No_occu" 25000 0
      "Agriculture" "Homemaker" "" "" 25000 0
      "Agriculture" "No_occu" "No_occu" "No_occu" 15000 50000
      "Agriculture" "Agriculture" "" "" 35000 0
      "Agriculture" "Agriculture" "No_occu" "No_occu" 15000 0
      "Agriculture" "Homemaker" "" "" 22000 0
      "Agriculture" "Homemaker" "" "" 21000 0
      "Agriculture" "Homemaker" "" "Agriculture" 30000 0
      "Agriculture" "Homemaker" "Private_job" "" 20000 0
      "Agriculture" "Homemaker" "" "" 2000 0


      Here is the example of my data, unit of observation is household, for each household, I have information of 20 household members, and hence 20 variables for primary occupation, secondary occupation as well as income earned from those occupation. I need to create a variable total income if primary and secondary occupation is Agriculture. Here is my code

      foreach v of varlist pri_occu_* sec_occu_mem* {
      foreach j of varlist income1_mem* income2_mem* income3_mem* migincome_mem* {
      egen total_income_agri_labor=total(`j') if `v'=="Agriculture"
      }
      }

      After I run the above code stata shows the following error

      total_income_agri already defined

      Could someone help me to rectify my code?

      Comment


      • #4
        Apart from the error you identify -- this single variable you are creating would contain just one value, the total of several variables over several other variables conditional on the latter being equal to "Agriculture". I could give you code to get there but I doubt that it is really what you want.

        In particular, my wild guess is that you may want rowtotal() not total().

        Comment


        • #5
          Yes, I do need the rowtotal. I typed total by mistake.

          Comment


          • #6
            foreach v of varlist n_pri_occu_* n_sec_occu_mem* n_ter_occu_mem* {
            foreach j of varlist income1_mem* income2_mem* income3_mem* migincome_mem*{
            egen total_income_agri_labor=rowtotal(`j') if `v'==1
            }
            }

            The above code gives me the error ; total_income_agri_labor already defined

            Comment


            • #7
              Well, yes, you fixed one problem but your first problem remained. This may help.


              Code:
              gen total_income_agri_labor = 0 
              
              foreach v of varlist n_pri_occu_* n_sec_occu_mem* n_ter_occu_mem* {
                  egen work = rowtotal(income1_mem* income2_mem* income3_mem* migincome_mem*)  if `v' == 1 
                  replace total_income_agri_labor = total_income_agri_labor + work 
                  drop work 
              }

              Comment


              • #8
                Thanks Nick, however this gives me a variable with no observations, what I am trying to understand here is why the error variable already defined? Is there any other way to get total income of a household if members report income only for agriculture labor? I tried the same code for occupation business and it also shows no observations.
                Last edited by Abha Indurkar; 03 Mar 2020, 22:43.

                Comment


                • #9
                  I don't follow that. If the code ran at all, the worst you could expect is that all values remain zero, not that there are no observations.

                  What I can buy is that the code is not what you want, because I am reacting to your latest post each time and (quite likely) not following what you want. Indeed, and sorry on my part,
                  #7 looks legal but nonsense when I read all your posts again.

                  I started again with one of your statements in #3 which is

                  I need to create a variable total income if primary and secondary occupation is Agriculture.
                  Note the and. At the outset I assumed you didn't mean what you say as if the primary occupation is Agriculture how can the secondary occupation be Agriculture too, and vice versa? So, I think and means or here, which is always confusing to a programmer.

                  Also your code started testing occupations for being "Agriculture", a string value, and then all of a sudden in #5 you were comparing with 1, a numeric value. That was and is puzzling, but i assumed you had a good reason for that, say that you were working with a different version of the dataset, or whatever,

                  Any way, I now guess that you want to sum all the incomes from agriculture. If so, then you just need a loop over family members. Perhaps this will help, but I am still guessing as your variable name conventions are inconsistent: pri... and sec... and income1... and income2... I guess should be how occupations and incomes line up.

                  Your example in #3 does not let this be tested, as you have 4 occupations but just 2 incomes. This relates to testing for "Agriculture" and surely won't work if the occupation variable is numeric. I am assuming that all 40 variables are defined.


                  Code:
                  local work total_income_agri_labor
                  gen `work' = 0
                  
                  forval j = 1/20 {
                       replace `work' = `work' + income1_mem`j' if pri_occu_mem`j' == "Agriculture"
                       replace `work' = `work' + income2_mem`j' if sec_occu_mem`j' == "Agriculture"
                  }

                  Comment


                  • #10
                    hello,help please having two var that measures wellbeing, happiness (4categories), satisfaction(10categories) in an RCT trial with treat(0,1), how may i transform the variables to be easily interpreted?. Thank you.

                    Comment


                    • #11
                      #10 sebastian asuka Please post in a new thread. Your question has no obvious bearing on this thread, which is confused already. I have to say that it looks unanswerable to me without much more detail,

                      Comment


                      • #12
                        ---------------------- copy starting from the next line -----------------------
                        Code:
                        * Example generated by -dataex-. To install: ssc install dataex
                        clear
                        input double(household_id longitude latitude age) byte(gender education) int village byte(wvs_happiness wvs_life_sat) float(treat spillover purecontrol cons_food cons_social cons_total hh_size children mis a sum)
                         186779224 34.3845287 -.1212743  28 2  8 15 3  5 0 1 0  5238.519  4650 24762.477  5 4 . .a 0
                         188387876    34.3882 -.1299187   . .  . 15 .  . 1 0 0         0     0         0  . 0 . .a 0
                         176895075 34.3902187   -.11984  25 2  3 15 3  8 1 0 0 1396.3934   900 12429.285  4 1 . .a 0
                        1482168531 34.3942218 -.1150712  34 2 13 16 3  5 0 1 0  1325.634  5800  8441.393  7 5 . .a 0
                        1065171387  34.397085 -.1180231  39 2 11 16 3  4 0 1 0 2084.4536  2940 15293.356  5 3 . .a 0
                        1068171384 34.3941353 -.1178486  51 1 14 16 .  . 1 0 0 2352.9507  3920 19689.855  9 7 . .a 0
                        1068169977 34.4059633 -.1090553  45 1  2 16 .  . 1 0 0   564.153     0  4397.857  2 1 . .a 0
                        1083168487   34.39193    -.1148  49 2  7 16 3  3 0 1 0  1606.776  2000  8752.619  1 0 . .a 0
                        1477171400   34.40493 -.1091743  54 2  1 16 3  4 1 0 0  3446.541  3150 22150.404  5 1 . .a 0
                        2187180012 34.3941162 -.1175743  46 1 14 16 .  . 1 0 0 3564.6995   880 18490.238  1 0 . .a 0
                        2279180002 34.3977931   -.11823  34 1  8 16 3  3 1 0 0 1321.8306  6600  9368.928  3 1 . .a 0
                        1069167047   34.39821 -.1175243  34 2 10 16 3  3 0 1 0 2132.7595  1800 12962.738  6 4 . .a 0
                        1269168481   34.39712   -.11806  29 2 12 16 3  4 0 1 0   926.153  3700  7276.381  4 1 . .a 0
                        1274167050    34.3974 -.1174687  28 2  8 16 3  5 1 0 0 2182.5247  1900 14891.237  6 4 . .a 0
                        2774806415   34.37858 -.0772906  62 1 12 19 3  1 0 0 1  1862.377  2460 10829.643  6 4 . .a 0
                        1371810744 34.3713606 -.0821631  63 1  1 19 3 10 0 0 1   3269.18  2800 17952.617 11 6 . .a 0
                        2787804992   34.37146   -.08363  26 1 10 19 3  2 0 0 1 1375.6066   680  9340.797  3 0 . .a 0
                        2780804969 34.3718662   -.07601  40 2  8 19 3  2 0 0 1 2455.3826  3180 13706.785  6 3 . .a 0
                        1365804977   34.36824   -.08263  50 2  9 19 3  4 0 0 1 1318.6885  2000  8329.881  3 1 . .a 0
                        1384807882   34.37843   -.07741  47 1 12 19 3  3 0 0 1 1510.6558  1600   8643.81  4 2 . .a 0
                        1467110895   34.40011   -.07122  41 2 10 22 4  2 1 0 0 1140.3005 20500  7344.286  5 3 . .a 0
                        1469108012   34.40382  -.070027  40 2  1 22 3  3 0 1 0  254.7541   900 2011.6666  4 2 . .a 0
                        1480109461   34.39867   -.07259 100 2  1 22 3  1 1 0 0  173.6885    80  6017.262  1 0 . .a 0
                        1480110937   34.40218   -.07332  40 2  1 22 3  4 0 1 0 1014.4536  1350  5802.619  6 3 . .a 0
                        1565105175  34.395265 -.0679237  28 2  7 22 4  3 1 0 0 2665.9126  3460 18054.262  6 3 . .a 0
                        1271105154   34.39992    -.0691  68 1  1 22 4  3 1 0 0 2376.0437  3460 14856.928  5 2 . .a 0
                        1380105136  34.400795   -.07157  74 2  1 22 4  4 1 0 0 1190.6284  2200  9651.071  2 1 . .a 0
                        1277109456 34.4000162  -.076845  49 2 14 22 3  1 0 1 0  675.1093  3000  4486.548  3 1 . .a 0
                        1574118114   34.39657 -.0671247  49 2  9 22 3  1 0 1 0 1117.7869  2300  6492.023  3 2 . .a 0
                        1382155535   34.40377   -.07469  47 2  9 22 4  3 1 0 0  638.5519 15800  9673.929  4 2 . .a 0
                        1487106607   34.39525   -.06807  28 1 10 22 4  7 1 0 0   1638.88  3200 12811.834  4 2 . .a 0
                        1368105162   34.39917   -.06905  45 1  7 22 3  1 0 1 0  4348.918 10090 26298.023 10 6 . .a 0
                        1183105125 34.3988837   -.06803   . .  . 22 .  . 1 0 0         0     0         0  . 0 . .a 0
                        1983108037 34.3990875   -.07502  19 2 10 22 3  1 0 1 0 1603.0547  1780 10876.405  4 2 . .a 0
                        1282116698   34.39737   -.06794  26 2 11 22 2  2 0 1 0  418.2514   600  2709.881  6 3 . .a 0
                        1767105128   34.39609   -.06691  64 2  1 22 4  1 0 1 0  1722.022  2700  15236.43  5 2 . .a 0
                        1767115250   34.40477   -.07307  45 1  9 23 3  8 1 0 0 2124.5083  1450 11711.786  6 2 . .a 0
                        1776110917 34.4099525 -.0712862  24 2  8 23 3  1 0 1 0 539.21313   450  3169.762  5 2 . .a 0
                        1074106605   34.40705  -.073625  38 2  5 23 4 10 0 1 0  825.8142   850   4885.69  2 0 . .a 0
                        1282110918   34.40752    -.0709  77 1 12 23 .  . 1 0 0  346.9126   520  3780.714  1 0 . .a 0
                        1365116643 34.4086281 -.0734112  44 2  8 23 3  5 0 1 0  806.9945  1300   4938.69  3 2 . .a 0
                        1777110891   34.40723   -.07142  30 2  9 23 3  1 0 1 0  869.8361  1150      4885  5 2 . .a 0
                        1171106589  34.403498 -.0753793  64 1  8 23 .  . 0 1 0   819.317   700  6030.714  4 2 . .a 0
                        1767119530 34.4122687 -.0728312  71 1  6 23 .  . 1 0 0  975.0273   620  6618.452  3 2 . .a 0
                        1377106581 34.4032975 -.0754262  23 2  8 23 3  1 1 0 0  761.1475  4100  4839.345  3 1 . .a 0
                        1069109443 34.4060775 -.0778162  44 1 14 23 .  . 1 0 0 2112.1584  2770 12450.785  7 3 . .a 0
                        1774118136 34.4120075 -.0732718   . .  . 23 .  . 1 0 0         0     0         0  . 0 . .a 0
                        1382109449   34.40863 -.0734812  27 2  9 23 3  4 1 0 0  386.3388  2100 3348.3335  5 3 . .a 0
                        1083106560 34.4072405 -.0733405  23 2  9 23 3  2 1 0 0 2161.8252  1000 12109.547  5 2 . .a 0
                        1790106561 34.4091375 -.0737475  40 2  8 23 4  1 1 0 0 2333.2515  1300 13262.976  7 4 . .a 0
                        1165109490 34.4095152 -.0732652   . .  . 23 .  . 1 0 0         0     0         0  . 0 . .a 0
                        1077108027  34.402836  -.078717  62 1 10 23 2  9 0 1 0  4030.869  3800  27509.74  6 3 . .a 0
                        1782108027  34.404424 -.0758026  43 2  6 23 3  1 0 1 0  766.0164  2900  5385.976  5 1 . .a 0
                        1767106617 34.4061333 -.0736666  44 2  6 23 4  1 1 0 0  398.0874   550  2967.857  5 2 . .a 0
                        1177116655  34.410055 -.0745375  45 1 10 23 .  . 0 1 0   994.317   300  8640.715  9 6 . .a 0
                        1165108033 34.4069335 -.0749035  52 1 10 23 .  . 0 1 0 4030.7104  1840 26643.215  1 0 . .a 0
                        1877792037 34.3767787   -.11697  62 2  1 31 2  1 0 0 1  809.0219   580  3973.357  1 0 . .a 0
                        1867786295   34.37112 -.1204525  27 2  7 31 3  5 0 0 1 1653.6613  1350  9011.071  5 2 . .a 0
                        1282786251 34.3686486  -.124246  30 2  9 31 1  1 0 0 1      1805  3040  10326.31  5 2 . .a 0
                        1274792021 34.3776768 -.1227962  43 1 10 31 .  . 0 0 1  838.8525  1730    9382.5  1 0 . .a 0
                        1877786258 34.3712187 -.1204731  20 2 10 31 3  6 0 0 1  468.8142  1280  2626.191  2 0 . .a 0
                        1276790601 34.3743631 -.1254481  26 1 10 31 2  4 0 0 1  339.1257  9240 2835.9524  4 1 . .a 0
                        1874866883 34.4036766   -.09667  32 2 10 34 3  3 0 0 1 1429.3716  3700  9475.643  5 4 . .a 0
                        2965868329 34.3959813   -.09855  50 2  8 34 3  1 0 0 1  615.1913  1900  3617.143  3 2 . .a 0
                        1869884171   34.40338 -.0951013  22 2 10 34 4  2 0 0 1  1458.213  1440  8300.311  5 2 . .a 0
                        2972866928 34.3973913 -.0938186  48 2  1 34 4  1 0 0 1 2035.7377  3000 11543.167  8 4 . .a 0
                        1880866886 34.4012737 -.0952693  28 2 12 34 4  7 0 0 1 1814.2078  2330  9807.262  5 2 . .a 0
                        2989868361   34.39312   -.10067  30 2  9 34 4  3 0 0 1 1039.5629  1400   5941.19  4 2 . .a 0
                         148357638   34.43395   -.16255  39 2  8 46 4  4 1 0 0  3547.814 11440     23145  9 8 . .a 0
                         198238913 34.4353325   -.16102  45 1  9 46 3  5 0 1 0 3131.0874  1650  14666.88  6 3 . .a 0
                         167247544   34.43325   -.15991  34 2  9 46 2  2 0 1 0  1276.694  4700  8790.119  6 4 . .a 0
                         196738918  34.431874  -.163928  91 2  1 46 4  5 0 1 0 1711.2295  1970  9290.238  4 0 . .a 0
                         147638911   34.43312   -.16522  51 2  1 46 2  1 0 1 0  455.2131  1225  2918.226  2 1 . .a 0
                         177147534 34.4320293 -.1635793  40 1 10 46 .  . 0 1 0  2143.552 15240     15925  2 1 . .a 0
                         126648960 34.4348593   -.16297  27 2  9 46 4  7 1 0 0 1605.6284  2400 12235.952  5 3 . .a 0
                         167738914   34.43498   -.16137  40 2  8 46 2  1 1 0 0 1593.1147  2100 10369.762  4 3 . .a 0
                         177647535 34.4329218   -.15842  20 2 10 46 3  5 1 0 0  970.7104 16500 10402.857  5 2 . .a 0
                         108054731   34.43598   -.16221  25 2 10 46 3  4 1 0 0 1623.7705 15800 13611.667  5 4 . .a 0
                         177759050   34.43701   -.16194  36 2  9 46 3  2 0 1 0  768.6284  1900  8547.357  1 0 . .a 0
                         127638922   34.43276   -.16288  24 2 14 46 3  8 1 0 0   726.082   120  3622.476  4 1 . .a 0
                         168738906   34.43198   -.16545  19 2  9 46 3  1 0 1 0  417.1858   740  3219.405  3 0 . .a 0
                         126838932   34.43325   -.16264  76 2  1 46 4  3 1 0 0  476.4208   720  3006.667  3 2 . .a 0
                         127447548   34.43375   -.16012  23 1 10 46 .  . 0 1 0 1243.7705  2240  7585.952  4 0 . .a 0
                         106854772   34.43597 -.1622231  24 1 13 46 .  . 0 1 0  676.3224   650  3573.167  4 1 . .a 0
                         177746084   34.43222   -.16071  42 1 10 46 3  5 1 0 0  1927.705  6260 11737.382  6 4 . .a 0
                         176757616   34.43286   -.16303  26 2 10 46 4  7 1 0 0  3319.131 16000  22090.26  5 2 . .a 0
                         177454773 34.4364412 -.1634856  32 2 10 46 3 10 1 0 0 1357.3826  1010   7721.81  5 3 . .a 0
                         107756208 34.4318686  -.174838  28 2 10 46 4 10 1 0 0  5510.104  8400  34691.64  8 4 . .a 0
                         176850446   34.43527   -.15804  22 2 10 46 3  5 1 0 0  775.6995     0  7685.833  4 1 . .a 0
                         126751863   34.43608   -.16197  20 2  8 46 2  4 1 0 0 2203.2515 17340 15378.215  5 2 . .a 0
                         197757609   34.43134    -.1755  55 2  1 46 .  . 0 1 0  1690.328  5900 11930.834  3 1 . .a 0
                         126750457   34.43365   -.16018  23 2  6 46 3  5 1 0 0 1876.5847  5830 15349.048  4 1 . .a 0
                        1274158432   34.43799   -.16527  57 1  9 47 .  . 0 1 0 1993.8853  3000  11426.31  3 0 . .a 0
                        1977158422 34.4381146   -.16503  19 2  7 47 3  2 0 1 0  920.3279   780  8496.666  4 1 . .a 0
                        1272156978   34.43257 -.1698729  26 2 10 47 4  2 0 1 0 1564.0874  1900 10573.285  5 2 . .a 0
                        1567155577   34.43619   -.17187  38 2  7 47 2  1 0 1 0 1390.4645  8600  9164.166  3 1 . .a 0
                        1074161281 34.4317275 -.1746293  32 1 10 47 4  3 1 0 0  2832.787 23600 18513.691  7 3 . .a 0
                        1467151233   34.43237   -.17482  30 1  7 47 .  . 0 1 0 1025.6613  1620  6203.953  5 2 . .a 0
                        1482151221    34.4319   -.17397  32 2 10 47 3  3 1 0 0 3721.6284  1140 26007.334 10 7 . .a 0
                        1782151256   34.43545   -.17373  35 2 10 47 3  2 0 1 0 3040.9016  3060  23186.19  8 5 . .a 0
                        end
                        label values household_id HouseholdId
                        label values longitude dkrf
                        label values latitude dkrf
                        label values age dkrf
                        label values wvs_life_sat dkrf
                        label values gender gender
                        label def gender 1 "male", modify
                        label def gender 2 "female", modify
                        label values education highestedlevel
                        label def highestedlevel 1 "Never attended school", modify
                        label def highestedlevel 2 "Pre-school", modify
                        label def highestedlevel 3 "Standard 1", modify
                        label def highestedlevel 5 "Standard 3", modify
                        label def highestedlevel 6 "Standard 4", modify
                        label def highestedlevel 7 "Standard 5", modify
                        label def highestedlevel 8 "Standard 6", modify
                        label def highestedlevel 9 "Standard 7", modify
                        label def highestedlevel 10 "Standard 8", modify
                        label def highestedlevel 11 "Form 1", modify
                        label def highestedlevel 12 "Form 2", modify
                        label def highestedlevel 13 "Form 3", modify
                        label def highestedlevel 14 "Form 4", modify
                        label values village villagename
                        label def villagename 15 "Nyangoe", modify
                        label def villagename 16 "Miyare", modify
                        label def villagename 19 "Ochinya", modify
                        label def villagename 22 "Chore", modify
                        label def villagename 23 "Lumba", modify
                        label def villagename 31 "Konjiko", modify
                        label def villagename 34 "Konge", modify
                        label def villagename 46 "Pala", modify
                        label def villagename 47 "Asino", modify
                        label values wvs_happiness wvs3scale
                        label def wvs3scale 1 "Very happy", modify
                        label def wvs3scale 2 "Quite happy", modify
                        label def wvs3scale 3 "Not very happy", modify
                        label def wvs3scale 4 "Not at all happy", modify
                        label values treat Treat
                        label values spillover Spills
                        label values purecontrol Control
                        label values cons_food Food
                        label values cons_social Social
                        label values cons_total Total
                        label values hh_size Size
                        label values children Children
                        ------------------ copy up to and including the previous line ------------------

                        Listed 100 out of 949 observations
                        Use the count() option to list more

                        N/B
                        1.how can i determine whether randomization was successful?
                        2. measures of pscological wellbeing is measured by wvs_happiness, wvs_life_sat, measures of consumption(cons_food, cons_social, cons_total) how can i transform these variables so that they can be readily interpreted?
                        3. Evaluate the effect of cash transfers on households in treatment villages who were not selected to receice the transfer?
                        Thank you, any guide.

                        Comment


                        • #13
                          #12 Again, please start a new thread. There is no value in joining a thread with no bearing on your question,

                          Comment


                          • #14
                            Nick Cox It is a large RCT trial where control group(62villages), treatment(62 villages), then treatment village(32-control, 32-treatment), cash transfers done to the 32 villages then after one year, a survey is done to the 32, 32 and 62 villages(control) to assess if it has changed impact of their lives.

                            Comment


                            • #15
                              ---------------------- copy starting from the next line -----------------------
                              Code:
                              * Example generated by -dataex-. To install: ssc install dataex
                              clear
                              input double(household_id longitude latitude age) byte(gender education) int village byte(wvs_happiness wvs_life_sat) float(treat spillover purecontrol cons_food cons_social cons_total hh_size children mis a sum)
                               186779224 34.3845287 -.1212743  28 2  8 15 3  5 0 1 0  5238.519  4650 24762.477  5 4 . .a 0
                               188387876    34.3882 -.1299187   . .  . 15 .  . 1 0 0         0     0         0  . 0 . .a 0
                               176895075 34.3902187   -.11984  25 2  3 15 3  8 1 0 0 1396.3934   900 12429.285  4 1 . .a 0
                              1482168531 34.3942218 -.1150712  34 2 13 16 3  5 0 1 0  1325.634  5800  8441.393  7 5 . .a 0
                              1065171387  34.397085 -.1180231  39 2 11 16 3  4 0 1 0 2084.4536  2940 15293.356  5 3 . .a 0
                              1068171384 34.3941353 -.1178486  51 1 14 16 .  . 1 0 0 2352.9507  3920 19689.855  9 7 . .a 0
                              1068169977 34.4059633 -.1090553  45 1  2 16 .  . 1 0 0   564.153     0  4397.857  2 1 . .a 0
                              1083168487   34.39193    -.1148  49 2  7 16 3  3 0 1 0  1606.776  2000  8752.619  1 0 . .a 0
                              1477171400   34.40493 -.1091743  54 2  1 16 3  4 1 0 0  3446.541  3150 22150.404  5 1 . .a 0
                              2187180012 34.3941162 -.1175743  46 1 14 16 .  . 1 0 0 3564.6995   880 18490.238  1 0 . .a 0
                              2279180002 34.3977931   -.11823  34 1  8 16 3  3 1 0 0 1321.8306  6600  9368.928  3 1 . .a 0
                              1069167047   34.39821 -.1175243  34 2 10 16 3  3 0 1 0 2132.7595  1800 12962.738  6 4 . .a 0
                              1269168481   34.39712   -.11806  29 2 12 16 3  4 0 1 0   926.153  3700  7276.381  4 1 . .a 0
                              1274167050    34.3974 -.1174687  28 2  8 16 3  5 1 0 0 2182.5247  1900 14891.237  6 4 . .a 0
                              2774806415   34.37858 -.0772906  62 1 12 19 3  1 0 0 1  1862.377  2460 10829.643  6 4 . .a 0
                              1371810744 34.3713606 -.0821631  63 1  1 19 3 10 0 0 1   3269.18  2800 17952.617 11 6 . .a 0
                              2787804992   34.37146   -.08363  26 1 10 19 3  2 0 0 1 1375.6066   680  9340.797  3 0 . .a 0
                              2780804969 34.3718662   -.07601  40 2  8 19 3  2 0 0 1 2455.3826  3180 13706.785  6 3 . .a 0
                              1365804977   34.36824   -.08263  50 2  9 19 3  4 0 0 1 1318.6885  2000  8329.881  3 1 . .a 0
                              1384807882   34.37843   -.07741  47 1 12 19 3  3 0 0 1 1510.6558  1600   8643.81  4 2 . .a 0
                              1467110895   34.40011   -.07122  41 2 10 22 4  2 1 0 0 1140.3005 20500  7344.286  5 3 . .a 0
                              1469108012   34.40382  -.070027  40 2  1 22 3  3 0 1 0  254.7541   900 2011.6666  4 2 . .a 0
                              1480109461   34.39867   -.07259 100 2  1 22 3  1 1 0 0  173.6885    80  6017.262  1 0 . .a 0
                              1480110937   34.40218   -.07332  40 2  1 22 3  4 0 1 0 1014.4536  1350  5802.619  6 3 . .a 0
                              1565105175  34.395265 -.0679237  28 2  7 22 4  3 1 0 0 2665.9126  3460 18054.262  6 3 . .a 0
                              1271105154   34.39992    -.0691  68 1  1 22 4  3 1 0 0 2376.0437  3460 14856.928  5 2 . .a 0
                              1380105136  34.400795   -.07157  74 2  1 22 4  4 1 0 0 1190.6284  2200  9651.071  2 1 . .a 0
                              1277109456 34.4000162  -.076845  49 2 14 22 3  1 0 1 0  675.1093  3000  4486.548  3 1 . .a 0
                              1574118114   34.39657 -.0671247  49 2  9 22 3  1 0 1 0 1117.7869  2300  6492.023  3 2 . .a 0
                              1382155535   34.40377   -.07469  47 2  9 22 4  3 1 0 0  638.5519 15800  9673.929  4 2 . .a 0
                              1487106607   34.39525   -.06807  28 1 10 22 4  7 1 0 0   1638.88  3200 12811.834  4 2 . .a 0
                              1368105162   34.39917   -.06905  45 1  7 22 3  1 0 1 0  4348.918 10090 26298.023 10 6 . .a 0
                              1183105125 34.3988837   -.06803   . .  . 22 .  . 1 0 0         0     0         0  . 0 . .a 0
                              1983108037 34.3990875   -.07502  19 2 10 22 3  1 0 1 0 1603.0547  1780 10876.405  4 2 . .a 0
                              1282116698   34.39737   -.06794  26 2 11 22 2  2 0 1 0  418.2514   600  2709.881  6 3 . .a 0
                              1767105128   34.39609   -.06691  64 2  1 22 4  1 0 1 0  1722.022  2700  15236.43  5 2 . .a 0
                              1767115250   34.40477   -.07307  45 1  9 23 3  8 1 0 0 2124.5083  1450 11711.786  6 2 . .a 0
                              1776110917 34.4099525 -.0712862  24 2  8 23 3  1 0 1 0 539.21313   450  3169.762  5 2 . .a 0
                              1074106605   34.40705  -.073625  38 2  5 23 4 10 0 1 0  825.8142   850   4885.69  2 0 . .a 0
                              1282110918   34.40752    -.0709  77 1 12 23 .  . 1 0 0  346.9126   520  3780.714  1 0 . .a 0
                              1365116643 34.4086281 -.0734112  44 2  8 23 3  5 0 1 0  806.9945  1300   4938.69  3 2 . .a 0
                              1777110891   34.40723   -.07142  30 2  9 23 3  1 0 1 0  869.8361  1150      4885  5 2 . .a 0
                              1171106589  34.403498 -.0753793  64 1  8 23 .  . 0 1 0   819.317   700  6030.714  4 2 . .a 0
                              1767119530 34.4122687 -.0728312  71 1  6 23 .  . 1 0 0  975.0273   620  6618.452  3 2 . .a 0
                              1377106581 34.4032975 -.0754262  23 2  8 23 3  1 1 0 0  761.1475  4100  4839.345  3 1 . .a 0
                              1069109443 34.4060775 -.0778162  44 1 14 23 .  . 1 0 0 2112.1584  2770 12450.785  7 3 . .a 0
                              1774118136 34.4120075 -.0732718   . .  . 23 .  . 1 0 0         0     0         0  . 0 . .a 0
                              1382109449   34.40863 -.0734812  27 2  9 23 3  4 1 0 0  386.3388  2100 3348.3335  5 3 . .a 0
                              1083106560 34.4072405 -.0733405  23 2  9 23 3  2 1 0 0 2161.8252  1000 12109.547  5 2 . .a 0
                              1790106561 34.4091375 -.0737475  40 2  8 23 4  1 1 0 0 2333.2515  1300 13262.976  7 4 . .a 0
                              1165109490 34.4095152 -.0732652   . .  . 23 .  . 1 0 0         0     0         0  . 0 . .a 0
                              1077108027  34.402836  -.078717  62 1 10 23 2  9 0 1 0  4030.869  3800  27509.74  6 3 . .a 0
                              1782108027  34.404424 -.0758026  43 2  6 23 3  1 0 1 0  766.0164  2900  5385.976  5 1 . .a 0
                              1767106617 34.4061333 -.0736666  44 2  6 23 4  1 1 0 0  398.0874   550  2967.857  5 2 . .a 0
                              1177116655  34.410055 -.0745375  45 1 10 23 .  . 0 1 0   994.317   300  8640.715  9 6 . .a 0
                              1165108033 34.4069335 -.0749035  52 1 10 23 .  . 0 1 0 4030.7104  1840 26643.215  1 0 . .a 0
                              1877792037 34.3767787   -.11697  62 2  1 31 2  1 0 0 1  809.0219   580  3973.357  1 0 . .a 0
                              1867786295   34.37112 -.1204525  27 2  7 31 3  5 0 0 1 1653.6613  1350  9011.071  5 2 . .a 0
                              1282786251 34.3686486  -.124246  30 2  9 31 1  1 0 0 1      1805  3040  10326.31  5 2 . .a 0
                              1274792021 34.3776768 -.1227962  43 1 10 31 .  . 0 0 1  838.8525  1730    9382.5  1 0 . .a 0
                              1877786258 34.3712187 -.1204731  20 2 10 31 3  6 0 0 1  468.8142  1280  2626.191  2 0 . .a 0
                              1276790601 34.3743631 -.1254481  26 1 10 31 2  4 0 0 1  339.1257  9240 2835.9524  4 1 . .a 0
                              1874866883 34.4036766   -.09667  32 2 10 34 3  3 0 0 1 1429.3716  3700  9475.643  5 4 . .a 0
                              2965868329 34.3959813   -.09855  50 2  8 34 3  1 0 0 1  615.1913  1900  3617.143  3 2 . .a 0
                              1869884171   34.40338 -.0951013  22 2 10 34 4  2 0 0 1  1458.213  1440  8300.311  5 2 . .a 0
                              2972866928 34.3973913 -.0938186  48 2  1 34 4  1 0 0 1 2035.7377  3000 11543.167  8 4 . .a 0
                              1880866886 34.4012737 -.0952693  28 2 12 34 4  7 0 0 1 1814.2078  2330  9807.262  5 2 . .a 0
                              2989868361   34.39312   -.10067  30 2  9 34 4  3 0 0 1 1039.5629  1400   5941.19  4 2 . .a 0
                               148357638   34.43395   -.16255  39 2  8 46 4  4 1 0 0  3547.814 11440     23145  9 8 . .a 0
                               198238913 34.4353325   -.16102  45 1  9 46 3  5 0 1 0 3131.0874  1650  14666.88  6 3 . .a 0
                               167247544   34.43325   -.15991  34 2  9 46 2  2 0 1 0  1276.694  4700  8790.119  6 4 . .a 0
                               196738918  34.431874  -.163928  91 2  1 46 4  5 0 1 0 1711.2295  1970  9290.238  4 0 . .a 0
                               147638911   34.43312   -.16522  51 2  1 46 2  1 0 1 0  455.2131  1225  2918.226  2 1 . .a 0
                               177147534 34.4320293 -.1635793  40 1 10 46 .  . 0 1 0  2143.552 15240     15925  2 1 . .a 0
                               126648960 34.4348593   -.16297  27 2  9 46 4  7 1 0 0 1605.6284  2400 12235.952  5 3 . .a 0
                               167738914   34.43498   -.16137  40 2  8 46 2  1 1 0 0 1593.1147  2100 10369.762  4 3 . .a 0
                               177647535 34.4329218   -.15842  20 2 10 46 3  5 1 0 0  970.7104 16500 10402.857  5 2 . .a 0
                               108054731   34.43598   -.16221  25 2 10 46 3  4 1 0 0 1623.7705 15800 13611.667  5 4 . .a 0
                               177759050   34.43701   -.16194  36 2  9 46 3  2 0 1 0  768.6284  1900  8547.357  1 0 . .a 0
                               127638922   34.43276   -.16288  24 2 14 46 3  8 1 0 0   726.082   120  3622.476  4 1 . .a 0
                               168738906   34.43198   -.16545  19 2  9 46 3  1 0 1 0  417.1858   740  3219.405  3 0 . .a 0
                               126838932   34.43325   -.16264  76 2  1 46 4  3 1 0 0  476.4208   720  3006.667  3 2 . .a 0
                               127447548   34.43375   -.16012  23 1 10 46 .  . 0 1 0 1243.7705  2240  7585.952  4 0 . .a 0
                               106854772   34.43597 -.1622231  24 1 13 46 .  . 0 1 0  676.3224   650  3573.167  4 1 . .a 0
                               177746084   34.43222   -.16071  42 1 10 46 3  5 1 0 0  1927.705  6260 11737.382  6 4 . .a 0
                               176757616   34.43286   -.16303  26 2 10 46 4  7 1 0 0  3319.131 16000  22090.26  5 2 . .a 0
                               177454773 34.4364412 -.1634856  32 2 10 46 3 10 1 0 0 1357.3826  1010   7721.81  5 3 . .a 0
                               107756208 34.4318686  -.174838  28 2 10 46 4 10 1 0 0  5510.104  8400  34691.64  8 4 . .a 0
                               176850446   34.43527   -.15804  22 2 10 46 3  5 1 0 0  775.6995     0  7685.833  4 1 . .a 0
                               126751863   34.43608   -.16197  20 2  8 46 2  4 1 0 0 2203.2515 17340 15378.215  5 2 . .a 0
                               197757609   34.43134    -.1755  55 2  1 46 .  . 0 1 0  1690.328  5900 11930.834  3 1 . .a 0
                               126750457   34.43365   -.16018  23 2  6 46 3  5 1 0 0 1876.5847  5830 15349.048  4 1 . .a 0
                              1274158432   34.43799   -.16527  57 1  9 47 .  . 0 1 0 1993.8853  3000  11426.31  3 0 . .a 0
                              1977158422 34.4381146   -.16503  19 2  7 47 3  2 0 1 0  920.3279   780  8496.666  4 1 . .a 0
                              1272156978   34.43257 -.1698729  26 2 10 47 4  2 0 1 0 1564.0874  1900 10573.285  5 2 . .a 0
                              1567155577   34.43619   -.17187  38 2  7 47 2  1 0 1 0 1390.4645  8600  9164.166  3 1 . .a 0
                              1074161281 34.4317275 -.1746293  32 1 10 47 4  3 1 0 0  2832.787 23600 18513.691  7 3 . .a 0
                              1467151233   34.43237   -.17482  30 1  7 47 .  . 0 1 0 1025.6613  1620  6203.953  5 2 . .a 0
                              1482151221    34.4319   -.17397  32 2 10 47 3  3 1 0 0 3721.6284  1140 26007.334 10 7 . .a 0
                              1782151256   34.43545   -.17373  35 2 10 47 3  2 0 1 0 3040.9016  3060  23186.19  8 5 . .a 0
                              end
                              label values household_id HouseholdId
                              label values longitude dkrf
                              label values latitude dkrf
                              label values age dkrf
                              label values wvs_life_sat dkrf
                              label values gender gender
                              label def gender 1 "male", modify
                              label def gender 2 "female", modify
                              label values education highestedlevel
                              label def highestedlevel 1 "Never attended school", modify
                              label def highestedlevel 2 "Pre-school", modify
                              label def highestedlevel 3 "Standard 1", modify
                              label def highestedlevel 5 "Standard 3", modify
                              label def highestedlevel 6 "Standard 4", modify
                              label def highestedlevel 7 "Standard 5", modify
                              label def highestedlevel 8 "Standard 6", modify
                              label def highestedlevel 9 "Standard 7", modify
                              label def highestedlevel 10 "Standard 8", modify
                              label def highestedlevel 11 "Form 1", modify
                              label def highestedlevel 12 "Form 2", modify
                              label def highestedlevel 13 "Form 3", modify
                              label def highestedlevel 14 "Form 4", modify
                              label values village villagename
                              label def villagename 15 "Nyangoe", modify
                              label def villagename 16 "Miyare", modify
                              label def villagename 19 "Ochinya", modify
                              label def villagename 22 "Chore", modify
                              label def villagename 23 "Lumba", modify
                              label def villagename 31 "Konjiko", modify
                              label def villagename 34 "Konge", modify
                              label def villagename 46 "Pala", modify
                              label def villagename 47 "Asino", modify
                              label values wvs_happiness wvs3scale
                              label def wvs3scale 1 "Very happy", modify
                              label def wvs3scale 2 "Quite happy", modify
                              label def wvs3scale 3 "Not very happy", modify
                              label def wvs3scale 4 "Not at all happy", modify
                              label values treat Treat
                              label values spillover Spills
                              label values purecontrol Control
                              label values cons_food Food
                              label values cons_social Social
                              label values cons_total Total
                              label values hh_size Size
                              label values children Children
                              ------------------ copy up to and including the previous line ------------------

                              Listed 100 out of 949 observations
                              Use the count() option to list more

                              In the attached dataset cashtransfers.dta, each observation is a household. The variable treat denotes whether a household received a transfer. The variable spillover denotes whether a household was a control household living in a treatment village. The variable purecontrol denotes whether a household was a control household in a control village.

                              1. Please explain briefly why randomization of treatment is important to this study. a. Using the data in cashtransfers.dta, determine whether the household-level randomization was successful. 2. The primary outcome variables for the project at hand are 3 measures of household consumption, and 2 measures of psychological wellbeing (taken from an interview with the head of household.)
                              a. The measures of psychological wellbeing are questions taken from the World Values Survey. How would you transform these variables so that they can be more readily interpreted?
                              b. Evaluate the effect of the cash transfers on each outcome.
                              c. Evaluate the effects of the cash transfers on households in treatment villages who were not selected to receive the transfer.
                              d. Create one table reporting these results.

                              Comment

                              Working...
                              X