Question about survdiff in for-loop.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about survdiff in for-loop.

Sando

Hi everyone!!

I have dataset composed of a numbers of survival analyses.
( for batch survival analyses by using for-loop) .
Here are code !!

#######
dim(svsv)
Num_t<-dim(svsv)
Num<-Num_t[2]   # These are predictors !!

names=colnames(svsv)

for (i in 1:Num  )
{
name_tt=names[i]
survdiff(Surv(survival.m, survival) ~ names[i], data=svsv)
fit.Group<-survfit(Surv(survival.m, survival) ~ names[i] , data=svsv)
plot(fit.Group, col=2:1, xlab="Survival", ylab="Prob")
}

#####

names[i] is not working in the survdiff.
According to help R , the predictor must be single subset.

And, names[i] is also single character, I think.

But, it do NOT work.

How I can solve this problem ?



Thank you.

 
Reply | Threaded
Open this post in threaded view
|

Re: Question about survdiff in for-loop.

Thomas Lumley-2
On Fri, Oct 19, 2012 at 8:02 PM, Sando <[hidden email]> wrote:

>
> Hi everyone!!
>
> I have dataset composed of a numbers of survival analyses.
> ( for batch survival analyses by using for-loop) .
> Here are code !!
>
> #######
> dim(svsv)
> Num_t<-dim(svsv)
> Num<-Num_t[2]   # These are predictors !!
>
> names=colnames(svsv)
>
> for (i in 1:Num  )
> {
> name_tt=names[i]
> survdiff(Surv(survival.m, survival) ~ names[i], data=svsv)
> fit.Group<-survfit(Surv(survival.m, survival) ~ names[i] , data=svsv)
> plot(fit.Group, col=2:1, xlab="Survival", ylab="Prob")
> }
>
> #####
>
> names[i] is not working in the survdiff.

That's a problem with how formulas are parsed: you are effectively
telling survdiff() that you want names[i] as your predictor variable,
when actually you want it as the name of your predictor variable.

Using svsv[i] rather than names[i] should work.  Or you can insert the
value of names[i] into the formula with

 survdiff(eval(bquote(Surv(survival.m, survival) ~ .(names[i]))), data=svsv)

Even after you fix that, there's another problem, which is that your
code doesn't actually use the result from survdiff() in any way.

  -thomas

--
Thomas Lumley
Professor of Biostatistics
University of Auckland

______________________________________________
[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: Question about survdiff in for-loop.

Sando


Thank you for your replay and help !!

Best Regards, Young.