Paul Miller
2012-Mar-22 17:41 UTC
[R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!
Hello All, Want very much to learn how to plot patient drug timelines. Trouble is I need to figure out how to do this today. So not much time for me to struggle with it. Hoping someone can just help me out a bit. Below are some sample data and code that produces what I think is the beginning of a very nice graph. Need to alter the code to: 1. Get the lines for the drugs to appear on the y-axis in the order that they appear in the data. 2. Decrease the vertical space between the line segments for each drug so they are fairly close to one another. 3. Remove the numbering from the x-axis. 4. Put the text for pattern above the graph (e.g., "Begin (A), Begin (B), End (B), End (A)"), either centered or left aligned. 5. Put the patient and line information below the text for pattern (e.g., "profile_key = 1, line = 1") 6. Output a separate graph for each patient and line of treatent. Ultimately, I want to combine all the graphs into a single Word document. Or perhaps better yet, to create a pdf using LaTeX. I'm going to continue to try and fugure this out as best I can. Any help with it will be greatly appreciated though. Thanks, Paul connection <- textConnection(" 1/1/Drug A/ Begin (A), Begin (B), End (B), End (A)/0.0000/21.000 1/1/Drug B/ Begin (A), Begin (B), End (B), End (A)/0.7143/18.000 1/2/Drug A/ Begin (A, B, C), End (A, B), End (C)/0.0000/20.000 1/2/Drug B/ Begin (A, B, C), End (A, B), End (C)/0.0000/20.000 1/2/Drug C/ Begin (A, B, C), End (A, B), End (C)/0.0000/36.000 2/1/Drug A/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/0.0000/7.429 2/1/Drug B/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/ 0.0000/7.429 2/1/Drug C/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/ 14.5714/21.857 2/1/Drug D/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/ 25.4286/231.286 2/2/Drug A/ Begin (A, B), End (A, B)/0.0000/35.286 2/2/Drug B/ Begin (A, B), End (A, B)/0.0000/35.286 ") TestData <- data.frame(scan(connection, list(profile_key=0, line=0, drug="", pattern="", start_drug=0, stop_drug=0), sep="/")) TestData <- TestData[TestData$profile_key == 2 & TestData$line == 1,] TestData require(reshape) TestData <- melt(TestData, measure.vars = c("start_drug", "stop_drug")) TestData ggplot(TestData, aes(value, drug)) + geom_line(size = 6) + xlab("") + ylab("") + theme_bw() ggsave(file = "plot21.pdf")
Bert Gunter
2012-Mar-22 17:50 UTC
[R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!
Your data are still uninterpretable to me. A general approach to this sort of thing is to get your data in "long" format, like this patientID time value A On Thu, Mar 22, 2012 at 10:41 AM, Paul Miller <pjmiller_57 at yahoo.com> wrote:> Hello All, > > Want very much to learn how to plot patient drug timelines. Trouble is I need to figure out how to do this today. So not much time for me to struggle with it. Hoping someone can just help me out a bit. > > Below are some sample data and code that produces what I think is the beginning of a very nice graph. > > Need to alter the code to: > > 1. Get the lines for the drugs to appear on the y-axis in the order that they appear in the data. > > 2. Decrease the vertical space between the line segments for each drug so they are fairly close to one another. > > 3. Remove the numbering from the x-axis. > > 4. Put the text for pattern above the graph (e.g., "Begin (A), Begin (B), End (B), End (A)"), either centered or left aligned. > > 5. Put the patient and line information below the text for pattern (e.g., "profile_key = 1, line = 1") > > 6. Output a separate graph for each patient and line of treatent. > > Ultimately, I want to combine all the graphs into a single Word document. Or perhaps better yet, to create a pdf using LaTeX. > > I'm going to continue to try and fugure this out as best I can. Any help with it will be greatly appreciated though. > > Thanks, > > Paul > > > connection <- textConnection(" > 1/1/Drug A/ Begin (A), Begin (B), End (B), End (A)/0.0000/21.000 > 1/1/Drug B/ Begin (A), Begin (B), End (B), End (A)/0.7143/18.000 > 1/2/Drug A/ Begin (A, B, C), End (A, B), End (C)/0.0000/20.000 > 1/2/Drug B/ Begin (A, B, C), End (A, B), End (C)/0.0000/20.000 > 1/2/Drug C/ Begin (A, B, C), End (A, B), End (C)/0.0000/36.000 > 2/1/Drug A/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/0.0000/7.429 > 2/1/Drug B/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/ 0.0000/7.429 > 2/1/Drug C/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/ 14.5714/21.857 > 2/1/Drug D/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/ 25.4286/231.286 > 2/2/Drug A/ Begin (A, B), End (A, B)/0.0000/35.286 > 2/2/Drug B/ Begin (A, B), End (A, B)/0.0000/35.286 > ") > > TestData <- data.frame(scan(connection, list(profile_key=0, line=0, drug="", pattern="", start_drug=0, stop_drug=0), sep="/")) > TestData <- TestData[TestData$profile_key == 2 & TestData$line == 1,] > TestData > > require(reshape) > TestData <- melt(TestData, measure.vars = c("start_drug", "stop_drug")) > TestData > > ggplot(TestData, aes(value, drug)) + geom_line(size = 6) + xlab("") + ylab("") + theme_bw() > ggsave(file = "plot21.pdf") > > ______________________________________________ > R-help at r-project.org 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
R. Michael Weylandt
2012-Mar-22 17:55 UTC
[R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!
Inline: On Thu, Mar 22, 2012 at 1:41 PM, Paul Miller <pjmiller_57 at yahoo.com> wrote:> Hello All, > > Want very much to learn how to plot patient drug timelines. Trouble is I need to figure out how to do this today. So not much time for me to struggle with it. Hoping someone can just help me out a bit. > > Below are some sample data and code that produces what I think is the beginning of a very nice graph. > > Need to alter the code to: > > 1. Get the lines for the drugs to appear on the y-axis in the order that they appear in the data.Scale_x_flip()> > 2. Decrease the vertical space between the line segments for each drug so they are fairly close to one another.Why? This will happen automatically if needed....> > 3. Remove the numbering from the x-axis.opts(axis.ticks.x = theme_blank())> > 4. Put the text for pattern above the graph (e.g., "Begin (A), Begin (B), End (B), End (A)"), either centered or left aligned.opts(title = "Begin (A), Begin (B), End (B), End (A)")> > 5. Put the patient and line information below the text for pattern (e.g., "profile_key = 1, line = 1")example(geom_text) or https://learnr.wordpress.com/2010/01/03/directlabels-adding-direct-labels-to-ggplot2-and-lattice-plots/> > 6. Output a separate graph for each patient and line of treatent. > > Ultimately, I want to combine all the graphs into a single Word document. Or perhaps better yet, to create a pdf using LaTeX.Google Sweave or (possibly easier) knitr> > I'm going to continue to try and fugure this out as best I can. Any help with it will be greatly appreciated though. > > Thanks, > > Paul > > > connection <- textConnection(" > 1/1/Drug A/ Begin (A), Begin (B), End (B), End (A)/0.0000/21.000 > 1/1/Drug B/ Begin (A), Begin (B), End (B), End (A)/0.7143/18.000 > 1/2/Drug A/ Begin (A, B, C), End (A, B), End (C)/0.0000/20.000 > 1/2/Drug B/ Begin (A, B, C), End (A, B), End (C)/0.0000/20.000 > 1/2/Drug C/ Begin (A, B, C), End (A, B), End (C)/0.0000/36.000 > 2/1/Drug A/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/0.0000/7.429 > 2/1/Drug B/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/ 0.0000/7.429 > 2/1/Drug C/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/ 14.5714/21.857 > 2/1/Drug D/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/ 25.4286/231.286 > 2/2/Drug A/ Begin (A, B), End (A, B)/0.0000/35.286 > 2/2/Drug B/ Begin (A, B), End (A, B)/0.0000/35.286 > ") > > TestData <- data.frame(scan(connection, list(profile_key=0, line=0, drug="", pattern="", start_drug=0, stop_drug=0), sep="/")) > TestData <- TestData[TestData$profile_key == 2 & TestData$line == 1,] > TestData > > require(reshape) > TestData <- melt(TestData, measure.vars = c("start_drug", "stop_drug")) > TestData > > ggplot(TestData, aes(value, drug)) + geom_line(size = 6) + xlab("") + ylab("") + theme_bw()It would be easier if you could use dput() but this was helpfully reproducible. Hope this gets you started, Michael> ggsave(file = "plot21.pdf") > > ______________________________________________ > R-help at r-project.org 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.
On Fri, Mar 23, 2012 at 12:35:57AM +0100, Gildas Mazo wrote:> Dear R users, > > Let f be a function over d variables x1,..,xd. I want to compute the k^th-order derivative with respect to x1,..,xk (k<=d). I have a by hand solution (see below) using an iterating code using D. However, I expect d to be high and f to be complicated. Then I want a vector x to be the input, instead of x1,..,xd. How to avoid the x1 <- x[1]; x2 <- x[2], etc steps in the code below? Moreover, D uses symbolic differentation and then eval evaluates the output to get a numerical result. But is there a way to compute the desired derivatives numerically directly (without using symbolic calculus at all)? Finally, what is the most efficient and fast way to get a numerical result for such derivatives? > > Thank you very much in advance, > Gildas > > ### Code ### > ### dif takes a function f, an order k, and a vector x as input. f must be a function of x1,..,xd with d >= k. The correspondance is done between xi and x[i]. The expression for f must be at the last row of the body function. > dif <- function(f,k,x){ > o <- list() > n <- length(body(f)) > o[[1]] <- body(f)[[n]] > for (i in 1:k){ > xi <- paste("x",i,sep="") > o[[i+1]] <- D(o[[i]],name=xi) > } > x1 <- x[1] > x2 <- x[2] > x3 <- x[3] > eval(o[[k+1]]) > } > > ### Examples ### > ## function to differentiate > f <- function(x){ > x1 <- x[1] > x2 <- x[2] > x3 <- x[3] > 0.5*x1*x2*x3^2 > } > ## derivative w.r.t. x1, x2 and x3 at the point (1,2,3). > dif(f,3,c(1,2,3)) > > ### My Questions ### > ## how to avoid to write by hand xi <- x[i] ?? > ## is there a way in R to compute such derivatives without using symbolic calculation but numerical compuation instead.Hi. For the first question, try the following dif <- function(f,k,x){ o <- list() n <- length(body(f)) o[[1]] <- body(f)[[n]] for (i in 1:k){ xi <- paste("x",i,sep="") o[[i+1]] <- D(o[[i]],name=xi) assign(xi, x[i]) } eval(o[[k+1]]) } For the second question, try the following. x <- c(1, 2, 3) k <- length(x) grid <- as.matrix(expand.grid(rep(list(c(0, 1)), times=k))) signs <- 1 - 2*(rowSums(1 - grid) %% 2) for (eps in 2^-(5:20)) { xeps <- eps*grid + rep(x, each=nrow(grid)) print(sum(signs*apply(xeps, 1, FUN=f))/eps^k) } [1] 3.015625 [1] 3.007812 [1] 3.003906 [1] 3.001953 [1] 3.000977 [1] 3.000488 [1] 3.000244 [1] 3.000122 [1] 3 [1] 3 [1] 3 [1] 3 [1] 4 [1] 0 [1] 0 [1] 0 If the above is computed in an exact arithmetic, then with "eps" converging to zero, the result converges to the required derivative. Since the numerical computations are done with a rounding error, too small "eps" yields a completely wrong result. The choice of a good "eps" depends on the function and on "k". For a high "k", there may even be no good "eps". See the considerations at http://en.wikipedia.org/wiki/Numerical_derivative where the choice of "eps" is discussed in the simplest case of a univariate function. Hope this helps. Petr Savicky.
Dear Petr Savicky, this helped indeed. Thank you very much. Gildas ----- Mail original -----> De: "Petr Savicky" <savicky at cs.cas.cz> > ?: r-help at r-project.org > Envoy?: Vendredi 23 Mars 2012 09:39:37 > Objet: Re: [R] Computing High Order Derivatives (Numerically) > On Fri, Mar 23, 2012 at 12:35:57AM +0100, Gildas Mazo wrote: > > Dear R users, > > > > Let f be a function over d variables x1,..,xd. I want to compute the > > k^th-order derivative with respect to x1,..,xk (k<=d). I have a by > > hand solution (see below) using an iterating code using D. However, > > I expect d to be high and f to be complicated. Then I want a vector > > x to be the input, instead of x1,..,xd. How to avoid the x1 <- x[1]; > > x2 <- x[2], etc steps in the code below? Moreover, D uses symbolic > > differentation and then eval evaluates the output to get a numerical > > result. But is there a way to compute the desired derivatives > > numerically directly (without using symbolic calculus at all)? > > Finally, what is the most efficient and fast way to get a numerical > > result for such derivatives? > > > > Thank you very much in advance, > > Gildas > > > > ### Code ### > > ### dif takes a function f, an order k, and a vector x as input. f > > must be a function of x1,..,xd with d >= k. The correspondance is > > done between xi and x[i]. The expression for f must be at the last > > row of the body function. > > dif <- function(f,k,x){ > > o <- list() > > n <- length(body(f)) > > o[[1]] <- body(f)[[n]] > > for (i in 1:k){ > > xi <- paste("x",i,sep="") > > o[[i+1]] <- D(o[[i]],name=xi) > > } > > x1 <- x[1] > > x2 <- x[2] > > x3 <- x[3] > > eval(o[[k+1]]) > > } > > > > ### Examples ### > > ## function to differentiate > > f <- function(x){ > > x1 <- x[1] > > x2 <- x[2] > > x3 <- x[3] > > 0.5*x1*x2*x3^2 > > } > > ## derivative w.r.t. x1, x2 and x3 at the point (1,2,3). > > dif(f,3,c(1,2,3)) > > > > ### My Questions ### > > ## how to avoid to write by hand xi <- x[i] ?? > > ## is there a way in R to compute such derivatives without using > > symbolic calculation but numerical compuation instead. > > Hi. > > For the first question, try the following > > dif <- function(f,k,x){ > o <- list() > n <- length(body(f)) > o[[1]] <- body(f)[[n]] > for (i in 1:k){ > xi <- paste("x",i,sep="") > o[[i+1]] <- D(o[[i]],name=xi) > assign(xi, x[i]) > } > eval(o[[k+1]]) > } > > For the second question, try the following. > > x <- c(1, 2, 3) > k <- length(x) > grid <- as.matrix(expand.grid(rep(list(c(0, 1)), times=k))) > signs <- 1 - 2*(rowSums(1 - grid) %% 2) > for (eps in 2^-(5:20)) { > xeps <- eps*grid + rep(x, each=nrow(grid)) > print(sum(signs*apply(xeps, 1, FUN=f))/eps^k) > } > > [1] 3.015625 > [1] 3.007812 > [1] 3.003906 > [1] 3.001953 > [1] 3.000977 > [1] 3.000488 > [1] 3.000244 > [1] 3.000122 > [1] 3 > [1] 3 > [1] 3 > [1] 3 > [1] 4 > [1] 0 > [1] 0 > [1] 0 > > If the above is computed in an exact arithmetic, then > with "eps" converging to zero, the result converges to > the required derivative. Since the numerical computations > are done with a rounding error, too small "eps" yields > a completely wrong result. The choice of a good "eps" > depends on the function and on "k". For a high "k", there > may even be no good "eps". See the considerations at > > http://en.wikipedia.org/wiki/Numerical_derivative > > where the choice of "eps" is discussed in the simplest > case of a univariate function. > > Hope this helps. > > Petr Savicky. > > ______________________________________________ > R-help at r-project.org 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.-- Gildas Mazo PhD student MISTIS team at INRIA Grenoble, France