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
glmgives " 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