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
############################ Original message:
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.
|