variable as argument to makeContrasts (limma)
3
0
Entering edit mode
Georg Otto ▴ 510
@georg-otto-956
Last seen 10.3 years ago
Hi, maybe this is a more general R programming question, but I first try it here, because the problem occured to me with limma: I use the function makeContrasts makeContrasts(...,levels=design) like this makeContrasts(B-A,C-B,C-A,levels=design) now instead of giving explicitely "B-A,C-B,C-A" I would like to replace this by a variable, eg: a<-c("B-A","C-B","C-A") makeContrasts(a,levels=design) unfortunately, this does not work. So my question is: how can I make this work, and - more generally - how can I replace the "..." option in a function by a variable? Thanks, Georg
• 4.0k views
ADD COMMENT
1
Entering edit mode
@saroj-k-mohapatra-3419
Last seen 10.3 years ago
Hi Georg: It is doable, the idea is to create the command string first and then evaluate it as an R expression. Frirst, collapse the contrasts. If it is a vector like in your case: a<-c("B-A","C-B","C-A") > astr=paste(a, collapse=",") > astr [1] "B-A,C-B,C-A" Then add correct strings before and after. > prestr="makeContrasts(" > poststr=",levels=design)" > commandstr=paste(prestr,astr,poststr,sep="") > commandstr [1] "makeContrasts(B-A,C-B,C-A,levels=design)" Now evaluate the command string: > eval(parse(text=commandstr)) Contrasts Levels B - A C - B C - A A -1 0 -1 B 1 -1 0 C 0 1 1 However, 'eval' is a tricky thing (see some comments here on the list some days back). There might be better (i.e., more straight-forward) ways of doing this. Hopefully, others would comment. Best wishes, Saroj Georg Otto wrote: > Hi, > > maybe this is a more general R programming question, but I first try it > here, because the problem occured to me with limma: > > I use the function makeContrasts > > makeContrasts(...,levels=design) > > like this > > makeContrasts(B-A,C-B,C-A,levels=design) > > > now instead of giving explicitely "B-A,C-B,C-A" I would like to replace > this by a variable, eg: > > a<-c("B-A","C-B","C-A") > > makeContrasts(a,levels=design) > > unfortunately, this does not work. > > So my question is: how can I make this work, and - more generally - how > can I replace the "..." option in a function by a variable? > > Thanks, > > Georg > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor > >
ADD COMMENT
0
Entering edit mode
Hi Georg -- Saroj K Mohapatra wrote: > Hi Georg: > > It is doable, the idea is to create the command string first and then > evaluate it as an R expression. > > Frirst, collapse the contrasts. If it is a vector like in your case: > > a<-c("B-A","C-B","C-A") > > > astr=paste(a, collapse=",") > > astr > [1] "B-A,C-B,C-A" > > Then add correct strings before and after. > > prestr="makeContrasts(" > > poststr=",levels=design)" > > commandstr=paste(prestr,astr,poststr,sep="") > > commandstr > [1] "makeContrasts(B-A,C-B,C-A,levels=design)" > > Now evaluate the command string: > > eval(parse(text=commandstr)) > Contrasts > Levels B - A C - B C - A > A -1 0 -1 > B 1 -1 0 > C 0 1 1 > > However, 'eval' is a tricky thing (see some comments here on the list > some days back). There might be better (i.e., more straight-forward) > ways of doing this. Hopefully, others would comment. > > Best wishes, > > Saroj > > Georg Otto wrote: >> Hi, >> >> maybe this is a more general R programming question, but I first try it >> here, because the problem occured to me with limma: >> >> I use the function makeContrasts >> >> makeContrasts(...,levels=design) >> >> like this >> >> makeContrasts(B-A,C-B,C-A,levels=design) >> >> >> now instead of giving explicitely "B-A,C-B,C-A" I would like to replace >> this by a variable, eg: >> >> a<-c("B-A","C-B","C-A") >> >> makeContrasts(a,levels=design) >> >> unfortunately, this does not work. One way is to aim for do.call(makeConstrasts, myargs) where the 'myargs' is a list that you can construct any way you like, e.g., myargs = list("B-A", "C-B", "C-A", levels=design) do.call(makeContrasts, myargs) Martin >> >> So my question is: how can I make this work, and - more generally - how >> can I replace the "..." option in a function by a variable? >> >> Thanks, >> >> Georg >> >> _______________________________________________ >> Bioconductor mailing list >> Bioconductor at stat.math.ethz.ch >> https://stat.ethz.ch/mailman/listinfo/bioconductor >> Search the archives: >> http://news.gmane.org/gmane.science.biology.informatics.conductor >> >> > > > -------------------------------------------------------------------- ---- > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor -- Martin Morgan Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793
ADD REPLY
0
Entering edit mode
Martin Morgan wrote: > > One way is to aim for > > do.call(makeConstrasts, myargs) > > where the 'myargs' is a list that you can construct any way you like, > e.g., > > myargs = list("B-A", "C-B", "C-A", levels=design) > do.call(makeContrasts, myargs) > Perfect! Saroj > Martin > >>> >>> So my question is: how can I make this work, and - more generally - how >>> can I replace the "..." option in a function by a variable? >>> >>> Thanks, >>> >>> Georg >>> >>> _______________________________________________ >>> Bioconductor mailing list >>> Bioconductor at stat.math.ethz.ch >>> https://stat.ethz.ch/mailman/listinfo/bioconductor >>> Search the archives: >>> http://news.gmane.org/gmane.science.biology.informatics.conductor >>> >>> >> >> >> ------------------------------------------------------------------- ----- >> >> _______________________________________________ >> Bioconductor mailing list >> Bioconductor at stat.math.ethz.ch >> https://stat.ethz.ch/mailman/listinfo/bioconductor >> Search the archives: >> http://news.gmane.org/gmane.science.biology.informatics.conductor > >
ADD REPLY
0
Entering edit mode
Georg Otto ▴ 510
@georg-otto-956
Last seen 10.3 years ago
Martin Morgan <mtmorgan at="" fhcrc.org=""> writes: >> >> Georg Otto wrote: >>> Hi, >>> >>> maybe this is a more general R programming question, but I first try it >>> here, because the problem occured to me with limma: >>> >>> I use the function makeContrasts >>> >>> makeContrasts(...,levels=design) >>> >>> like this >>> >>> makeContrasts(B-A,C-B,C-A,levels=design) >>> >>> >>> now instead of giving explicitely "B-A,C-B,C-A" I would like to replace >>> this by a variable, eg: >>> >>> a<-c("B-A","C-B","C-A") >>> >>> makeContrasts(a,levels=design) >>> >>> unfortunately, this does not work. > > One way is to aim for > > do.call(makeConstrasts, myargs) > > where the 'myargs' is a list that you can construct any way you like, e.g., > > myargs = list("B-A", "C-B", "C-A", levels=design) > do.call(makeContrasts, myargs) > thanks a lot, that helps, but there comes up another problem: I start with a character vector, from which I have to construct the list: > contrast.vector [1] "First=B-A" "Second=D-C" How do I get from there to: myargs = list(First="B-A", Second="D-C", levels=design) Any hint will be appreciated, Georg
ADD COMMENT
0
Entering edit mode
Georg Otto wrote: > Martin Morgan <mtmorgan at="" fhcrc.org=""> writes: > >>> Georg Otto wrote: >>>> Hi, >>>> >>>> maybe this is a more general R programming question, but I first try it >>>> here, because the problem occured to me with limma: >>>> >>>> I use the function makeContrasts >>>> >>>> makeContrasts(...,levels=design) >>>> >>>> like this >>>> >>>> makeContrasts(B-A,C-B,C-A,levels=design) >>>> >>>> >>>> now instead of giving explicitely "B-A,C-B,C-A" I would like to replace >>>> this by a variable, eg: >>>> >>>> a<-c("B-A","C-B","C-A") >>>> >>>> makeContrasts(a,levels=design) >>>> >>>> unfortunately, this does not work. >> One way is to aim for >> >> do.call(makeConstrasts, myargs) >> >> where the 'myargs' is a list that you can construct any way you like, e.g., >> >> myargs = list("B-A", "C-B", "C-A", levels=design) >> do.call(makeContrasts, myargs) >> > > thanks a lot, that helps, but there comes up another problem: > > I start with a character vector, from which I have to construct the > list: > >> contrast.vector > [1] "First=B-A" "Second=D-C" I'm not sure that you want to start from there? myargs <- list(First="B-A", Second="D-C") or x <- c("First=B-A", "Second=D-C") namecontr <- strsplit(x, "=") myargs <- lapply(namecontr, "[[", 2) names(myargs) <- sapply(namecontr, "[[", 1) myargs[["levels"]] <- design but I wonder a bit what the 'bigger picture' is, since these kinds of manipulations are not what one would do in a typical analysis. Martin > > How do I get from there to: > > myargs = list(First="B-A", Second="D-C", levels=design) > > Any hint will be appreciated, > > Georg > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor
ADD REPLY
0
Entering edit mode
Georg Otto ▴ 510
@georg-otto-956
Last seen 10.3 years ago
Martin Morgan <mtmorgan at="" fhcrc.org=""> writes: > Georg Otto wrote: >> Martin Morgan <mtmorgan at="" fhcrc.org=""> >> writes: >> >>> One way is to aim for >>> >>> do.call(makeConstrasts, myargs) >>> >>> where the 'myargs' is a list that you can construct any way you >>> like, e.g., >>> >>> myargs = list("B-A", "C-B", "C-A", levels=design) >>> do.call(makeContrasts, myargs) >>> >> >> thanks a lot, that helps, but there comes up another problem: >> >> I start with a character vector, from which I have to construct the >> list: >> >> contrast.vector [1] "First=B-A" "Second=D-C" > > I'm not sure that you want to start from there? > > myargs <- list(First="B-A", Second="D-C") > > or > > x <- c("First=B-A", "Second=D-C") namecontr <- strsplit(x, "=") > myargs <- lapply(namecontr, "[[", 2) names(myargs) <- > sapply(namecontr, "[[", 1) myargs[["levels"]] <- design > > but I wonder a bit what the 'bigger picture' is, since these kinds of > manipulations are not what one would do in a typical analysis. > > Martin > > >> >> How do I get from there to: >> >> myargs = list(First="B-A", Second="D-C", levels=design) well, maybe I should have explained this before: the "bigger picture" is, that I want to programmatically construct calls to the limma lmFit function, to be able to analyze subsets of data a large collection od data. I got most of it to work but I have difficulties with the way the parameters are passed to makeContrasts. In my function I have is a vector with strings which are the contrasts that I want to analyse: contrast.vector [1] "First=B-A" "Second=D-C" and the design object. from this I want to programatically construct the contrast matrix, that can be passed to another function that does the lmFit. I think the way you suggested is the way to go, I was a little bit too complicated in my thinking, by trying all kinds of parse/deparse/eval constructs Thanks, Georg
ADD COMMENT

Login before adding your answer.

Traffic: 490 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6