Dear all, ?The code below is used to generate interval censored data but unfortunately there is an error with the ifelse which i am not able to rectify. ?Can somebody help correct it for me. Thank you t<-rexp(20,0.2)? v<-c(0,m,999)? y<-function(t,v){ ? z<-numeric(length(t (( ? ? s<-numeric(length(t (( ? ? ? for(i in 1:length(t)){ ? ? ? ? for(j in 1:length(v-1))? ? ? ? ? { ifelse ((t[i]>v[j] & t< v[j+1] ),{z[i]<-v[j];s[i]<-v[j+1]},NA)}}? ? ? ? return(cbind(z,s))}? y(t,v) Chris Kelvin Institute for Mathematical Research UPM
Hi, The error with ifelse() seems to be that you have no idea what ifelse() does. As far as I can tell, you tried to construct code that does something like this: y<-function(tdata,v){ z <- rep(NA, length(tdata)) s <- z for(i in 1:length(tdata)) { for(j in 1:length(v-1)) { if(tdata[i] > v[j] & tdata[i] < v[j+1]) { z[i]<-v[j] s[i]<-v[j+1] } } } return(cbind(z,s)) } But what's with all the (( instead of ))? And are you certain that the logic in the if statement is correct? If you tell us what you expect the results to be for given input values, we can help with that part too. Including making this more Rish: the nested for-loop construct is entirely unnecessary here, but I'm disinclined to rewrite it unless I actually know what you're trying to achieve. Incidentally, your example is only nearly-reproducible, since we don't know what m is. Sarah On Wed, May 30, 2012 at 10:01 AM, Christopher Kelvin <chris_kelvin2001 at yahoo.com> wrote:> Dear all, > ?The code below is used to generate interval censored data but unfortunately there is an error with the ifelse which i am not able to rectify. > ?Can somebody help correct it for me. > Thank you > > > t<-rexp(20,0.2) > v<-c(0,m,999) > > y<-function(t,v){ > ? z<-numeric(length(t (( > ? ? s<-numeric(length(t (( > ? ? ? for(i in 1:length(t)){ > ? ? ? ? for(j in 1:length(v-1)) > ? ? ? ? { ifelse ((t[i]>v[j] & t< v[j+1] ),{z[i]<-v[j];s[i]<-v[j+1]},NA)}} > ? ? ? return(cbind(z,s))} > > y(t,v) >-- Sarah Goslee http://www.functionaldiversity.org
Since your code has things like this: z<-numeric(length(t (( either you have a serious problem with your email client or you need to reread some introductory material and take a hard look at your code. Also note that g() doesn't work, because it contains the statement return(m) but m is undefined within g(). Meanwhile, you could provide what I asked: a statement of what you expect your code to produce given particular input. Otherwise how would we know if we've offered the right solution, since your function doesn't work? Using set.seed() would be a useful component of this reproducible example. Without having a working if poorly-written function to go by or a clear results statement, I'm not interested in trying to rewrite your code. But some thoughts: Here's a new version of f(). f2 <- function(c1, c2) { r <- pmax(c1 + c2, c1 + 0.5) cbind(c1, r) } It looks like you expected f() to be able to take vectors, but in g() you only return one value. Is that a mistake, or what you wanted? Since you're also using cbind(), I assume it's a mistake. Again, there are lots of problems here that suggest that you are coming from some other programming language and have not taken the time to learn much about R's syntax. This is easily remedied by reading the introduction. Sarah On Wed, May 30, 2012 at 3:32 PM, Christopher Kelvin <chris_kelvin2001 at yahoo.com> wrote:> Hello Sarah, > Thank you for your response. Below is the complete code. My desire is to obtain interval censored data through simulation to fit it on the weibull distribution to estimate the parameters. I am actually not very sure of the code correctness. You may try it and advice me on what to do and also about it correctness if time will permit you. > > Thank you > > > g<-function(c1,c2) { > ? f<-function(c1,c2) { > ? ? u<-c1 > ? ? h<-c1+c2 > ? ? k<-c1+0.5 > ? ? r<-numeric (length(c1)) > ? ? for(i in 1:length(r)) r[i]<-max(h[i],k[i]) > ? ? return( cbind (u,r))} > ? r1<-f(c1,c2) > ? r2<-f(r1[2],r1[1]) > ? r3<-f(r2[2],r2[1]) > ? r4<-f(r3[2],r3[1]) > ? r5<-f(r4[2],r4[1]) > ? a<-(cbind(r1[1],r2[1],r3[1],r4[1],r5[1],r5[2])) > ? return(m )} > c1<-runif(1,0,1.5) > c2<-runif(1,0,0.5) > m<-g(c1,c2) > > tdata<-rweibull(25,0.8,1.5) > v<-c(0,m,999) > > y<-function(t,v){ > ? z<-numeric(length(t (( > ? ? s<-numeric(length(t (( > ? ? ? for(i in 1:length(t)){ > ? ? ? ? for(j in 1:length(v-1)) > ? ? ? ? { ifelse ((t[i]>v[j] & t< v[j+1] ),{z[i]<-v[j];s[i]<-v[j+1]},NA)}} > ? ? ? return(cbind(z,s))} > > y(t,v) > > > > > Chris Kelvin > > > > > > > > Hi, > > The error with ifelse() seems to be that you have no idea what ifelse() does. > > As far as I can tell, you tried to construct code that does something like this: > > > y<-function(tdata,v){ > ? ?z <- rep(NA, length(tdata)) > ? ?s <- z > ? ?for(i in 1:length(tdata)) { > ? ? ? for(j in 1:length(v-1)) { > ??? ??? ??? if(tdata[i] > v[j] & tdata[i] < v[j+1]) { > ??? ??? ??? ??? z[i]<-v[j] > ??? ??? ??? ??? s[i]<-v[j+1] > ??? ??? ??? } > ??? ??? } > ??? } > ? ?return(cbind(z,s)) > } > > But what's with all the (( instead of ))? > > And are you certain that the logic in the if statement is correct? > > If you tell us what you expect the results to be for given input > values, we can help with that part too. Including making this more > Rish: the nested for-loop construct is entirely unnecessary here, but > I'm disinclined to rewrite it unless I actually know what you're > trying to achieve. > > Incidentally, your example is only nearly-reproducible, since we don't > know what m is. > > Sarah > > On Wed, May 30, 2012 at 10:01 AM, Christopher Kelvin > <chris_kelvin2001 at yahoo.com> wrote: >> Dear all, >> ?The code below is used to generate interval censored data but unfortunately there is an error with the ifelse which i am not able to rectify. >> ?Can somebody help correct it for me. >> Thank you >> >> >> t<-rexp(20,0.2) >> v<-c(0,m,999) >> >> y<-function(t,v){ >> ? z<-numeric(length(t (( >> ? ? s<-numeric(length(t (( >> ? ? ? for(i in 1:length(t)){ >> ? ? ? ? for(j in 1:length(v-1)) >> ? ? ? ? { ifelse ((t[i]>v[j] & t< v[j+1] ),{z[i]<-v[j];s[i]<-v[j+1]},NA)}} >> ? ? ? return(cbind(z,s))} >> >> y(t,v) >> > >-- Sarah Goslee http://www.functionaldiversity.org
________________________________________> The code below is used to generate interval censored data but unfortunately there is an > error with the ifelse which i am not able to rectify. > Can somebody help correct it for me.Not quickly. But cut() might do a large part of what you need with a lot less faffing about; see ?cut S Ellison******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
Hi, On Thu, May 31, 2012 at 10:09 AM, Christopher Kelvin <chris_kelvin2001 at yahoo.com> wrote:> Hello Sarah, > ?I hope i have understood you; All i seek to do is to get a code that i can obtain interval censoring from without using the survival package. Can you come to my aid?Probably, but you need to meet me halfway. What do your inputs look like? What do your desired outputs look like? First, state them in plain English: my input is a vector of numeric values. In my desired output, this sort of value is changed to this number. Then provide that in R form. Here's an example input. Here's what I want the output to look like. I'm good at writing R code, but I'm not interested in wading through your non-working code to figure out what you meant. Please read the posting guide, and please send your replies back to the whole list and not just me. Sarah> Thank you > Chris > > > > > > > > ----- Original Message ----- > From: Sarah Goslee <sarah.goslee at gmail.com> > To: Christopher Kelvin <chris_kelvin2001 at yahoo.com>; r-help <r-help at r-project.org> > Cc: > Sent: Thursday, May 31, 2012 3:51 AM > Subject: Re: [R] problem with ifelse > > Since your code has things like this: > z<-numeric(length(t (( > > either you have a serious problem with your email client or you need > to reread some introductory material and take a hard look at your > code. > > Also note that g() doesn't work, because it contains the statement > return(m) but m is undefined within g(). > > Meanwhile, you could provide what I asked: a statement of what you > expect your code to produce given particular input. Otherwise how > would we know if we've offered the right solution, since your function > doesn't work? Using set.seed() would be a useful component of this > reproducible example. > > Without having a working if poorly-written function to go by or a > clear results statement, I'm not interested in trying to rewrite your > code. But some thoughts: > > Here's a new version of f(). > > f2 <- function(c1, c2) { > ??? r <- pmax(c1 + c2, c1 + 0.5) > ??? cbind(c1, r) > } > > It looks like you expected f() to be able to take vectors, but in g() > you only return one value. Is that a mistake, or what you wanted? > Since you're also using cbind(), I assume it's a mistake. > > Again, there are lots of problems here that suggest that you are > coming from some other programming language and have not taken the > time to learn much about R's syntax. This is easily remedied by > reading the introduction. > > Sarah > > On Wed, May 30, 2012 at 3:32 PM, Christopher Kelvin > <chris_kelvin2001 at yahoo.com> wrote: >> Hello Sarah, >> Thank you for your response. Below is the complete code. My desire is to obtain interval censored data through simulation to fit it on the weibull distribution to estimate the parameters. I am actually not very sure of the code correctness. You may try it and advice me on what to do and also about it correctness if time will permit you. >> >> Thank you >> >> >> g<-function(c1,c2) { >> ? f<-function(c1,c2) { >> ? ? u<-c1 >> ? ? h<-c1+c2 >> ? ? k<-c1+0.5 >> ? ? r<-numeric (length(c1)) >> ? ? for(i in 1:length(r)) r[i]<-max(h[i],k[i]) >> ? ? return( cbind (u,r))} >> ? r1<-f(c1,c2) >> ? r2<-f(r1[2],r1[1]) >> ? r3<-f(r2[2],r2[1]) >> ? r4<-f(r3[2],r3[1]) >> ? r5<-f(r4[2],r4[1]) >> ? a<-(cbind(r1[1],r2[1],r3[1],r4[1],r5[1],r5[2])) >> ? return(m )} >> c1<-runif(1,0,1.5) >> c2<-runif(1,0,0.5) >> m<-g(c1,c2) >> >> tdata<-rweibull(25,0.8,1.5) >> v<-c(0,m,999) >> >> y<-function(t,v){ >> ? z<-numeric(length(t (( >> ? ? s<-numeric(length(t (( >> ? ? ? for(i in 1:length(t)){ >> ? ? ? ? for(j in 1:length(v-1)) >> ? ? ? ? { ifelse ((t[i]>v[j] & t< v[j+1] ),{z[i]<-v[j];s[i]<-v[j+1]},NA)}} >> ? ? ? return(cbind(z,s))} >> >> y(t,v) >> >> >> >> >> Chris Kelvin >> >> >> >> >> >> >> >> Hi, >> >> The error with ifelse() seems to be that you have no idea what ifelse() does. >> >> As far as I can tell, you tried to construct code that does something like this: >> >> >> y<-function(tdata,v){ >> ? ?z <- rep(NA, length(tdata)) >> ? ?s <- z >> ? ?for(i in 1:length(tdata)) { >> ? ? ? for(j in 1:length(v-1)) { >> ??? ??? ??? if(tdata[i] > v[j] & tdata[i] < v[j+1]) { >> ??? ??? ??? ??? z[i]<-v[j] >> ??? ??? ??? ??? s[i]<-v[j+1] >> ??? ??? ??? } >> ??? ??? } >> ??? } >> ? ?return(cbind(z,s)) >> } >> >> But what's with all the (( instead of ))? >> >> And are you certain that the logic in the if statement is correct? >> >> If you tell us what you expect the results to be for given input >> values, we can help with that part too. Including making this more >> Rish: the nested for-loop construct is entirely unnecessary here, but >> I'm disinclined to rewrite it unless I actually know what you're >> trying to achieve. >> >> Incidentally, your example is only nearly-reproducible, since we don't >> know what m is. >> >> Sarah >> >> On Wed, May 30, 2012 at 10:01 AM, Christopher Kelvin >> <chris_kelvin2001 at yahoo.com> wrote: >>> Dear all, >>> ?The code below is used to generate interval censored data but unfortunately there is an error with the ifelse which i am not able to rectify. >>> ?Can somebody help correct it for me. >>> Thank you >>> >>> >>> t<-rexp(20,0.2) >>> v<-c(0,m,999) >>> >>> y<-function(t,v){ >>> ? z<-numeric(length(t (( >>> ? ? s<-numeric(length(t (( >>> ? ? ? for(i in 1:length(t)){ >>> ? ? ? ? for(j in 1:length(v-1)) >>> ? ? ? ? { ifelse ((t[i]>v[j] & t< v[j+1] ),{z[i]<-v[j];s[i]<-v[j+1]},NA)}} >>> ? ? ? return(cbind(z,s))} >>> >>> y(t,v) >>> >> >>-- Sarah Goslee http://www.functionaldiversity.org
Hi, On Thu, May 31, 2012 at 12:49 PM, Christopher Kelvin <chris_kelvin2001 at yahoo.com> wrote:> Hello, > Sorry for reply you directly but i feel that if i send to R they might not post because they may not understand what i am trying to say > I want to provide you with the code below by using the survival package, that might make sense to you than all these codes i am confusing myself with.But treating me as your personal R guru is a less-optimal solution than asking the entire list. Someone else might understand your problem; I certainly don't.> I have say some values represented by r which i record for the first time i am conducting my study for each individual, 15 people > i have another set of data that i refer to as the stop time of my study represented by t. Now what i have is two set of data say [1 2], [3 4] that is numeric values.So you have actual measured data for start time and stop time? What do those look like?> To get my desired output i require that the number of start time be equal to the number of end time.That seems unlikely.> So at the end i need a data that has lower values in the left and upper values in the right.What do your recorded data look like, and what does your intended output look like?> > library(survival) > p1<-1.2;b<-1.5;n<-15 > r<-runif(n,min=0,max=b) > t<-rweibull(n,shape=p1,scale=b) > w=Surv(r,t+r,type="interval2")So r and t+r are equivalent to your recorded data, and w is your desired output? Comments would be helpful. Then why can't you use this method to process your own data? Sarah> > Thank you > Chris > > > > ----- Original Message ----- > From: Sarah Goslee <sarah.goslee at gmail.com> > To: Christopher Kelvin <chris_kelvin2001 at yahoo.com>; r-help <r-help at r-project.org> > Cc: > Sent: Thursday, May 31, 2012 11:19 PM > Subject: Re: [R] problem with ifelse > > Hi, > > On Thu, May 31, 2012 at 10:09 AM, Christopher Kelvin > <chris_kelvin2001 at yahoo.com> wrote: >> Hello Sarah, >> ?I hope i have understood you; All i seek to do is to get a code that i can obtain interval censoring from without using the survival package. Can you come to my aid? > > Probably, but you need to meet me halfway. > > What do your inputs look like? > > What do your desired outputs look like? > > First, state them in plain English: my input is a vector of numeric > values. In my desired output, this sort of value is changed to this > number. > > Then provide that in R form. Here's an example input. Here's what I > want the output to look like. > > I'm good at writing R code, but I'm not interested in wading through > your non-working code to figure out what you meant. Please read the > posting guide, and please send your replies back to the whole list and > not just me. > > Sarah > >> Thank you >> Chris >> >> >> >> >> >> >> >> ----- Original Message ----- >> From: Sarah Goslee <sarah.goslee at gmail.com> >> To: Christopher Kelvin <chris_kelvin2001 at yahoo.com>; r-help <r-help at r-project.org> >> Cc: >> Sent: Thursday, May 31, 2012 3:51 AM >> Subject: Re: [R] problem with ifelse >> >> Since your code has things like this: >> z<-numeric(length(t (( >> >> either you have a serious problem with your email client or you need >> to reread some introductory material and take a hard look at your >> code. >> >> Also note that g() doesn't work, because it contains the statement >> return(m) but m is undefined within g(). >> >> Meanwhile, you could provide what I asked: a statement of what you >> expect your code to produce given particular input. Otherwise how >> would we know if we've offered the right solution, since your function >> doesn't work? Using set.seed() would be a useful component of this >> reproducible example. >> >> Without having a working if poorly-written function to go by or a >> clear results statement, I'm not interested in trying to rewrite your >> code. But some thoughts: >> >> Here's a new version of f(). >> >> f2 <- function(c1, c2) { >> ??? r <- pmax(c1 + c2, c1 + 0.5) >> ??? cbind(c1, r) >> } >> >> It looks like you expected f() to be able to take vectors, but in g() >> you only return one value. Is that a mistake, or what you wanted? >> Since you're also using cbind(), I assume it's a mistake. >> >> Again, there are lots of problems here that suggest that you are >> coming from some other programming language and have not taken the >> time to learn much about R's syntax. This is easily remedied by >> reading the introduction. >> >> Sarah >> >> On Wed, May 30, 2012 at 3:32 PM, Christopher Kelvin >> <chris_kelvin2001 at yahoo.com> wrote: >>> Hello Sarah, >>> Thank you for your response. Below is the complete code. My desire is to obtain interval censored data through simulation to fit it on the weibull distribution to estimate the parameters. I am actually not very sure of the code correctness. You may try it and advice me on what to do and also about it correctness if time will permit you. >>> >>> Thank you >>> >>> >>> g<-function(c1,c2) { >>> ? f<-function(c1,c2) { >>> ? ? u<-c1 >>> ? ? h<-c1+c2 >>> ? ? k<-c1+0.5 >>> ? ? r<-numeric (length(c1)) >>> ? ? for(i in 1:length(r)) r[i]<-max(h[i],k[i]) >>> ? ? return( cbind (u,r))} >>> ? r1<-f(c1,c2) >>> ? r2<-f(r1[2],r1[1]) >>> ? r3<-f(r2[2],r2[1]) >>> ? r4<-f(r3[2],r3[1]) >>> ? r5<-f(r4[2],r4[1]) >>> ? a<-(cbind(r1[1],r2[1],r3[1],r4[1],r5[1],r5[2])) >>> ? return(m )} >>> c1<-runif(1,0,1.5) >>> c2<-runif(1,0,0.5) >>> m<-g(c1,c2) >>> >>> tdata<-rweibull(25,0.8,1.5) >>> v<-c(0,m,999) >>> >>> y<-function(t,v){ >>> ? z<-numeric(length(t (( >>> ? ? s<-numeric(length(t (( >>> ? ? ? for(i in 1:length(t)){ >>> ? ? ? ? for(j in 1:length(v-1)) >>> ? ? ? ? { ifelse ((t[i]>v[j] & t< v[j+1] ),{z[i]<-v[j];s[i]<-v[j+1]},NA)}} >>> ? ? ? return(cbind(z,s))} >>> >>> y(t,v) >>> >>> >>> >>> >>> Chris Kelvin >>> >>> >>> >>> >>> >>> >>> >>> Hi, >>> >>> The error with ifelse() seems to be that you have no idea what ifelse() does. >>> >>> As far as I can tell, you tried to construct code that does something like this: >>> >>> >>> y<-function(tdata,v){ >>> ? ?z <- rep(NA, length(tdata)) >>> ? ?s <- z >>> ? ?for(i in 1:length(tdata)) { >>> ? ? ? for(j in 1:length(v-1)) { >>> ??? ??? ??? if(tdata[i] > v[j] & tdata[i] < v[j+1]) { >>> ??? ??? ??? ??? z[i]<-v[j] >>> ??? ??? ??? ??? s[i]<-v[j+1] >>> ??? ??? ??? } >>> ??? ??? } >>> ??? } >>> ? ?return(cbind(z,s)) >>> } >>> >>> But what's with all the (( instead of ))? >>> >>> And are you certain that the logic in the if statement is correct? >>> >>> If you tell us what you expect the results to be for given input >>> values, we can help with that part too. Including making this more >>> Rish: the nested for-loop construct is entirely unnecessary here, but >>> I'm disinclined to rewrite it unless I actually know what you're >>> trying to achieve. >>> >>> Incidentally, your example is only nearly-reproducible, since we don't >>> know what m is. >>> >>> Sarah >>> >>> On Wed, May 30, 2012 at 10:01 AM, Christopher Kelvin >>> <chris_kelvin2001 at yahoo.com> wrote: >>>> Dear all, >>>> ?The code below is used to generate interval censored data but unfortunately there is an error with the ifelse which i am not able to rectify. >>>> ?Can somebody help correct it for me. >>>> Thank you >>>> >>>> >>>> t<-rexp(20,0.2) >>>> v<-c(0,m,999) >>>> >>>> y<-function(t,v){ >>>> ? z<-numeric(length(t (( >>>> ? ? s<-numeric(length(t (( >>>> ? ? ? for(i in 1:length(t)){ >>>> ? ? ? ? for(j in 1:length(v-1)) >>>> ? ? ? ? { ifelse ((t[i]>v[j] & t< v[j+1] ),{z[i]<-v[j];s[i]<-v[j+1]},NA)}} >>>> ? ? ? return(cbind(z,s))} >>>> >>>> y(t,v) >-- Sarah Goslee http://www.functionaldiversity.org