Rainer M Krug
2012-Aug-21 14:44 UTC
[R] define subset argument for function lm as variable?
Hi I want to do a series of linear models, and would like to define the input arguments for lm() as variables. I managed easily to define the formula arguments in a variable, but I also would like to have the "subset" in a variable. My reasoning is, that I have the subset in the results object. So I wiould like to add a line like: subs <- dead==FALSE & recTreat==FALSE which obviously does not work as the expression is evaluated immediately. Is is it possible to do what I want to do here, or do I have to go back to use dat <- subset(dat, dead==FALSE & recTreat==FALSE) ? dat <- loadSPECIES(SPECIES) feff <- height~pHarv*year # fixed effect in the model reff <- ~year|plant # random effect in the model, where year is the dat.lme <- lme( fixed = feff, # fixed effect in the model data = dat, random = reff, # random effect in the model correlation = corAR1(form=~year|plant), # subset = dead==FALSE & recTreat==FALSE, # na.action = na.omit ) dat.lm <- lm( formula = feff, # fixed effect in the model data = dat, subset = dead==FALSE & recTreat==FALSE, na.action = na.omit ) Thanks, Rainer -- Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany) Centre of Excellence for Invasion Biology Stellenbosch University South Africa Tel : +33 - (0)9 53 10 27 44 Cell: +33 - (0)6 85 62 59 98 Fax : +33 - (0)9 58 10 27 44 Fax (D): +49 - (0)3 21 21 25 22 44 email: Rainer at krugs.de Skype: RMkrug
Joshua Wiley
2012-Aug-21 14:51 UTC
[R] define subset argument for function lm as variable?
Hi Rainer, You could try: subs <- expression(dead==FALSE & recTreat==FALSE) lme(formula, subset = eval(subs)) Not tested, but something along those lines should work. Cheers, Josh On Tue, Aug 21, 2012 at 7:44 AM, Rainer M Krug <r.m.krug at gmail.com> wrote:> Hi > > I want to do a series of linear models, and would like to define the input > arguments for lm() as variables. I managed easily to define the formula > arguments in a variable, but I also would like to have the "subset" in a > variable. My reasoning is, that I have the subset in the results object. > > So I wiould like to add a line like: > > subs <- dead==FALSE & recTreat==FALSE > > which obviously does not work as the expression is evaluated immediately. Is > is it possible to do what I want to do here, or do I have to go back to use > > dat <- subset(dat, dead==FALSE & recTreat==FALSE) > > ? > > > > dat <- loadSPECIES(SPECIES) > feff <- height~pHarv*year # fixed effect in the model > reff <- ~year|plant # random effect in the model, where > year is the > dat.lme <- lme( > fixed = feff, # fixed effect in the > model > data = dat, > random = reff, # random effect in the > model > correlation = corAR1(form=~year|plant), # > subset = dead==FALSE & recTreat==FALSE, # > na.action = na.omit > ) > dat.lm <- lm( > formula = feff, # fixed effect in the model > data = dat, > subset = dead==FALSE & recTreat==FALSE, > na.action = na.omit > ) > > Thanks, > > Rainer > > -- > Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, > UCT), Dipl. Phys. (Germany) > > Centre of Excellence for Invasion Biology > Stellenbosch University > South Africa > > Tel : +33 - (0)9 53 10 27 44 > Cell: +33 - (0)6 85 62 59 98 > Fax : +33 - (0)9 58 10 27 44 > > Fax (D): +49 - (0)3 21 21 25 22 44 > > email: Rainer at krugs.de > > Skype: RMkrug > > ______________________________________________ > 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.-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/
?? I do not groc what you mean. ... subset == subs would work fine in your lm call. So unless someone else does get it, you may need to elaborate. In general, ?substitute, ?bquote, and ?quote are useful to avoid immediate evaluation of calls, but I don't know if that's relevant to what you want here. -- Bert On Tue, Aug 21, 2012 at 7:44 AM, Rainer M Krug <r.m.krug at gmail.com> wrote:> Hi > > I want to do a series of linear models, and would like to define the input > arguments for lm() as variables. I managed easily to define the formula > arguments in a variable, but I also would like to have the "subset" in a > variable. My reasoning is, that I have the subset in the results object. > > So I wiould like to add a line like: > > subs <- dead==FALSE & recTreat==FALSE > > which obviously does not work as the expression is evaluated immediately. Is > is it possible to do what I want to do here, or do I have to go back to use > > dat <- subset(dat, dead==FALSE & recTreat==FALSE) > > ? > > > > dat <- loadSPECIES(SPECIES) > feff <- height~pHarv*year # fixed effect in the model > reff <- ~year|plant # random effect in the model, where > year is the > dat.lme <- lme( > fixed = feff, # fixed effect in the > model > data = dat, > random = reff, # random effect in the > model > correlation = corAR1(form=~year|plant), # > subset = dead==FALSE & recTreat==FALSE, # > na.action = na.omit > ) > dat.lm <- lm( > formula = feff, # fixed effect in the model > data = dat, > subset = dead==FALSE & recTreat==FALSE, > na.action = na.omit > ) > > Thanks, > > Rainer > > -- > Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, > UCT), Dipl. Phys. (Germany) > > Centre of Excellence for Invasion Biology > Stellenbosch University > South Africa > > Tel : +33 - (0)9 53 10 27 44 > Cell: +33 - (0)6 85 62 59 98 > Fax : +33 - (0)9 58 10 27 44 > > Fax (D): +49 - (0)3 21 21 25 22 44 > > email: Rainer at krugs.de > > Skype: RMkrug > > ______________________________________________ > 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