Skip to content
Prev 30944 / 398506 Next

using update( ) in a function?

On Mon, 21 Apr 2003, Roger Peng wrote:

            
No, the code as given doesn't get as far as that
The main problem here is that the argument `x' is only going to have the
*value* of x1, where you need the symbol x1.  On top of that,
	update(fm, x ~ . -x,data=df)
uses the symbol `x' rather than its value, so even if its value were
correct it wouldn't work.

This works
+ ff<-substitute(term~.-term)
+ update(model,ff,data=data)
+ }
Call:
lm(formula = Height ~ Volume, data = data)

Coefficients:
(Intercept)       Volume
    69.0034       0.2319

but is not recommended style, as you are defeating the call-by-value
semantics of R.  A better version is
+ ff<-substitute(x~.-x,list(x=as.name(term)))
+  update(model,ff,data=data)
+ }
Call:
lm(formula = Height ~ Volume, data = data)

Coefficients:
(Intercept)       Volume
    69.0034       0.2319

and you could have versions that allowed `term' to be a quoted name or
expression rather than a string.

	-thomas