提问者:小点点

引导ICC在R


作为R新手,我很难引导ICC输出。我首先设法使用ICC包计算“正常”ICC,没有任何问题(ICCbare(subject,variable,ICC)),但当我试图得到一些自举估计值时,情况变得更糟。。。

我一开始是

icc_boot<-function(icc, i)ICCbare(subject [i], variable [i], icc)

并在引导程序中输入icc_引导,如下所示:

testicc<-boot(icc, icc_boot, 1000)

然而,我得到一个错误消息说"未定义的列选择",我哪里出错了?

这是我的一个小数据输出


共1个答案

匿名用户

在定义引导函数的统计信息时,传递的第一个参数将始终是原始数据。第二个将是指数的向量...'看到没?开机

一个例子:

library(ICC)
library(MASS)
library(boot)

# Data
Sigma <- matrix(c(10,3,3,2),2,2)
df <- data.frame(mvrnorm(n=20, rep(0, 2), Sigma))

#ICC on data
m.df <- reshape(df , dir = "long" , varying = list(1:2))
ICCbare(id , X1 , data = m.df)

# Bootstrap function ---------------------------------------------
boot.fun <- function(dat , i) {
  newdf <- dat[i , ]
  m.newdf <- reshape(newdf , dir = "long" , varying = list(1:2) , new.row.names=1:40)  
  ICCbare(id , X1 , data = m.newdf)$ICC
}

boo1 <- boot(df , boot.fun , 2000)
boot.ci(boo1)

编辑:根据您的数据,这将是我的方法。希望有人能提出更好的解决方案。由于您的数据已经是长格式,我会将其转换为宽格式,以便在重新采样时保留主题内的相关性(必须有一种方法以长格式采样)。

If your data is df.

#ICC on your data
ICCbare(subject_id , interval_0_epochs , data = df)

# Reshape your data to wide - preserve wothin subject correlation when resampling
df$time <- ave(df$subject_id, list(df$subject_id), FUN=seq_along)
w.df <- reshape(df , timevar = "time" , idvar = "subject_id"  , direction = "wide" )  

# Quick check ---------------------------------------------------------
l.df <- reshape(w.df , direction = "long")
# Define new grouping factor for repeat id's when resamples (later)
l.df$grp <- 1:27
ICCbare(grp , interval_0_epochs.1 , data = l.df)$ICC #same as before

# Bootstrap function ---------------------------------------------
boot.fun <- function(dat , i) {
 newdf <- dat[i , ]
 m.newdf <- reshape(newdf , dir = "long", new.row.names = seq((ncol(dat)-1)*nrow(dat))) 
 m.newdf$grp <- seq(nrow(dat))
 ICCbare(grp , interval_0_epochs.1 , data = m.newdf)$ICC
}

boo1 <- boot(w.df , boot.fun , 2000)
boot.ci(boo1)