0
votes

I wrote a script that for every Id i build a model.
Data is a data.frame, that for every Id i have several rows that i can build for him a model. so dlply take this subset of data.frame for each Id and build for him a model.

model<- dlply(Data, "Id", 
               function(df) {
                 HistoryWeights<-1+log(length(df$Row))
                 model<-glm(formula = form,family = binomial("logit"),data = df,weights = HistoryWeights)
                 return(model)
                 })

The problem is that if i run the script without the weights all fine. But if i add the weights in glm model this return me: Error in eval(expr, envir, enclos) : object 'HistoryWeights' not found

Here is a simple example with iris:

Data<-iris
Data$Predicted<- ceiling(rnorm(dim(Data)[1],0,0.00001))
Data$Row<-1:nrow(Data)
form<-formula(Predicted~Sepal.Length
              +Sepal.Width
              +Petal.Length
              +Petal.Width)
model<- dlply(Data, "Species", 
              function(df) {
                HistoryWeights<-1+log(length(df$Row))
                model<-glm(formula = form,family = binomial("logit"),data = df,weights = HistoryWeights)
                return(model)
              })

What is the problem?
Thanks

1
The help page for glm gives " If not found in data, the variables are taken from environment(formula), typically the environment from which glm is called." Note: it searches for HistoryWeights in the environment of the formula, not the calling environment of glm. - kasterma
@kasterma So what should i do? - Dima Ha
The answer @Spacedman gives works, the other is to define the function in the function so that your HistoryWeights is defined in the environment where the formula is defined. - kasterma

1 Answers

3
votes

In your example, put HistoryWeights as a column in the data frame:

model<- dlply(Data, "Species", 
              function(df) {
                df$HistoryWeights<-1+log(length(df$Row))
                model<-glm(formula = form,family = binomial("logit"),data = df,weights = HistoryWeights)
                return(model)
              })