[19110] in s-news-athena
[S] Missing Data "Runs" (Belated) Summary / rle / -apply/Converting List
daemon@ATHENA.MIT.EDU (Barker, Chris {PHAR~Palo Alto})
Fri Sep 3 16:24:15 1999
Content-Return: allowed
Date: Fri, 03 Sep 1999 13:18:57 -0700
From: "Barker, Chris {PHAR~Palo Alto}" <CHRIS.BARKER@ROCHE.COM>
To: "'s-news@wubios.wustl.edu'" <s-news@wubios.wustl.edu>
Message-Id: <B9E51B9719BDD111AB640000F80197CD016464AC@rplmsem2.pal.roche.com>
Mime-Version: 1.0
Content-Type: text/plain; CHARSET=US-ASCII
Two weeks ago or so I asked a question
about use of tapply (and also lapply and sapply) for creating a summary of
missing data "runs".
Below is my (belated) summary of the very helpful replies I received below
from both Julian Taylor and Bill Venables.
(Most important, my sincere thanks to Julian Taylor. In testing his code it
helped me
to discover an important error in my datafile.)
Restating the question:
I have a large dataframe, 600 or so patients, measured each day for 21 days
(600*21 rows of data).
The response has only integer values (or is missing).
I would like to summarize "runs" of missing (NA) data
or runs of the integer responses or prepare histograms or else groupedData
plots
of patients with certain patterns of data.
From Julian Taylor:
The algorithm below summarizes "runs" using rle - and converts a list to a
dataframe. An assumption is that the dataframe
is sorted in a meaningful order and that patient identifiers are unique. (Of
course be sure to test this code on your own)
BIGFRAME.rle.simple <- tapply(BIGFRAME$response.var, BIGFRAME$patient.id,
FUN=rle)
TxBigFile.datlist <- lapply(BIGFRAME.rle.simple, function(ek)
{data.frame(ek)})
TxBigFile.dat <- data.frame(lengths = c(unlist(sapply(TxBigFile.datlist,
function(el) el$lengths))), values = c(unlist(sapply(TxBigFile.datlist,
function(el) el$values))), row.names = NULL)
TxBigFile.dat$patient.id <- rep(unique(BIGFRAME$patient.id),
sapply(TxBigFile.datlist, function(el) nrow(el)))
__________________________________________________________________
Also from Bill Venables
For example, suppose you wish to classify the patients by the longest run of
non-missing values.
> maxlen <- tapply(TxBigFile$response, patient.ID,
FUN = function(x) {
runs <- rle(is.na(x))
max(runs$length[runs$values == F])
})
> TxBigFile$MaxLen <- ordered(maxlen[patient.ID])
So at this point your data frame has an additional ordered factor (called
MaxLen) that groups patients according to the size of the longest run of
non-missing values. Other criteria for classification based on runs would
be handled similarly.
________________________________________________________________
Chris Barker, Ph.D.
Principal Pharmacoeconomic Statistician
Global Pharmacoeconomic Research
Roche Pharma Business - Palo Alto
(650)-852-3152
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news