R 2.2 on windows XP I have a dataset with multiple columns. Some of the columns represent independent variables, some represent dependent variables. I would like to run the same analyses on a fixed set of independent variables, changing only the dependent variable, e.g. y1-y2=x1+x2+x3 y3-y4=x1+x2+x3 y5-y6=x1+x2+x3, etc. I know I can write a function to perform the analyses, however in order to make the analyses easy to do, I really need a macro scripting language that will allow preprocessing of the function and substitution of macro variables with parameters passed to the macro, i.e. I need someting akin to the macro facility in SAS. Is there a macro facility that I can use with R? I have tried help.search("macro") and did not have any success. Thanks, John John Sorkin M.D., Ph.D. Chief, Biostatistics and Informatics Baltimore VA Medical Center GRECC, University of Maryland School of Medicine Claude D. Pepper OAIC, University of Maryland Clinical Nutrition Research Unit, and Baltimore VA Center Stroke of Excellence University of Maryland School of Medicine Division of Gerontology Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 410-605-7119 jsorkin at grecc.umaryland.edu
"John Sorkin" <jsorkin at grecc.umaryland.edu> writes:> R 2.2 on windows XP(that's a bit old...)> I have a dataset with multiple columns. Some of the columns represent > independent variables, some represent dependent variables. I would like > to run the same analyses on a fixed set of independent variables, > changing only the dependent variable, e.g. > y1-y2=x1+x2+x3 > y3-y4=x1+x2+x3 > y5-y6=x1+x2+x3, etc. > I know I can write a function to perform the analyses, however in order > to make the analyses easy to do, I really need a macro scripting > language that will allow preprocessing of the function and substitution > of macro variables with parameters passed to the macro, i.e. I need > someting akin to the macro facility in SAS. Is there a macro facility > that I can use with R? I have tried help.search("macro") and did not > have any success. > Thanks, > JohnSome people have indicated that they might want to try their hand and write a macro facility for R, at some time in the future. (There are some parts of the R internals that would benefit from such a facility too). Meanwhile, update() is your friend. mdl <- lm(y1-y2 ~ x1+x2+x3,....) summary(mdl) summary(update(mdl, y3 - y4 ~ .)) ...etc... -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On 7/3/2006 7:39 AM, John Sorkin wrote:> R 2.2 on windows XP > I have a dataset with multiple columns. Some of the columns represent > independent variables, some represent dependent variables. I would like > to run the same analyses on a fixed set of independent variables, > changing only the dependent variable, e.g. > y1-y2=x1+x2+x3 > y3-y4=x1+x2+x3 > y5-y6=x1+x2+x3, etc. > I know I can write a function to perform the analyses, however in order > to make the analyses easy to do, I really need a macro scripting > language that will allow preprocessing of the function and substitution > of macro variables with parameters passed to the macro, i.e. I need > someting akin to the macro facility in SAS. Is there a macro facility > that I can use with R? I have tried help.search("macro") and did not > have any success.I think the substitute function in R does most of what you would want from a macro facility. For example, > analyze <- function(dependent, data) { + formula <- as.formula(substitute(dep ~ x1 + x2 + x3, + list(dep=substitute(dependent)))) + lm(formula=formula, data=data) + } > > dat <- data.frame(y = rnorm(10)+1:10, x1=rnorm(10)+1:10, x2=rnorm(10), x3=rnorm(10)) > > analyze(y, dat) Call: lm(formula = formula, data = data) Coefficients: (Intercept) x1 x2 x3 1.1256 0.8248 0.1671 -0.1907 I used substitute twice: the inner call gets the unevaluated expression that was passed as "dependent"; the outer one puts that in place of the "dep" variable. Duncan Murdoch
John Sorkin wrote:> R 2.2 on windows XP > I have a dataset with multiple columns. Some of the columns represent > independent variables, some represent dependent variables. I would like > to run the same analyses on a fixed set of independent variables, > changing only the dependent variable, e.g. > y1-y2=x1+x2+x3 > y3-y4=x1+x2+x3 > y5-y6=x1+x2+x3, etc. > I know I can write a function to perform the analyses, however in order > to make the analyses easy to do, I really need a macro scripting > language that will allow preprocessing of the function and substitution > of macro variables with parameters passed to the macro, i.e. I need > someting akin to the macro facility in SAS. Is there a macro facility > that I can use with R? I have tried help.search("macro") and did not > have any success.Also try RSiteSearch("macro", restrict="functions"). And have a look at Thomas Lumley's column in the following issue of Rnews: http://cran.r-project.org/doc/Rnews/Rnews_2001-3.pdf> Thanks, > John > > John Sorkin M.D., Ph.D. > Chief, Biostatistics and Informatics > Baltimore VA Medical Center GRECC, > University of Maryland School of Medicine Claude D. Pepper OAIC, > University of Maryland Clinical Nutrition Research Unit, and > Baltimore VA Center Stroke of Excellence > > University of Maryland School of Medicine > Division of Gerontology > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > > 410-605-7119 > jsorkin at grecc.umaryland.edu > > ______________________________________________ > 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 >-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Try this: # test data set.seed(1) mat <- matrix(rnorm(900), nc = 9) colnames(mat) <- letters[1:9] DF <- as.data.frame(mat) # run lm's and display coefs for(i in seq(1, 5, 2)) { dat <- cbind(z = DF[,i] - DF[,i+1], DF[7:9]) Coef <- coef(lm(z ~., dat)) cat("y: DF[,", i, "] - DF[,", i+1, "], coef: ", Coef, "\n") } On 7/3/06, John Sorkin <jsorkin at grecc.umaryland.edu> wrote:> R 2.2 on windows XP > I have a dataset with multiple columns. Some of the columns represent > independent variables, some represent dependent variables. I would like > to run the same analyses on a fixed set of independent variables, > changing only the dependent variable, e.g. > y1-y2=x1+x2+x3 > y3-y4=x1+x2+x3 > y5-y6=x1+x2+x3, etc. > I know I can write a function to perform the analyses, however in order > to make the analyses easy to do, I really need a macro scripting > language that will allow preprocessing of the function and substitution > of macro variables with parameters passed to the macro, i.e. I need > someting akin to the macro facility in SAS. Is there a macro facility > that I can use with R? I have tried help.search("macro") and did not > have any success. > Thanks, > John > > John Sorkin M.D., Ph.D. > Chief, Biostatistics and Informatics > Baltimore VA Medical Center GRECC, > University of Maryland School of Medicine Claude D. Pepper OAIC, > University of Maryland Clinical Nutrition Research Unit, and > Baltimore VA Center Stroke of Excellence > > University of Maryland School of Medicine > Division of Gerontology > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > > 410-605-7119 > jsorkin at grecc.umaryland.edu > > ______________________________________________ > 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 >
Here is one user's perspective on this, with no pretense of being definitive. In the S (R) world, the expression "computing on the language" is used to encompass what I would call the tasks of macro programming. This involves uses of various S (R) expressions that convert between names of objects and the object themselves; pasting together expressions out of fixed and variable text; and executing them. I for one have been able to do everything I could do in the SAS macro language, but it has taken trial and error. To get started, you might consult section 3.5 of S Programming by Venables and Ripley. MHP John Sorkin wrote on 7/3/2006 7:39 AM:> R 2.2 on windows XP > I have a dataset with multiple columns. Some of the columns represent > independent variables, some represent dependent variables. I would like > to run the same analyses on a fixed set of independent variables, > changing only the dependent variable, e.g. > y1-y2=x1+x2+x3 > y3-y4=x1+x2+x3 > y5-y6=x1+x2+x3, etc. > I know I can write a function to perform the analyses, however in order > to make the analyses easy to do, I really need a macro scripting > language that will allow preprocessing of the function and substitution > of macro variables with parameters passed to the macro, i.e. I need > someting akin to the macro facility in SAS. Is there a macro facility > that I can use with R? I have tried help.search("macro") and did not > have any success. > Thanks, > John > > John Sorkin M.D., Ph.D. > Chief, Biostatistics and Informatics > Baltimore VA Medical Center GRECC, > University of Maryland School of Medicine Claude D. Pepper OAIC, > University of Maryland Clinical Nutrition Research Unit, and > Baltimore VA Center Stroke of Excellence > > University of Maryland School of Medicine > Division of Gerontology > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > > 410-605-7119 > jsorkin at grecc.umaryland.edu > > ______________________________________________ > 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 >-- Michael H. Prager, Ph.D. Population Dynamics Team NOAA Center for Coastal Habitat and Fisheries Research NMFS Southeast Fisheries Science Center Beaufort, North Carolina 28516 USA http://shrimp.ccfhrb.noaa.gov/~mprager/
On Mon, 3 Jul 2006, John Sorkin wrote:> R 2.2 on windows XP > I have a dataset with multiple columns. Some of the columns represent > independent variables, some represent dependent variables. I would like > to run the same analyses on a fixed set of independent variables, > changing only the dependent variable, e.g. > y1-y2=x1+x2+x3 > y3-y4=x1+x2+x3 > y5-y6=x1+x2+x3, etc.Other people have mentioned my defmacro() function in R-news, however when I have to do this sort of thing I usually use substitute or bquote For example for(i in 1+(1:5)*2){ A<-as.name(paste("y",i,sep="")) B<-as.name(paste("y",i+1,sep="") eval(bquote(lm(I(.(A)-.(B))~x1+x2+x3,data=dat))) } -thomas