[R] using a string as the formula in rlm

Marc Schwartz MSchwartz at medanalytics.com
Thu Oct 2 17:50:56 CEST 2003


On Thu, 2003-10-02 at 10:25, Rajarshi Guha wrote:
> Hi,
>  I am trying to build a series of rlm models. I have my data frame and
> the models will be built using various coulmns of the data frame.
> 
> Thus a series of models would be
> 
> m1 <- rlm(V1 ~ V2 + V3 + V4, data)
> m2 <- rlm(V1 ~ V2 + V5 + V7, data)
> m3 <- rlm(V1 ~ V2 + V8 + V9, data)
> 
> I would like to automate this. Is it possible to use a string in place
> of the formula?
> 
> I tried doing:
> 
> fmla <- sprintf('V1 ~ V%g + V%g + V%g',2,3,4)
> m1 <-rlm(fmla,data)
> 
> I get:
> 
> Error in qr(x) : NA/NaN/Inf in foreign function call (arg 1)
> In addition: Warning message:
> NAs introduced by coercion
> 
> I can understand why this method results in the error but is there any
> way to get around this?
> 


See ?as.formula

You can then construct an expression and use something like:

m1 <- rlm(as.formula(expression), data)

HTH,

Marc Schwartz




More information about the R-help mailing list