Eric Fail
2010-Jan-18 23:25 UTC
[R] add spline to longitudinal data - preferably similar to SAS's 'I=SM50S' routine
Hi Ruser I'm trying to replicate some SAS code. I have to add a spline to my longitudinal spaghetti plot. I have the plot, but I can't add the spline, a overall trend line. In the SAS code they use the command 'I=SM50S' and I would prefer something similar. I?m using R 2.10.1 on windows XP? I have made this working example. tolerance.pp <- read.table("http://www.ats.ucla.edu/stat/R/examples/alda/tolerance1_pp.txt ", sep=",", header=T) # install.packages("lattice", dep = T) library(lattice) xyplot(tolerance ~ age, groups = id, data=tolerance.pp, type = "l") This is where I want to add a overall spline. Hope someone out there can figure this out. Thanks Eric
Dylan Beaudette
2010-Jan-18 23:49 UTC
[R] add spline to longitudinal data - preferably similar to SAS's 'I=SM50S' routine
On Mon, Jan 18, 2010 at 3:25 PM, Eric Fail <e at it.dk> wrote:> Hi Ruser > > I'm trying to replicate some SAS code. I have to add a spline to my > longitudinal spaghetti plot. > > I have the plot, but I can't add the spline, a overall trend line. In the > SAS code they use the command ? 'I=SM50S' and I would prefer something > similar. I?m using R 2.10.1 on windows XP? > > I have made this working example. > > tolerance.pp <- > read.table("http://www.ats.ucla.edu/stat/R/examples/alda/tolerance1_pp.txt", > sep=",", header=T) > # install.packages("lattice", dep = T) > library(lattice) > xyplot(tolerance ~ age, groups = id, data=tolerance.pp, type = "l")Hi, How about something like: # your data tolerance.pp <- read.table("http://www.ats.ucla.edu/stat/R/examples/alda/tolerance1_pp.txt", sep=",", header=T) # almost works... xyplot(tolerance ~ age, groups = id, data=tolerance.pp, type = c('l','smooth')) see ?panel.loess for more ideas Cheers, Dylan> > This is where I want to add a overall spline. > > Hope someone out there can figure this out. > > Thanks > > Eric > ______________________________________________ > 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. >
Dennis Murphy
2010-Jan-19 00:12 UTC
[R] add spline to longitudinal data - preferably similar to SAS's 'I=SM50S' routine
Hi: There is a very similar example in the ggplot book by Hadley Wickham (section 4.5, pp. 50-52). Here's one approach using ggplot: library(ggplot2) p <- ggplot(tolerance.pp, aes(age, tolerance, group = id)) + geom_line() p + geom_smooth(aes(group = 1), size = 2) The second command adds a smoothing spline in blue, with twice the line width as the individual spaghetti plots, and by default, a confidence envelope around it. To get rid of the envelope, include se = FALSE as an argument to geom_smooth(); to change the color, add the argument colour = 'red', for example. HTH, Dennis On Mon, Jan 18, 2010 at 3:25 PM, Eric Fail <e@it.dk> wrote:> Hi Ruser > > I'm trying to replicate some SAS code. I have to add a spline to my > longitudinal spaghetti plot. > > I have the plot, but I can't add the spline, a overall trend line. In the > SAS code they use the command 'I=SM50S' and I would prefer something > similar. I’m using R 2.10.1 on windows XP… > > I have made this working example. > > tolerance.pp <- read.table(" > http://www.ats.ucla.edu/stat/R/examples/alda/tolerance1_pp.txt", sep=",", > header=T) > # install.packages("lattice", dep = T) > library(lattice) > xyplot(tolerance ~ age, groups = id, data=tolerance.pp, type = "l") > > This is where I want to add a overall spline. > > Hope someone out there can figure this out. > > Thanks > > Eric > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Hi Dennis (cc Lucien Lemmens and the r-help) Thank you for your help. I got help from Mr. Lucien Lemmens as well, and thanks to him to (again). The thing is, if, my working example, is run line by line, it overwrites ... or one thing overwrites the next. However both you and Lucien Lemmens solved my problem. Lucien Lemmens solution. plot <- ggplot(tolerance.pp, aes(age, tolerance, group= id)) + geom_line() plot <- plot+geom_smooth(aes(group=male,colour=male),size=1,se=FALSE) plot + scale_x_continuous(breaks = c(10, 12, 13, 15)) I know it seems as there is no purpose, but I have a huge dataset (here) where the measures are at 6, 12, 20 and 24 (i think), which was the reason for my question. Maybe I should have written that in the mail. Anyhow, here is two solutions on how to make the grid in a ggplot2 unequally spaced, if anyone ever should run it to that problem agin. Thanks for all your help. Eric On 31/01/2010, at 17.51, Dennis Murphy wrote:> Hi: > > I don't quite see what the problem is, but I > can show you a couple of things that might be useful... > > (1) you want male to be a factor, but in the data set, it takes > integer > values; therefore, you need to redefine it as a factor: > > str(tolerance.pp) > 'data.frame': 80 obs. of 6 variables: > $ id : int 9 9 9 9 9 45 45 45 45 45 ... > $ age : int 11 12 13 14 15 11 12 13 14 15 ... > $ tolerance: num 2.23 1.79 1.9 2.12 2.66 1.12 1.45 1.45 1.45 > 1.99 ... > $ male : int 0 0 0 0 0 1 1 1 1 1 ... > $ exposure : num 1.54 1.54 1.54 1.54 1.54 1.16 1.16 1.16 1.16 > 1.16 ... > $ time : int 0 1 2 3 4 0 1 2 3 4 ... > > tolerance.pp$male <- factor(tolerance.pp$male, labels = c('F', 'M')) > > str(tolerance.pp) > 'data.frame': 80 obs. of 6 variables: > $ id : int 9 9 9 9 9 45 45 45 45 45 ... > $ age : int 11 12 13 14 15 11 12 13 14 15 ... > $ tolerance: num 2.23 1.79 1.9 2.12 2.66 1.12 1.45 1.45 1.45 > 1.99 ... > $ male : Factor w/ 2 levels "F","M": 1 1 1 1 1 2 2 2 2 2 ... > $ exposure : num 1.54 1.54 1.54 1.54 1.54 1.16 1.16 1.16 1.16 > 1.16 ... > $ time : int 0 1 2 3 4 0 1 2 3 4 ... > > This has an impact on the legend, as you'll see below. > > (2) I set scale_x_continuous(breaks = NA) in the initial setup and > redefined > it later on. There's probably a more efficient way, but it works. > (3) I thought that xlim(9, 16) would extend the limits of the x- > axis, but it > had no effect on the plot. What you'll see below are x-ticks at > 12, 13 and 15, > although I don't see the purpose of it other than to see that > it can be done... > > plot <- ggplot(tolerance.pp, aes(age, tolerance, group = id)) + > xlim(9, 16) + > scale_x_continuous(breaks = NA) > plot + geom_line() + > geom_smooth(aes(group = male, colour = male), size = 1.2, > se = FALSE) + > scale_x_continuous(breaks = c(10, 12, 13, 15)) + > scale_colour_hue("gender") > > The last line changes the legend title to something a little more > evocative. > > HTH, > Dennis > > > On Sun, Jan 31, 2010 at 1:28 PM, Eric Fail <e@it.dk> wrote: > Dear list > > A week ago Dennis Murphy helped me out by showing me some nice > ggplot2 tricks . Now I got stuck in a new problem that I can't solve > (I have ordered the ggplot2-book). > > My problem is that I can't add my spline (or geom_smooth) and at the > same time control the grid (using scale_x_continuous), they seem to > overwrite each other. > > I have continued the working example from my last question (http://n4.nabble.com/add-spline-to-longitudinal-data-preferably-similar-to-SAS-s-I-SM50S-routine-td1017138.html > ) > > ############ example start ############ > > tolerance.pp <- read.table("http://www.ats.ucla.edu/stat/R/examples/alda/tolerance1_pp.txt > ", sep=",", header=T) > # install.packages("ggplot2", dep = T) > library(ggplot2) > > plot <- ggplot(tolerance.pp, aes(age, tolerance, group = id)) + > geom_line() > plot + geom_smooth(aes(group = male, colour = male), size = 1, se = > FALSE) > plot + scale_x_continuous(breaks = c(10, 12, 13, 15)) > > # plot + scale_x_continuous(limits = c(9, 16)) > > ############ example end ############ > > I have added the 'plot + scale_x_continuous(limits = c(9, 16)) ' > since this seem to conflict as well. > > Thanks in advance! > > Eric > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]