[R] Calculating confidence limits for the difference between

(Ted Harding) Ted.Harding at manchester.ac.uk
Wed Oct 22 20:05:50 CEST 2008


On 22-Oct-08 17:09:27, Dennis Fisher wrote:
> Colleagues,
> I am working on a problem at the edge of my knowledge of statistics.
> 
> Basically, I have values for two groups - I am trying to calculate the 
> 90% confidence limits for the difference between means.  I can  
> implement this without difficulty based on standard equations that are 
> available in stat textbooks: (difference between means) / (pooled  
> standard error)
> 
> My problem arises when I try to correct for the value of a covariate.  
> I can do the ANOVA with the covariate as a factor.  But, I don't know  
> how to use the output to obtain the confidence limits of the
> difference.
> 
> I suspect that several participants in this board have implemented
> code to so this.  I hope that someone is willing to share the code.

Mostly, we haven't, though a few of us once did (in coding lm()
and t.test()).

Assuming that you will adopt equal variances for the two groups
(as in standard ANOVA), you can approach this in at least two
different ways. Both illustrated below, for comparison.

## Make up some data, and a Group indicator
  set.seed(213243)
  X1 <- rnorm(15) ; X2 <- 0.1+rnorm(25) ; X <- c(X1,X2)
  Group <- factor(c(rep(0,length(X1)),rep(1,length(X2))))
  mean(X1)
# [1] 0.1108255
  mean(X2)
# [1] 0.1002999
  mean(X2)-mean(X1)
# [1] -0.01052560

  summary(lm(X ~ Group))$coef
#                Estimate Std. Error     t value  Pr(>|t|)
# (Intercept)  0.11082552  0.2543404  0.43573696 0.6654929
# Group1      -0.01052560  0.3217180 -0.03271686 0.9740716
## Note that the "Group" coefficient is the difference of means here

  Diff<-summary(lm(X ~ Group))$coef[2,1] ## -0.01052560
  SE  <-summary(lm(X ~ Group))$coef[2,2] ##  0.3217180
  Diff + qt(0.975,38)*c(-1,1)*SE ## d.f. = (15-1)+(25-1)
# [1] -0.6618097  0.6407585
## 95% confidence interval calculated from output of lm() above

  t.test(X2,X1,var.equal=TRUE)
#         Two Sample t-test
# data:  X2 and X1
# t = -0.0327, df = 38, p-value = 0.974
# alternative hypothesis: true difference in means is not equal to 0 
# 95 percent confidence interval:
#  -0.6618097  0.6407585 
# sample estimates:
# mean of x mean of y 
# 0.1002999 0.1108255

## The 95% CI fron t.test() is the same as previously calulculated
## from the ouput of lm(). But t.test() does not directly output
## the difference of means.

Hoping this helps
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 22-Oct-08                                       Time: 19:05:47
------------------------------ XFMail ------------------------------



More information about the R-help mailing list