[R] (no subject)

r.ghezzo@staff.mcgill.ca r.ghezzo at staff.mcgill.ca
Mon Jun 20 17:58:10 CEST 2005


R friends,
I am using R 2.1.0 in a Win XP . I have a problem working with lists, probably I
do not understand how to use them.

Lets suppose that a set of patients visit a clinic once a year for 4 years
on each visit a test, say 'eib' is performed with results 0 or 1
The patients do not all visit the clinic the 4 times but they missed a lot
of visits.
The test is considered positive if it is positive at the last 2 visits of that
patient, or a more lenient definition, it is positive in the last visit, and
never before.
Otherwise it is Negative = always negative or is a YoYo = unstable = changes
from positive to negative.
So, if I codify the visits with codes 1,2,4,8 if present at year 1,2,3,4 and
similarly the tests positive I get the last2 list codifying the test code
corresponding to the visits patterns possible, similarly the last1 list
20 here means NULL

nobs <- 400
#  visits       0   1   2    3     4    5      6      7       8    9
last1 <- list((20),(1),(2),c(3,2),(4),c(5,4),c(6,4),c(7,6,4),(8),c(9,8),
#  visits      10      11         12      13         14         15
             c(10,8),c(11,10,8),c(12,8),c(13,12,8),c(14,12,8),c(15,14,12,8))
#  visits       0   1    2    3   4    5   6    7     8    9
last2 <- list((20),(20),(20),(3),(20),(5),(6),c(7,6),(20),(9),
#  visits      10    11      12    13       14       15
              (10),c(11,10),(12),c(13,12),c(14,12),c(15,14,12))
#
#     simulate the visits
#
visit <- rbinom(nobs,1,0.7)
eib <- visit
#
#     simulate a positive test at a given visit
#
eib <- ifelse(runif(nobs) > 0.7,visit,0)
#
#     create the codes
#
viskode <- matrix(visit,ncol=4) %*% c(1,2,4,8)
eibkode <- matrix(eib,ncol=4) %*% c(1,2,4,8)
#
#    this is the brute force method, slow, of computing the Results according to
#    the 2 definitions above. Add 16 to the test kode to signify YoYos, Exactly
#    16 will be the negatives
#
 eibnoyoyo <- eibkode+16
 eiblst2 <- eibkode+16
 for(i in 1:nobs){
   if(eibkode[i] %in% last1[[viskode[i]+1]])
      eibnoyoyo[i] <- eibkode[i]
   if(eibkode[i] %in% last2[[viskode[i]+1]])
      eiblast2[i] <- eibkode[i]
 }
#
#    why is that these statements do not work?
#
eeibnoyoyo <- eeiblst2 <- rep(0,nobs)
eeibnoyoyo <- ifelse(eibkode %in% last1[viskode+1],eibkode,eibkode+16)
eeiblast2   <- ifelse(eibkode %in% last2[viskode+1],eibkode,eibkode+16)
#
table(viskode,eibkode)
table(viskode,eibnoyoyo)
table(viskode,eiblast2)
#
#  these two tables must be diagonal!!
#
table(eibnoyoyo,eeibnoyoyo)
table(eiblast2,eeiblast2)
#
Thanks for any help
Heberto Ghezzo
McGill University
Canada




More information about the R-help mailing list