title: How are computed the confidence intervals of emmeans when adjusted with Tukey method?
body: <p>I have troubles to get back to the &quot;CL&quot; values given by the emmeans package.</p>
  <p>My first goal was to perform a kind of power study : I am working on plants and I want to see how much I can improve my experimental design for next season to have more significant differences between my genotypes. My genotypes are repeated and this is used as a blocking factor.
  I want to play with the number of repetitions (assuming that the means and the residual SE would remain the same). In the end I want to find a compromise between budget issues and statistical power.</p>
  <p>I was thinking to look at the confidence intervals (CIs), having in mind that if the CIs of two genotypes cover at least one common value, the two genotypes are not significantly different. So I want to see how much I can reduce the CIs when playing with the number of repetitions.</p>
  <p>My guess for the CIs calculations using Tukey method for multiple comparisons is :</p>
  <blockquote>
  <p>μi ± (σ/(2*√(ni)))*q</p>
  <p>with μi being the mean for the i-th genotype</p>
  <p>ni the number of observations for the i-th genotype,</p>
  <p>σ the residual standard error and</p>
  <p>q the studentized range statistics with t (the number of genotypes) and dfE (the degrees of freedom of the residual) as argument for α = 0.05</p>
  </blockquote>
  <p>Which is equivalent to :</p>
  <blockquote>
  <p>μi ± (SEi*q)/2</p>
  <p>with SEi being the standard error of the i-th genotype</p>
  </blockquote>
  <p>Here is a small example dataset with 6 genotypes and 3 repetitions and the code I ran :</p>
  <pre><code>library(emmeans)
  
  # import DF
  df &lt;- structure(list(Geno = structure(c(6L, 3L, 2L, 1L, 4L, 5L, 2L, 
                                       4L, 5L, 1L, 3L, 6L, 2L, 3L, 1L, 5L, 6L, 4L),
                                     .Label = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;),
                                     class = &quot;factor&quot;), variable = c(2.279628571, 3.925157143, 3.089, 2.26, 2.503,
                                                                     2.495114286, 2.867166667, 3.069238095, 3.884285714, 3.409595238, 
                                                                     3.710714286, 1.763142857, 2.865285714, 4.219214286, 3.263452381, 
                                                                     3.359428571, 2.335285714, 2.443), 
                   Rep = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), 
                                   .Label = c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;), class = &quot;factor&quot;)), .Names = c(&quot;Geno&quot;, &quot;variable&quot;, &quot;Rep&quot;),
              row.names = c(NA, 18L), class = &quot;data.frame&quot;)
  
  # Number of observation
  N &lt;- nrow(df)
  
  # Number of treatments
  t &lt;- nlevels(df$Geno)
  
  # Define the model
  model &lt;- lm(data=df, variable ~ Geno + Rep)
  
  # Compute means and confidence intervals
  E_means &lt;- as.data.frame(emmeans(model, pairwise~Geno)$emmean)
  
  # Extract Standard Errors
  se &lt;- E_means[,&quot;SE&quot;]
  
  # Studentized range statistic
  q &lt;- qtukey(0.95, nmeans=t, df=E_means[,&quot;df&quot;])
  
  expected_CI &lt;- se*q/2
  emmeans_CI &lt;- ((E_means[,&quot;upper.CL&quot;] - E_means[,&quot;lower.CL&quot;])/2)
  
  print(expected_CI)
  print(emmeans_CI)
  </code></pre>
  <p>Note that most of the time I am dealing with unbalanced data (so I have different SE for each genotype). I want first to make it clear for a simple (= balanced) case.</p>
  <p>expected_CI and emmeans_CI are always different for every test I have done so far (not very much different, but still different) so I guess I am not doing the calculation the same way than the emmeans package. So here is my question : how is it done in the emmeans package ?</p>
  <p>Any help would be much appreciated !</p>