Here is the code:
data_complete<-read.delim("D:/Work/output_java_head.txt") #complete data set #working
modelfn<-function(data_complete){
model<-lm(mctr~price+age_group+gender+brand+product_typeid+google_product_category,data=data_complete)
data_complete$predicted<-predict(model,data_complete)
return(data_complete$predicted)
sink()
write.csv("D:/Work/output",i,".csv")
rm(model)
gc(TRUE)
} #working
Then using this command:
by(data_complete,data_complete$google_product_category,modelfn)
I got this error:
Error in
contrasts<-
(*tmp*
, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels
Please note that there are 117 levels in data_complete$google_product_category
and in all there are 22 columns in data_complete
.
Also I used for testing:
by(data_complete,data_complete$google_product_category,summary)
It gave me right answer.
So I guess there is some problem in the function modelfn
I have created.