title: R - svyciprop not working with just two groups in a cluster
body: <p>I am analysing a survey of school data. We performed it in 167 wards of a region in Northern Tanzania. In some wards, we sampled one school, in others three schools. However, in almost all cases we sampled two schools per ward.</p>
  <p>The goal is to give a point estimate and a confidence interval (CI) for the prevalence of a disease in that school. Prevalence is recorded as a binary variable (1 if the kid has the disease, 0 if not). We sample 30 children per school.</p>
  <p>When I run the code I prepared I have an if clause for the case that there is just one school in the ward. If there are three schools the code functions perfectly. But if there are two schools, then svyciprop says that the lower CI is 0, the upper is NA. Why and how can I get around this?</p>
  <p>I present a mock code that recreates the data for one ward.</p>
  <pre><code>[1] set.seed(123)
  [2] schools &lt;- rep(c(1,2), 30)
  [3] prevalence &lt;- sample(c(0,1), 60, replace=T, prob = c(0.7, 0.3))
  [4] uniqueID &lt;- 1:60
  
  [5] df &lt;- data.frame(schools, prevalence, uniqueID)
  [6] df$fpc1 &lt;- 0.0001 # As if from an infinite population
  [7] df$fpc2 &lt;- 0.0001
  
  [8] t.clus1 &lt;- svydesign(id = ~factor(schools) + factor(uniqueID), data = df, fpc =~ fpc1 + fpc2)
  [9] svyciprop(formula =~ prevalence, design=t.clus1, method = &quot;logit&quot;, level = 0.95)
  [10] &gt;&gt;                   2.5% 97.5%
  [11] &gt;&gt; prevalence 0.3   0.00    NA
  </code></pre>
  <p>If you run the code, the last line will produce the correct mean (30% of the kids across both schools are sick) but the CIs are not functioning.</p>
  <p>The very same process with three schools works.</p>
  <pre><code>set.seed(123)
  schools &lt;- rep(c(1,2,3), 30)
  prevalence &lt;- sample(c(0,1), 90, replace=T, prob = c(0.7, 0.3))
  uniqueID &lt;- 1:90
  
  df &lt;- data.frame(schools, prevalence, uniqueID)
  df$fpc1 &lt;- 0.0001
  df$fpc2 &lt;- 0.0001
  
  t.clus1 &lt;- svydesign(id = ~factor(schools) + factor(uniqueID), data = df, fpc =~ fpc1 + fpc2)
  svyciprop(formula =~ prevalence, design=t.clus1, method = &quot;logit&quot;, level = 0.95)
  &gt;&gt;                     2.5% 97.5%
  &gt;&gt; prevalence 0.3111 0.0733  0.72
  </code></pre>
  <p><strong>#==========================# EDIT #==========================#</strong></p>
  <p>After more research, I tried to get to the same point with <code>svyglm</code> and <code>confint</code> but the results differ. Any idea on this?</p>
  <p>Going back to the first example (with two schools) and using the same <code>t.clus1</code> design as defined in line [8] I create a <code>svyglm</code> object called <code>mylogit</code> of the binomial family. Then I can extract the coefficients and the confidence interval. To get the value I need to invert the logit link.</p>
  <pre><code>mylogit &lt;- svyglm(prevalence ~ 1, design = t.clus1, family=binomial())
  summary(mylogit)
  
  exp(mylogit$coefficients) / (1 + exp(mylogit$coefficients))
  &gt;&gt; (Intercept) 
  &gt;&gt;    0.3
  exp(confint(mylogit)) / (1 + exp(confint(mylogit)))
  &gt;&gt;                 2.5 %    97.5 %
  &gt;&gt; (Intercept) 0.1694734 0.4305266
  </code></pre>
  <p>So know I am getting the same point estimate but a functioning CI. Is it robust?</p>
  <hr />
  <h1>Solved</h1>
  <p>The difference between the approach of using <code>svyciprop</code> and defining and <code>svyglm</code> object to use as an argument in <code>confint</code> is that <code>svyciprop</code> will directly input the degrees of freedom of the design while <code>confint</code> defaults to <code>ddf = Inf</code>. Once that is changed, both confidence intervals are alike, i.e., spanning the whole probability range.</p>