SCM

Forum: help

Monitor Forum | Start New Thread Start New Thread
RE: model.frame in as.party needs global data? [ Reply ]
By: Achim Zeileis on 2016-03-20 08:09
[forum:43067]
Indeed, it is a model.frame() question but model.frame.rpart() is also provided by "partykit".

In any case, the source of the problem is the following: If the model.frame is not stored within the "rpart" object, then it has to be re-evaluated in the right environment. And the "right" environment is assumed to be environment(formula). This is needed for situations where the "rpart" object is fitted outside a function and model.frame/as.party is called inside a function. But in the situation that you are describing the local environment would be needed.

So there are two ways to fix the situation:

(1) You add another code line
environment(formula) <- environment()
before calling rpart().

(2) You store the model.frame inside the "rpart" object then it does not have to be re-evaluated
fit <- rpart(formula, data = iris2, model = TRUE)

In this situation, solution (2) seems preferable because then the model.frame just has to be built once.

model.frame in as.party needs global data? [ Reply ]
By: Markus Loecher on 2016-03-19 16:21
[forum:43066]
I am confused why the "local" (i.e. inside a function) call to as.party() seems to require access to the global data frame that was used to build the model, whereas it works fine in the global environment.
To be more clear, the following works great:

fit=rpart(Species ~ .,data = iris)
party_rp <- as.party(fit)

while:

LocalParty = function(formula, iris2){
fit=rpart(formula,data = iris2)
party_rp <- as.party(fit)
}
LocalParty(formula=Species ~ ., iris)

complains that "Error in is.data.frame(data) : object 'iris2' not found"

Hmm, I am worried this question is more about model.frame() than about as.party()

Thanks!
Markus

Thanks to:
Vienna University of Economics and Business Powered By FusionForge