Announcement

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

  • Analysisg multiple response variables (dhs data)

    Hello, i am trying to combine multiple response variables into one variable.

    Case id m15_1 m15_2 m15_3 m15_4 m15_5 m15_6
    1 11 12 31 96 - -
    2 36 22 22 22 22 22
    3 31 31 21 21 22 -
    4 36 - - - - -
    .
    .
    8500 13 13 22 - - -
    m15 stands for Place of delivery and m15_1 is last delivery, m15_2 is second last delivery, m15_3 is third last delivery and so on.
    The codes 11- respondents home, 12-other home, 13 - Tba, 22- govt hosp, 21- other gov't hosp, 31-private hosp, 36-other private hosp, 96- other
    What i would like to do is to be able to combine all the variables into one m15 i.e place of delivery(all occurences).
    Please help me because i have already tried generate - replace OR recode OR egen concat()

  • #2
    What would combination do in your example? How would you use the result?

    Comment


    • #3
      hello nick, i am trying to find out the total number of births disaggregated by place of birth in uganda using dhs data. I would like to have one variable lets call it m15 that will have all the births as per the survey

      Comment


      • #4
        i would have a table like this
        Place of Birth Number of deliveries(m15)
        respondents home 1700
        other home 848
        Tba 90
        govt hosp 3000
        private hosp. etc 1500

        Comment


        • #5
          I don't understand yet what you want. The display in #1 looks like a Stata dataset, but that in #4 doesn't.

          Given your data, you could for example concatenate the non-missing values into a string.

          Code:
          clear 
          input Case_id    m15_1    m15_2    m15_3    m15_4    m15_5    m15_6
          1    11    12    31    96    .    .
          2    36    22    22    22    22    22
          3    31    31    21    21    22    .
          4    36    .    .    .    .    .
          end 
          egen m15 = concat(m15_*), p(" ") 
          replace m15 = substr(m15, 1, strpos(m15, ".") - 2) if strpos(m15, ".") 
          list Case_id m15 
          
               +-----------------------------+
               | Case_id                 m15 |
               |-----------------------------|
            1. |       1         11 12 31 96 |
            2. |       2   36 22 22 22 22 22 |
            3. |       3      31 31 21 21 22 |
            4. |       4                  36 |
               +-----------------------------+
          Is that what you want? If not, say what the values in the new variable look like.

          Please note the use of CODE delimiters to show code and data listings. Tables such as you used need surgery before they can be read into Stata.

          http://www.statalist.org/forums/help#stata explains how to teach yourself. It takes about two minutes to learn.

          Comment


          • #6
            i want to have an output table like the one in #4 . sorry about the tables. i shall read the info in the link.

            Comment


            • #7
              I think I see now. For that a new variable won't help at all. I guess you need something like this

              Code:
              . tabm m15_*, oneway
              
                   values |      Freq.     Percent        Cum.
              ------------+-----------------------------------
                       11 |          1        6.25        6.25
                       12 |          1        6.25       12.50
                       21 |          2       12.50       25.00
                       22 |          6       37.50       62.50
                       31 |          3       18.75       81.25
                       36 |          2       12.50       93.75
                       96 |          1        6.25      100.00
              ------------+-----------------------------------
                    Total |         16      100.00
              Here tabm is from SSC, as part of the tab_chi package, except that you can only do this with the version below, which should be copied and pasted into a file under PLUS along your adopath (Note that the problem with tabs is agreeing on how many spaces they represent: my code is less untidy than it may seem.)

              Code:
              *! 2.2.0 NJC 31 March 2016
              *! 2.1.0 NJC 25 July 2015
              * 2.0.0 NJC 1 November 2010
              * 1.4.0 NJC 4 November 2005
              * 1.3.1 NJC 11 December 2002
              * 1.3.0 NJC 10 December 2002
              * 1.2.0 NJC 5 February 1999
              * 1.0.0 NJC 22 December 1998
              program tabm, byable(recall)
                      version 8.2
                      syntax varlist(min=2) [if] [in] [fw aw iw] ///
                  [, ONEway transpose Valuelabel(string) MISSing replace *]
              
                  marksample touse, novarlist
              
                  if "`replace'" != "" & _by() {
                      di as err "replace not allowed with by:"
                      exit 198
                  }
              
                  qui count if `touse'
                  if r(N) == 0 error 2000
              
                      if "`exp'" != "" {
                              tempvar wt
                              gen `wt' `exp'
                              local w "[`weight' = `wt']"
                      }
              
                      capture confirm string variable `: word 1 of `varlist''
                      local strOK = _rc == 0
                  local j = 1
              
                      foreach v of local varlist {
                              capture confirm string variable `v'
                              if (`strOK' & _rc == 0) | (!`strOK' & _rc) {
                                      local OKlist `OKlist' `v'
                                      local slist `slist' `v' `wt'
                                      local lbl`j' : variable label `v'
                                      if `"`lbl`j''"' == "" local lbl`j' "`v'"
                          local ++j
                              }
                              else local badlist `badlist' `v'  
              
                      }
              
                      if "`badlist'" != "" {
                      di _n "{res}`badlist' {txt}different type, so excluded"
                  }
              
                  local nvars : word count `OKlist'
                  // normal tabulation, no need to stack
                      if `nvars' == 1 {
                              tab `OKlist' `w', `missing' `options'
                              exit 0
                      }
              
                      if "`vallbl'" == "" {
                              local 1 : word 1 of `slist'
                              local vallbl : value label `1'
                      }
                  // insurance policy
                      if "`vallbl'" != "" {
                              tempfile flabels
                              qui label save `vallbl' using `"`flabels'"'
                      }
              
                      preserve
                      tempvar data
                      stack `slist' if `touse', into(`data' `wt') clear
                      label var _stack "variable"
                      label var `data' "values"
                      forval i = 1 / `nvars' {
                              label def _stack `i' `"`lbl`i''"', add
                      }
                      label val _stack _stack
              
                      if "`vallbl'" != "" {
                              if `strOK' di _n as txt "may not label strings"
                      else {
                                      capture label list `vallbl'
                                      if _rc run `flabels'
                          label val `data' `vallbl'
                              }
                      }
              
                  if "`oneway'" != "" {
                      tab `data' `w', `missing' `options'
                  }
                  else {
                          if "`transpose'" == "" {
                          tab _stack `data' `w', `missing' `options'
                      }
                      else tab `data' _stack `w' , `missing' `options'
                  }
              
                  if "`replace'" != "" {
                      clonevar _values = `data'
                      if "`w'" != "" {
                          clonevar _weight = `wt'
                          label var _weight "weights"
                      }
                      restore, not
                  }
              end
              Last edited by Nick Cox; 31 Mar 2016, 04:38.

              Comment


              • #8
                Thank you very much. It gives me what i am looking for. i later tried to use svy: tabm m15_* but it didnt work so i used tabm m15_* [iweight=weight] and it worked. Thank you very much!

                Comment


                • #9
                  the oneway option however was rejected by Stata

                  Comment


                  • #10
                    Already explained in #7

                    except that you can only do this with the version below, which should be copied and pasted into a file under PLUS along your adopath

                    Comment


                    • #11
                      I realised the oneway option wasn't working because the data was weighted but it is okay with unweighted data. Is it possible to use the oneway and twoway options with weighted data? Thanks

                      Comment


                      • #12
                        Weights are acceptable for both kinds of table.

                        Comment


                        • #13
                          how do i use the twoway option with tabm

                          Comment


                          • #14
                            There is no twoway option. It is just the default.

                            I've sent revised code and help file to Kit Baum for inclusion on SSC.

                            The code was made public above (#7).

                            Here is the new help file with examples you can run.

                            -------------------------------------------------------------------------------------------
                            help for tabm
                            -------------------------------------------------------------------------------------------

                            Tabulation of multiple variables

                            tabm varlist [weight] [if exp] [in range] [, missing oneway replace transpose
                            valuelabel(lblname) tabulate_options]

                            by ... : may be used with tabm: see help by.

                            fweights, aweights and iweights are allowed with tabm; see help weights.


                            Description

                            tabm tabulates varlist, containing two or more comparable variables. By default a
                            combined two-way table is shown of variables in varlist by values in varlist. Either
                            all variables should be numeric, or all variables should be string: the type of the
                            first variable named is taken to indicate the user's intentions. By default also,
                            variables are listed in the rows of the table and values in the columns. Optionally,
                            two-way tables may be transposed.

                            Optionally, a one-way table may be shown of values in varlist pooled across all
                            variables.


                            Remarks

                            For further discussion of handling multiple responses in Stata, see Cox and Kohler
                            (2003) and Jann (2005).


                            Options

                            missing specifies that observations with missing values are to be included.

                            oneway specifies display of a one-way table.

                            replace specifies that the tabulated data overwrite the data in question. The new
                            variables will be _stack, indexing variables, _values, the distinct values of
                            varlist, and (if weights are specified) _weight, the weights. replace may not be
                            specified with by:.

                            transpose transposes rows and columns as compared with the default. This option may
                            be needed if there are too many values to show as columns.

                            valuelabel(lblname) specifies a value label name to be used in tabulation. The
                            default is that any value label associated with the first numeric variable in
                            varlist will be used. This option will be ignored in tabulation of string
                            variables.

                            tabulate_options are options allowed with tabulate for two-way or one-way tables.


                            Examples

                            . clear
                            . set obs 100
                            . forval j = 1/7 {
                            . gen y`j' = floor(10 * runiform()^`j')
                            . }
                            . tabm y*
                            . tabm y*, transpose
                            . tabm y*, oneway



                            Author

                            Nicholas J. Cox, Durham University, U.K.
                            [email protected]


                            Acknowledgments

                            Lee Sieswerda suggested making tabm byable and helped in testing. Svend Juul gave a
                            careful analysis of how missing values were not being supported properly.


                            References

                            Cox, N.J. and Kohler, U. 2003. On structure and shape: the case of multiple
                            responses. Stata Journal 3: 81-99.

                            Jann, B. 2005. Tabulation of multiple responses. Stata Journal 5: 92-122.


                            Also see

                            On-line: help for tabulate; mrtab (if installed)

                            Comment


                            • #15
                              The revised version of tabm is now on SSC. See http://www.statalist.org/forums/foru...updated-on-ssc

                              Comment

                              Working...
                              X