Error in lm.wfit (rlm in MASS3): an scope problem?
I am trying to do a robust fitting with function rlm from V&R MASS3 (see
p.167-174) for several response variables (one at a time) and a single
predictor. My code is a for loop with a "template" formula in which I plug
iteratively each response variable. Residuals will be saved in a data frame,
along with an index ("orden"):
alargando <- data.frame("orden"=99,"residuo"=99)
for (i in c("ERIRUBc", "ALAARVc")) # a simplified example, the real character
vector has 60 more elements
{
tmp <- i
new.formula <- eval(parse(text=paste(tmp,'~ HAS',sep=" ")))
model <- rlm.formula(new.formula, data=tmp2, method="MM", init="lqs")
alargando <- rbind(alargando, data.frame("orden"=tmp2$orden,
"residuo"=resid(model)))
}
alargando <- alargando[(-1),] # delete the first spurious row
But I get the following error:
Error in lm.wfit: Missing value where logical needed: if(any(w < 0))
stop("negative weights not allowed")
But there should not be any problematic negative weights because I did not
state them and they are positive by default (w = rep(1, nrow(x), according to
the help for rlm). The response variables do not have missing values but, just
in case I overlooked, adding na.action="na.omit" to the call for rlm.formula
does not avoid the problem.
The loop works as expected if I type each step at a time, starting with, say,
tmp <- "ERIRUBc". I am afraid then that the code does not work due to a scoping
problem. I know that scoping issues recurr to often in the list, but after some
reading I was not able to fix the problem.
Any suggestion would be greatly appreciated.
Note:
> version
S-PLUS 2000 Professional Edition Release 2 for Microsoft Windows : 1999
|