Dear all I have a work-flow issue with lm(). When I use> lm(y1~x1, anscombe)Call: lm(formula = y1 ~ x1, data = anscombe) Coefficients: (Intercept) x1 3.0001 0.5001 I get as expected the formula, "y1 ~ x1", in the print()ed results or summary(). However, if I pass through a formula object> (form <- formula(y1~x1))y1 ~ x1> lm(form, anscombe)Call: lm(formula = form, data = anscombe) Coefficients: (Intercept) x1 3.0001 0.5001 then I only get the object name in the call, 'form', instead of the formula. When passing a 'formula' object to lm, is it possible to retain in the resulting 'lm' object the actual formula used for the regression: "y1 ~ x1"? Regards Liviu -- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
On 24/11/2011 2:48 PM, Liviu Andronic wrote:> Dear all > I have a work-flow issue with lm(). When I use > > lm(y1~x1, anscombe) > > Call: > lm(formula = y1 ~ x1, data = anscombe) > > Coefficients: > (Intercept) x1 > 3.0001 0.5001 > > > I get as expected the formula, "y1 ~ x1", in the print()ed results or > summary(). However, if I pass through a formula object > > (form<- formula(y1~x1)) > y1 ~ x1 > > lm(form, anscombe) > > Call: > lm(formula = form, data = anscombe) > > Coefficients: > (Intercept) x1 > 3.0001 0.5001 > > > then I only get the object name in the call, 'form', instead of the > formula. When passing a 'formula' object to lm, is it possible to > retain in the resulting 'lm' object the actual formula used for the > regression: "y1 ~ x1"?It is retained. terms(fit) will give it to you, if fit is an lm object. Duncan Murdoch
Gabor Grothendieck
2011-Nov-24 22:32 UTC
[R] proper work-flow with 'formula' objects and lm()
On Thu, Nov 24, 2011 at 2:48 PM, Liviu Andronic <landronimirc at gmail.com> wrote:> Dear all > I have a work-flow issue with lm(). When I use >> lm(y1~x1, anscombe) > > Call: > lm(formula = y1 ~ x1, data = anscombe) > > Coefficients: > (Intercept) ? ? ? ? ? x1 > ? ? 3.0001 ? ? ? 0.5001 > > > I get as expected the formula, "y1 ~ x1", in the print()ed results or > summary(). However, if I pass through a formula object >> (form <- formula(y1~x1)) > y1 ~ x1 >> lm(form, anscombe) > > Call: > lm(formula = form, data = anscombe) > > Coefficients: > (Intercept) ? ? ? ? ? x1 > ? ? 3.0001 ? ? ? 0.5001 > > > then I only get the object name in the call, 'form', instead of the > formula. When passing a 'formula' object to lm, is it possible to > retain in the resulting 'lm' object the actual formula used for the > regression: "y1 ~ x1"? >Try this:> form <- formula(y1~x1) > do.call("lm", list(form, quote(anscombe)))Call: lm(formula = y1 ~ x1, data = anscombe) Coefficients: (Intercept) x1 3.0001 0.5001 -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com