Many thanks to Bert Gunter, Santiago Pérez and Spencer Graves, who finally
solved my recent question about function rlm (V&R MASS3) inside a loop (see
previous message below). As he suggested, the function expected an actual
"formula" object, not the text version of it, so the tricky line is:
new.formula <- formula(paste(tmp, "~ HAS", sep = " "))
Now the corrected code that works is:
for(i in 1:length(nombres)) {
tmp <- nombres[i]
new.formula <- formula(paste(tmp, "~ HAS", sep = " "))
assign("model",rlm(eval(new.formula), data=tmp2,method="MM",init="lqs"))
alargando <- rbind(alargando, data.frame(orden = tmp2$orden, residuo =
resid(model)))
}
Javier,
----- Original Message -----
From: "Javier Seoane" <seoane@ebd.csic.es>
To: <s-news@lists.biostat.wustl.edu>
Sent: Friday, April 25, 2003 9:16 AM
Subject: [S] I can't make rlm to accept a response variable in a loop. Was:
Error in lm.wfit (rlm in MASS3): an scope problem?
I have recently posted a message about rlm function of V&R library mass (MASS3,
see p.167-174). My original message follows below. Thanks to Bert Gunter and
Santiago Pérez, who helped me out, I keep trying some alternatives and checking
problems. Finally, it seems that rlm function does not accept text inside a
loop as others function do, since the problematic code works finely with "lm"
or "glm", but not with "rlm".
# following Santiago's code:
nombres <- c("AEGCAUc", "ALAARVc")
# data simulation
HAS<-1:10
AEGCAUc<- 3+ceiling(.4*HAS+rnorm(1,0,2))
ALAARVc<- 7+ceiling(.2*HAS+rnorm(1,0,2))
orden<-11:20
tmp2<-data.frame(orden,HAS,AEGCAUc,ALAARVc)
alargando<-NULL
for(i in 1:length(nombres)) {
tmp <- nombres[i]
new.formula <- paste(tmp, "~ HAS", sep = " ")
assign("model",lm(eval(new.formula), data = tmp2))
alargando <- rbind(alargando, data.frame(orden = tmp2$orden, residuo =
resid(model)))
}
this code works but changing the model type to "rlm" does not (by the way, it
does not work either with "lqs", another method for robust fitting in mass). So
it seems that the problem is due to a particularity of the function rlm (or
lqs). A thorough reading of the help made me try some alternatives such as:
assign("model",rlm(formula=eval(new.formula), rlm.default(x=tmp2[,"HAS"],
y=tmp2[,tmp]),data = tmp2))
but with no result.
Well, it may seem a scope problem inside rlm function itself, so I tried
(following Bert's suggestions) to add the line:
assign("tmp2",tmp2,where=1)
before the call to rlm (i.e., inside the loop). It did not work. I tried also
to attach tmp2 before the loop and omit the data= argument in the rlm call, but
it did not work. I do not seem to have any other object called "tmp2" that
could mask the one of interest now.
Again, I would appreciate any comments as to this regard.
Javier Seoane
|