looping survdiff?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

looping survdiff?

Charles Determan Jr
Hello,

I am trying to set up a loop that can run the survdiff function with the
ultimate goal to generate a csv file with the p-values reported.  However,
whenever I try a loop I get an error such as "invalid type (list) for
variable 'survival_data_variables[i]".

This is a subset of my data:

structure(list(time = c(1.51666666666667, 72, 72, 25.7833333333333,
72, 72, 72, 72, 72, 72, 1.18333333333333, 0.883333333333333,
1.15, 0.866666666666667, 72, 1.03333333333333, 72, 1.05, 72,
22.7666666666667), group = c(2L, 1L, 3L, 3L, 3L, 4L, 4L,
1L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 4L, 3L, 4L, 3L, 4L), completion =
structure(c(2L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L,
2L, 1L, 2L), .Label = c("1", "2"), class = "factor"), var1 =
structure(c(2L,
2L, 3L, 1L, 1L, 3L, 1L, 1L, 1L, 3L, 2L, 2L, 4L, 3L, 2L, 4L, 2L,
4L, 2L, 4L), .Label = c("1", "2", "3", "4"), class = "factor"),
    var2 = structure(c(3L, 3L, 1L, 1L, 2L, 4L, 3L,
    3L, 2L, 4L, 2L, 1L, 2L, 1L, 2L, 2L, 4L, 4L, 2L, 3L), .Label = c("1",
    "2", "3", "4"), class = "factor"), var3 = structure(c(4L,
    2L, 3L, 1L, 3L, 4L, 4L, 2L, 2L, 4L, 2L, 2L, 1L, 2L, 2L, 2L,
    1L, 3L, 4L, 1L), .Label = c("1", "2", "3", "4"), class = "factor")),
.Names = c("time",
"group", "completion", "var1", "var2",
"var3"), row.names = c(NA, 20L), class = "data.frame")


The loop I have been trying for just group 3 is:

d=data.frame()
for(i in 4:6){
    a=assign(paste("p-value",i,sep=""),
        survdiff(Surv(time, completion=="2")~dat[i],
        data=dat[group=="3",],
        rho=0))
    b=as.matrix(a$chisq)
    d=rbind(d, b)
write.csv(d, file="C:/.../junk.csv", quote=FALSE)}

Perhaps I am making this more difficult than it needs to be.  Thanks for
any help,

Charles

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: looping survdiff?

Andrews, Chris
> sapply(seq(4,ncol(dat)), function(i) survdiff(Surv(time,completion=="2")~dat[,i], data=dat, subset=group=="3")$chisq)
[1] 0.0944 4.4854 3.4990

Chris

-----Original Message-----
From: Charles Determan Jr [mailto:[hidden email]]
Sent: Thursday, October 18, 2012 3:04 PM
To: [hidden email]
Subject: [R] looping survdiff?

Hello,

I am trying to set up a loop that can run the survdiff function with the ultimate goal to generate a csv file with the p-values reported.  However, whenever I try a loop I get an error such as "invalid type (list) for variable 'survival_data_variables[i]".

This is a subset of my data:

structure(list(time = c(1.51666666666667, 72, 72, 25.7833333333333, 72, 72, 72, 72, 72, 72, 1.18333333333333, 0.883333333333333, 1.15, 0.866666666666667, 72, 1.03333333333333, 72, 1.05, 72, 22.7666666666667), group = c(2L, 1L, 3L, 3L, 3L, 4L, 4L, 1L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 4L, 3L, 4L, 3L, 4L), completion = structure(c(2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("1", "2"), class = "factor"), var1 = structure(c(2L, 2L, 3L, 1L, 1L, 3L, 1L, 1L, 1L, 3L, 2L, 2L, 4L, 3L, 2L, 4L, 2L, 4L, 2L, 4L), .Label = c("1", "2", "3", "4"), class = "factor"),
    var2 = structure(c(3L, 3L, 1L, 1L, 2L, 4L, 3L,
    3L, 2L, 4L, 2L, 1L, 2L, 1L, 2L, 2L, 4L, 4L, 2L, 3L), .Label = c("1",
    "2", "3", "4"), class = "factor"), var3 = structure(c(4L,
    2L, 3L, 1L, 3L, 4L, 4L, 2L, 2L, 4L, 2L, 2L, 1L, 2L, 2L, 2L,
    1L, 3L, 4L, 1L), .Label = c("1", "2", "3", "4"), class = "factor")), .Names = c("time", "group", "completion", "var1", "var2", "var3"), row.names = c(NA, 20L), class = "data.frame")


The loop I have been trying for just group 3 is:

d=data.frame()
for(i in 4:6){
    a=assign(paste("p-value",i,sep=""),
        survdiff(Surv(time, completion=="2")~dat[i],
        data=dat[group=="3",],
        rho=0))
    b=as.matrix(a$chisq)
    d=rbind(d, b)
write.csv(d, file="C:/.../junk.csv", quote=FALSE)}

Perhaps I am making this more difficult than it needs to be.  Thanks for any help,

Charles

        [[alternative HTML version deleted]]


**********************************************************
Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.