[R] updating glm
vito muggeo
vito.muggeo at giustizia.it
Fri Feb 28 11:11:02 CET 2003
Dear all,
my function fn() (see code below) just takes a glm object and updates it by
including a function of a specified variable in dataframe.
x<-1:50
y<-rnorm(50)
d<-data.frame(yy=y,xx=x);rm(x,y)
o<-glm(yy~xx,data=d)
> fn(obj=o,x=xx)
Call: glm(formula = yy ~ xx + x1, data = obj$data)
....[SNIP]....
It works but I have two problems:
1)In the returning glm object the name of the dataframe is "obj$data", but I
would like to get the original one, i.e. "d";
2)fn() does not work if glm() is called without the data argument, namely
> attach(d)
> o<-glm(yy~xx)
> fn(obj=o,x=xx)
Error in eval(expr, envir, enclos) : Object "x1" not found
Here there seems to be some problem with the scoping rule; could you suggest
me how solve this problem? Also I'm almost sure that there exist a much more
elegant solution instead of the mine used; I tried get(), environment() and
friends, but without any success....Could you suggest me anything?
thanks in advance,
vito
#########
function(obj,x){
if(!is.null(obj$call$data)){
attach(eval(obj$call$data))
on.exit(detach(eval(obj$call$data)))
}
x1<-ifelse(x>mean(x),1,0)
if(!is.null(obj$call$data)) obj$data$x1<-x1
obj1<-update(obj,.~.+x1,data=obj$data)
return(obj1)
}
More information about the R-help
mailing list