The general formula is y ~ a + b + c + ... There is this approach: formula <- reformulate(independent_vars, response = "y") model <- lm(formula, data = mydata) summary(model)
It does not generate a string object, but the formula is still a string even if it is of class formula. Also, in this approach you only get + and if you want interactions or such you will need to code them into independent_vars. This technically satisfies the parameters as I understand them, but it is unsatisfying to me because it is playing with semantics. If I cannot generate a string (no matter what you call it) then I cannot get to y ~ a + b + c + ...and without that I do not have a model. An alternative: Someone could write a function. Say I am using lm() so the function will take a vector, a data frame, and a symbol. It will put them together (as a black box) and spit out the answer. You will never see the string, so it satisfies that requirement. However, it will generate a string internally to make everything work. Again, playing with semantics. I suggest that as stated the problem does not have a solution in a meaningful way. The best I can do is to try to hide the string from you. Tim -----Original Message----- From: R-help <r-help-boun...@r-project.org> On Behalf Of Bert Gunter Sent: Saturday, March 29, 2025 6:40 PM To: Richard M. Heiberger <r...@temple.edu>; R-help <R-help@r-project.org> Subject: Re: [R] [External] Creating model formulas programmatically [External Email] Thanks, Rich. I thought of that, too, but it violates the spirit of my restraints (to avoid character strings), which I unfortunately did not clearly articulate. So my apologies for that failure. My concern is that with more complex model formula, using as.formula, etc. to parse/convert character strings can get a bit hairy. But in most cases, as here maybe, it may be perfectly fine. So think of my post as mostly my attempt to learn some new tricks rather than to solve a useful problem. I hope this is not unfair to the list. Cheers, Bert On Sat, Mar 29, 2025 at 3:12 PM Richard M. Heiberger <r...@temple.edu> wrote: > > somenames <- c("Heigh", "Ho", "Silver", "Away") > > as.formula(paste("~(",paste(somenames, collapse="+"),")^2")) > ~(Heigh + Ho + Silver + Away)^2 > > > > > On Mar 29, 2025, at 14:30, Bert Gunter <bgunter.4...@gmail.com> wrote: > > > > somenames <- c("Heigh", "Ho", "Silver", "Away") > > > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.