How to calculate confidence interval for this mix of independent(different subjects) and dependent (data of same subject from two sessions) observations?

There are 12 subjects and the mean is calculated from these 12 subjects with data from two sessions, so 24 values go into the mean.

Any ideas?

p.s. R is the language of choice.



1 Answer 1


lme() in the nlme package can do this for you. We need to fit a basic linear mixed effects model, with a fixed effect for the intercept (i.e. mean), as well as a subject-level random effect for this intercept. Once the model is fit, the confidence intervals can be obtained with the lme method for the generic function intervals(). Here is an example:


n = 12

# Generate data table
ID = factor(rep(1:n, len=2*n))
value = 5 + rnorm(n)[as.numeric(ID)] + rnorm(2*n)

data = data.frame(ID, value)

# Plot data
dev.new(width=4, height=4)
qplot(ID, value, data=data, geom='boxplot')

# Fit linear mixed-effects model with an intercept term, as well as a random 
# subject-level effect on intercept
fit = lme(value ~ 1, random = ~ 1 | ID, data=data)


Raw data:

enter image description here

> summary(fit)
Linear mixed-effects model fit by REML
 Data: data 
       AIC      BIC    logLik
  80.90274 84.30923 -37.45137

Random effects:
 Formula: ~1 | ID
        (Intercept) Residual
StdDev:   0.9126617 0.871738

Fixed effects: value ~ 1 
               Value Std.Error DF  t-value p-value
(Intercept) 5.423818 0.3179249 12 17.06006       0

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-1.29134118 -0.78398894 -0.09942166  0.66779227  1.48250943 

Number of Observations: 24
Number of Groups: 12 
> intervals(fit)
Approximate 95% confidence intervals

 Fixed effects:
               lower     est.    upper
(Intercept) 4.731119 5.423818 6.116517
[1] "Fixed effects:"

 Random Effects:
  Level: ID 
                    lower      est.   upper
sd((Intercept)) 0.4827668 0.9126617 1.72537

 Within-group standard error:
    lower      est.     upper 
0.5841779 0.8717380 1.3008490 
  • $\begingroup$ Thanks! just to make sure. I am looking for a confidence interval for the mean of the 24 values from 12 subjects (over two sessions). Not for confidence intervals for each subject. $\endgroup$
    – Adam SA
    Oct 18, 2011 at 22:00
  • $\begingroup$ Correct. In the intervals output, it is (Intercept) under "Fixed effects". You can see that it matches the value of 5 that I chose when the example data was simulated. Same goes for the subject-level random effect and the within-group standard error (both 1, as per the default of rnorm). $\endgroup$
    – John Colby
    Oct 18, 2011 at 22:03
  • $\begingroup$ Thanks! I was a bit confused by the figure placed at the end. $\endgroup$
    – Adam SA
    Oct 18, 2011 at 22:12
  • $\begingroup$ @AdamSA Good point. Moved it. :) $\endgroup$
    – John Colby
    Oct 18, 2011 at 22:18
  • $\begingroup$ Thanks! I was really after the 'Std.Error' so that I can feed it to a plotCI. I figured out how to extract it, and now have to figure out how to make this work across the levels of another variable, so I am trying to make this work with an aggregate of a table. $\endgroup$
    – Adam SA
    Oct 18, 2011 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.