Skip to content

Instantly share code, notes, and snippets.

@peterdalle
Created September 4, 2017 17:32
Show Gist options
  • Save peterdalle/7bce0bbebc13c36180fbbac1353a5353 to your computer and use it in GitHub Desktop.
Save peterdalle/7bce0bbebc13c36180fbbac1353a5353 to your computer and use it in GitHub Desktop.
Calculate 95 % CI from ICC
# Code by Ashe: https://stats.stackexchange.com/questions/184767/how-do-i-calculate-the-confidence-interval-of-an-icc
#Make some mocked data
library(lme4)
library(reshape2)
set.seed(2024) #For the Bell Riots
id <- factor(seq(1, 15))
id.mu <- rnorm(15, 10, 5)
mydat <- NULL
for (a in 1:length(id)){
score <- rnorm(2, id.mu[a], 3)
id.fr <- data.frame(id=id[a], score1=score[1], score2=score[2])
mydat <- rbind(mydat, id.fr)
}
mydat <- melt(mydat, id.vars='id', value.name='score')
#Create function to calculate ICC from fitted model
calc.icc <- function(y) {
sumy <- summary(y)
(sumy$varcor$id[1]) / (sumy$varcor$id[1] + sumy$sigma^2)
}
#Fit the random effects model and calculate the ICC
mymod <- lmer(score ~ 1 + (1|id), data=mydat)
summary(mymod)
calc.icc(mymod)
#Calculate the bootstrap distribution
boot.icc <- bootMer(mymod, calc.icc, nsim=1000)
#Draw from the bootstrap distribution the usual 95% upper and lower confidence limits
quantile(boot.icc$t, c(0.025, 0.975))
@LizMansi
Copy link

Hi, I found your demo, but I'm having trouble getting it to work (the function, specifically) on my data. I need to calculate the 95%CI for ICC. This is how I'm calculating the ICC.

Empty model

library(lme4)
M0 <- glmer(outcome ~ ( 1 | pracid), data=master, family = "binomial")
summary(M0)

icc <- M0@theta[1]^2/ (M0@theta[1]^2 + (3.14159^2/3))
icc # Intraclass correlation coefficient.

Thanks a ton! It's been a challenge!
Liz

@peterdalle
Copy link
Author

Hi Liz,

I realize I haven't calculated that thing in seven years and the code above doesn't work anymore. Check out the Stack Overflow link (https://stats.stackexchange.com/questions/184767/how-do-i-calculate-the-confidence-interval-of-an-icc) and try to post the issue there.

All the best,
Peter

@LizMansi
Copy link

LizMansi commented Mar 28, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment