title: Margin of Error for Complex Sample in Python
body: <p>I have weighted STATA dataset of national survey (n=6342). The data has already been weighted, i.e. each respondent represents 4000 respondents on average.</p>
  
  <p>I am reading dataset with <code>pandas.read_stata</code> function. Basically, what I need to achieve is to extract data by each question with respected frequencies(%) along with Margin of Error for each frequency. </p>
  
  <p>I have written Python code to do it and it works perfectly fine with frequency itself, i.e. calculating sum of weighted value in each frequency and divide it by total weighted value sum. </p>
  
  <p>Pseudo-code looks like this:</p>
  
  <pre><code>   q_5 = dataset['q5'].unique()`
      frequencies = {}
      for value in q_5:
          variable = dataset[dataset['q5'] == value]
          freq = ((variable['indwt'].sum()/weights_sum)*100)
          freq = round(freq,0)
          frequencies.update({value : freq}) 
  </code></pre>
  
  <p>However, I cannot get the proper confidence intervals or margin of error since this is a complex sample. 
  I was advised to use R instead, but taking into consideration syntax learning curve, I would rather stick with Python. </p>
  
  <p>Is there any statistical package for Python that could calculate ME for complex sample?</p>