[R] working with eval and environments

darckeen darckeen at hotmail.com
Sun Sep 19 17:10:59 CEST 2010


I'm trying to get the following section of code to work, I think the problem
is being caused by the assignment of data to the lm function not evaluating
to "train" in the parent environment but I can't seem to figure out how to
do this.

fitmodel <- function(trial,data)
{	
	wrap.lm <- function(formula,data,...) { cat("in wrap lm",NROW(data),"\n");
lm(formula,data,...) }
	wrap.step <- function(object,scope,...) { cat("in wrap
step",NROW(train),"\n"); step(object,scope,...) }
	
	train <- data[1:trial at n,]
	null <- as.formula("y ~ 1")
	full <- as.formula("y ~ 1 + x1 + x2")
	
	cat("in fit",NROW(train),"\n")
	model <- do.call(trial at fit$func,trial at fit$args)
	print(model$call)
	select <- do.call(trial at step$func,trial at step$args)
}

mydf <- data.frame(y <- runif(100),x1 <- runif(100),x2 <- runif(100))
myfit <- list(func="wrap.lm",args=alist(formula=null,data=train))
mystep <- list(func="wrap.step",args=alist(object=model,scope=full,trace=0))

setClass("trial",representation(n="numeric",fit="list",step="list"))
trial1 <- new("trial",n=50,fit=myfit,step=mystep)
trial2 <- new("trial",n=75,fit=myfit,step=mystep)

result <- lapply(list(trial1,trial2),fitmodel,mydf)
print(coef(result[[1]]))
print(coef(result[[2]]))
cat("result #1",NROW(result[[1]]$model),"\n")
cat("result #2",NROW(result[[2]]$model),"\n")


The resulting output is:
in fit 50 
in wrap lm 50 
lm(formula = formula, data = data)
in wrap step 50 
in fit 75 
in wrap lm 75 
lm(formula = formula, data = data)
in wrap step 75 
(Intercept) 
  0.5276266 
(Intercept) 
  0.5276266 
result #1 100 
result #2 100 

I think what is happening is that step is reassigning the "data" for lm to
the fitmodel environment thus using resulting in using 100 rows.  Any ideas
would be appreciated.
-- 
View this message in context: http://r.789695.n4.nabble.com/working-with-eval-and-environments-tp2545939p2545939.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list