Announcement

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

  • Consistency checks

    Hi All,
    I am using Stata 13 on Windows 7 and would like to implement some consistency checks, specifically skip patterns in a questionnaire. Below is an example of a code for data example
    Code:
    clear all
    input q1 q2 q3 q4 q5
    1 . 1 2 1
    1 . . . .
    2 . . . .
    2 . . 1 1
    1 1 1 1 1
    end
    list
    The check is, if q1=1, then q2-q5 can't all be missing. If q1=2, then q2-q5 should all be missing. In the example observations 2 and 4 aren't consistent. Hope am clear.
    Thanks in advance.

  • #2
    Sounds like egen rowmiss can help you with this:

    Code:
    egen missing2to5 = rowmiss(q2-q5)
    gen inconsistent= 1 if q1==1 &  missing2to5==4
    replace inconsistent=1 if q1==2 & missing2to5!=4
    Whether or not you can make these rules more generic will depend on the logic in further rules for whta you call inconsistent

    Comment


    • #3
      Less easy than Jorrit's solution, but more easy to generalize:

      Code:
      // input the data
      clear all
      input q1 q2 q3 q4 q5
      1 . 1 2 1
      1 . . . .
      2 . . . .
      2 . . 1 1
      1 1 1 1 1
      end
      list
      
      // perform the tests
      di as txt "{hline}"
      di as txt "check if at least one of q2-q5 is nonmissing when q1 == 1"
      gen byte problem = q1==1 & !( q2 < . | q3 < . | q4 < . | q5 < . )
      capture assert problem == 0
      if !_rc {
          di as txt "test passed"
      }
      else {
          di as err "test failed"
          list if problem
      }
      
      local vars q2 q3 q4 q5
      forvalues i = 2/5 {
          local nonmis q`i'
          local shouldbemiss : list vars - nonmis
          di as txt "{hline}"
          di as txt "check if `shouldbemiss' is missing when `nonmis' is nonmissing"
          local test "`nonmis' < . & ("
          gettoken first rest : shouldbemiss
          local test "`test' `first' < ."
          foreach var of local rest {
              local test "`test' | `var' <."
          }
          local test "`test' )"
          qui replace problem = `test'
          capture assert problem == 0
          if !_rc {
              di as txt "test passed" _n
          }
          else {
              di as err "test failed" _n
              list if problem
          }
      }
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Thanks Maarten Buis and Jorrit Gosens I really appreciate your inputs.

        Comment

        Working...
        X