boshao zhang
2007-Nov-26 14:08 UTC
[R] pass lm( ) a char vector as the variables to be included
Dear Everyone in list: I am writing some codes to automate the process of fitting linear models where the names of variables of models are produced and stored in character vectors. But I have problems to pass the vectors to the lm( ) because I don't know how to strip the quotation marks automatically. Here are the codes of the example of lm( ): ## Annette Dobson (1990) "An Introduction to Generalized Linear Models". ## Page 9: Plant Weight Data. ctl <- (4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt <- (4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group <- gl(2,10,20, labels=c("Ctl","Trt")) weight <- c(ctl, trt) anova(lm.D9 <- lm(weight ~ group)) lm.D90 <- lm(weight ~ group - 1) # omitting intercept What I am doing is let the variable name "group" stored in a vector, say, g <- "group". The question is how to strip the quotation marks when we call lm( ) through g? Thank you all. Boshao ____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page.
Richard.Cotton at hsl.gov.uk
2007-Nov-26 14:17 UTC
[R] pass lm( ) a char vector as the variables to be included
> Here are the codes of the example of lm( ): > > ## Annette Dobson (1990) "An Introduction to > Generalized Linear Models". > ## Page 9: Plant Weight Data. > ctl <- > (4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) > trt <- > (4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) > group <- gl(2,10,20, labels=c("Ctl","Trt")) > weight <- c(ctl, trt) > anova(lm.D9 <- lm(weight ~ group)) > lm.D90 <- lm(weight ~ group - 1) # omitting intercept > > What I am doing is let the variable name "group" > stored in a vector, say, g <- "group". The question is > how to strip the quotation marks when we call lm( ) > through g?Try: w = "weight" g = "group" form = as.formula(paste(w,g,sep="~")) lm(form) Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}