1

I would like to get CI for a paired t-test using bootstrap in R. Unfortunately, I dont have privileges to install the MKinfer. Using MKinfer I would (like here: T-test with bootstrap in R):

  boot.t.test(
    x = iris["Petal.Length"],
    y = iris["Sepal.Length"],
    alternative = c("two.sided"),
    mu = 0,
    #paired = TRUE,
    conf.level = 0.95,
    R = 9999
  ) 

How would I do this for paired data with CI's and p-values not relying on MKinfer (relying on boot would be fine)?

Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62

1 Answers1

3

Here is an example using boot using R = 1000 bootstrap replicates

library(boot)
x <- iris$Petal.Length
y <- iris$Sepal.Length
change_in_mean <- function(df, indices) t.test(
    df[indices, 1], df[indices, 2], paired = TRUE, var.equal = FALSE)$estimate
model <- boot(
    data = cbind(x, y),
    statistic = change_in_mean,
    R = 1000)

We can calculate the confidence interval of the estimated change in the mean using boot.ci

boot.ci(model, type = "norm")
#BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
#Based on 1000 bootstrap replicates
#
#CALL :
#boot.ci(boot.out = model, type = "norm")
#
#Intervals :
#Level      Normal
#95%   (-2.262, -1.905 )
#Calculations and Intervals on Original Scale

Note that this is very close to the CI reported by t.test

t.test(x, y, paired = TRUE, var.equal = FALSE)
#
#   Paired t-test
#
#data:  x and y
#t = -22.813, df = 149, p-value < 2.2e-16
#alternative hypothesis: true difference in means is not equal to 0
#95 percent confidence interval:
# -2.265959 -1.904708
#sample estimates:
#mean of the differences
#              -2.085333
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68