White, Charles E WRAIR-Wash DC
2004-Aug-19 21:17 UTC
[R] List dimention labels to plots of components
It is frustrating to see the labels I want in the dimensions of a list but not be able to extract those labels into titles for plots generated from component objects. If someone could set me straight, I would appreciate it. For your amusement, I have provided an example of the Byzantine code I am currently using to avoid loops: # Simulate ANOVA type test data sex<-c(rep(1,8),rep(0,8)) dose<-c(rep(1,4),rep(0,4)) treatment<-c(rep(1,2),rep(0,2)) fix<-sex+dose+treatment Response<-fix+rnorm(16) Sex<-rep("Male",16) Sex[sex==0]<-"Female" Dose<-rep("High",16) Dose[dose==0]<-"Low" Treatment<-rep("A",16) Treatment[treatment==0]<-"B" dat<-data.frame(Sex,Dose,Treatment,fix,Response) # Redundant Transfer and Execution of Dimension ID? mymod<-function(x){ model<-lm(Response~Sex,data=x) list(model,Dose=x$Dose[1],Treatment=x$Treatment[1])} myplt<-function(x){ plot(x[[1]],main=paste(x$Dose,"/",x$Treatment,sep=""))} # Generate list of Model Estimates dat.lm<-by(dat,list(Dose=dat$Dose,Treatment=dat$Treatment),mymod) # Execute plots with labels pdf(file="junk.pdf",height=7.5,width=10) par(mfrow=c(2,2)) bitbucket<-lapply(dat.lm,myplt) dev.off() Charles E. White, Senior Biostatistician, MS Walter Reed Army Institute of Research 503 Robert Grant Ave., Room 1w102 Silver Spring, MD 20910-1557 301 319-9781 Personal/Professional Site: http://users.starpower.net/cwhite571/professional/
White, Charles E WRAIR-Wash DC wrote:> It is frustrating to see the labels I want in the dimensions of a list but not be able to extract those labels into titles for plots generated from component objects. If someone could set me straight, I would appreciate it. For your amusement, I have provided an example of the Byzantine code I am currently using to avoid loops:The code below works perfectly. I don't get the point what you are looking for .... Uwe Ligges> # Simulate ANOVA type test data > sex<-c(rep(1,8),rep(0,8)) > dose<-c(rep(1,4),rep(0,4)) > treatment<-c(rep(1,2),rep(0,2)) > fix<-sex+dose+treatment > Response<-fix+rnorm(16) > Sex<-rep("Male",16) > Sex[sex==0]<-"Female" > Dose<-rep("High",16) > Dose[dose==0]<-"Low" > Treatment<-rep("A",16) > Treatment[treatment==0]<-"B" > dat<-data.frame(Sex,Dose,Treatment,fix,Response) > > # Redundant Transfer and Execution of Dimension ID? > mymod<-function(x){ > model<-lm(Response~Sex,data=x) > list(model,Dose=x$Dose[1],Treatment=x$Treatment[1])} > myplt<-function(x){ > plot(x[[1]],main=paste(x$Dose,"/",x$Treatment,sep=""))} > > # Generate list of Model Estimates > dat.lm<-by(dat,list(Dose=dat$Dose,Treatment=dat$Treatment),mymod) > > # Execute plots with labels > pdf(file="junk.pdf",height=7.5,width=10) > par(mfrow=c(2,2)) > bitbucket<-lapply(dat.lm,myplt) > dev.off() > > Charles E. White, Senior Biostatistician, MS > Walter Reed Army Institute of Research > 503 Robert Grant Ave., Room 1w102 > Silver Spring, MD 20910-1557 > 301 319-9781 > Personal/Professional Site: http://users.starpower.net/cwhite571/professional/ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
White, Charles E WRAIR-Wash DC
2004-Aug-20 13:52 UTC
[R] List dimention labels to plots of components
My goal is more efficient code for something I anticipate doing a lot. My example code from my first message works because I insert a seemingly redundant recording of Dose & Treatment in the list generated in the "by" command. Since Dose & Treatment are already recorded in the dimensions of the list, I would like to pass those dimensions into the function executed by lapply. That may or may not be possible. # Append the following code to my message of 8/19 # Dose & Treatment go to list without separate user code to insert them dat.lm2<-by(dat,list(Dose=dat$Dose,Treatment=dat$Treatment), function(x) lm(Response~Sex,data=x)) dimnames(dat.lm2) dat.lm2["Low","B"] # lapply uses dimnames to select objects but does not appear to pass # dimnames with object lapply(dat.lm2,dimnames) lapply(dat.lm2,names) lapply(dat.lm2,function(x) print(x)[1:13]) Thanks for your help. Chuck
White, Charles E WRAIR-Wash DC wrote:> My goal is more efficient code for something I anticipate doing a lot. > My example code from my first message works because I insert a seemingly > redundant recording of Dose & Treatment in the list generated in the > "by" command. Since Dose & Treatment are already recorded in the > dimensions of the list, I would like to pass those dimensions into the > function executed by lapply. That may or may not be possible. > > # Append the following code to my message of 8/19 > # Dose & Treatment go to list without separate user code to insert them > dat.lm2<-by(dat,list(Dose=dat$Dose,Treatment=dat$Treatment), > function(x) lm(Response~Sex,data=x)) > dimnames(dat.lm2) > dat.lm2["Low","B"] > > # lapply uses dimnames to select objects but does not appear to pass > # dimnames with object > lapply(dat.lm2,dimnames) > lapply(dat.lm2,names) > lapply(dat.lm2,function(x) print(x)[1:13]) > > Thanks for your help. > > ChuckJust one example: mapply(function(x, y) plot(x[[1]], main = y), dat.lm2, outer(rownames(dat.lm2), colnames(dat.lm2), paste, sep="/")) Uwe Ligges