1
$\begingroup$

I would like to compare AUCs for two treatments using the boot package in R. However, something wrong with my R code and I don't know how to fix it. Can someone please give me a hint? many thanks in advance

require(boot)
#Create Data
set.seed(123)
N = 150
y1 = rnorm(150)
y2 = rnorm(150,1,0.2)
y3 = rnorm(150,2,0.5)
time = rep(1:3, each = 50)
trt = rep(rep(0:1, each = 25), times=3)
Data = data.frame(trt,time,y1,y2,y3)

#Trapezoid Rule
trap.rule = function(x,y) sum(diff(x)*(y[-1]+y[-length(y)]))/2

# Sum y1-y3 for each trt at each time point
SUM = function(data){
     set1 = subset(x = data, data$time==1)
     set2 = subset(x = data, data$time==2)
     set3 = subset(x = data, data$time==3)
     s1 = by(set1[,c("y1","y2","y3")], set1$trt, sum)
     s2 = by(set1[,c("y1","y2","y3")], set2$trt, sum)
     s3 = by(set3[,c("y1","y2","y3")], set3$trt, sum)
     result = matrix(data = c(s1[1],s2[1],s3[1],
                              s1[2],s2[2],s3[2]), ncol=2)
     colnames(result) = c("trt = 0", "trt = 1")
     output = cbind(time = 1:3,result)
     return(output)
}

# Statistic for boot
AREA = function(data,i){
        d = data[i,]
        MQ = SUM(data = d)
        A.0 = trap.rule(x = 1:3, y = MQ[,2])
        A.1 = trap.rule(x = 1:3, y = MQ[,3])
        area = A.1 - A.0
        result = c(A.0, A.1, area)
        return(result)
}

area.boot = boot(data = Data, statistic = AREA, R = 1000)
area.boot

However, there is an error message

Error in tapply(1L:49L, list(`set2$trt` = c(0L, 1L, 1L, 1L, 0L, 1L, 0L,  : 
  arguments must have same length
$\endgroup$
2
  • $\begingroup$ The problem is in your function AREA. $\endgroup$
    – user10525
    Commented Apr 21, 2012 at 12:13
  • 1
    $\begingroup$ Hi Pocrastinator, I have a typo in my SUM function (see Daniel's comment). That's why boot can't work with AREA function. thanks. $\endgroup$
    – Tu.2
    Commented Apr 21, 2012 at 19:17

1 Answer 1

1
$\begingroup$

I'm just getting started with R... Shouldn't it be set2 in the s2 assignment:

 s1 = by(set1[,c("y1","y2","y3")], set1$trt, sum)
 s2 = by(set2[,c("y1","y2","y3")], set2$trt, sum)
 s3 = by(set3[,c("y1","y2","y3")], set3$trt, sum)
$\endgroup$
0

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.