Dear Sir or Madam: This is an extension to a earlier post, about looping through several thousand files, and testing student's models against a different data-set, called rclnow, for "recall now". The problem is, that the instructor never specified to the students, what they should name their "lm" object. So what they created was: "arbitrary variable name" <- lm(Y~(V1+V2+.... more stuff), learn_data_frame) Now there are 1500+ files that are named: "BIOtotals_students_name_class_name_date_other_info..." "BIOtotals_more_stuff_2.." "BIOtotal_more-stuff_3 ...................... ...................... Now I always get this error in the "predict" statement: Error in UseMethod("predict") : no applicable method for "predict" Is there a way to pass the unknown object name to predict, or do we need to ask the students to redo all the work with a given model name? (I hope not!) function(L) { fname <- format(Sys.time(), 'Totals_%b%d_%H%M_%S.txt') filesused <- format(Sys.time(), 'Files_%b%d_%H%M_%S.txt') setwd("/Users/gregg/R_system") dire <- getwd() print(dire) flies <- list.files(pattern = "BIOtotal*")# flies instead of files because of FEAR of reserved words!!! for(i in 1:length(flies)) { s <- flies[i] print(i) # print everything for debugging print(s) model <- load(s) print(model) z <- predict( model ,rclnow) ############## what will model be????? print(z) write( z, fname , append = TRUE, ncolumns = L) write( as.character(s) , filesused , append = TRUE, ncolumns = 1 ) } } THANKS for any help! Greg Allen
On Apr 24, 2009, at 9:30 PM, greggallen at gmail.com wrote:> Dear Sir or Madam: > > This is an extension to a earlier post, about looping through several > thousand files, and testing student's models against a different > data-set, called rclnow, for "recall now". > > The problem is, that the instructor never specified to the students, > what they should name their "lm" object. > > So what they created was: > "arbitrary variable name" <- lm(Y~(V1+V2+.... more stuff), > learn_data_frame)That would have created an object, but would not have determined what it would be called on the disk when it was saved.> > > Now there are 1500+ files that are named: > "BIOtotals_students_name_class_name_date_other_info..." > "BIOtotals_more_stuff_2.." > "BIOtotal_more-stuff_3 > ...................... > ......................Right, but you do not know what the names of the models will be when they are loaded.> > > > Now I always get this error in the "predict" statement: > > Error in UseMethod("predict") : no applicable method for "predict"Perhaps because you have not named the object properly?> > > Is there a way to pass the unknown object name to predict, or do we > need to ask the students to redo all the work with a given model name? > (I hope not!)Can set up the workspace so that you assign the only lm object to a standardized name and then operate on that name?> > function(L) ### what is L? > { > fname <- format(Sys.time(), 'Totals_%b%d_%H%M_%S.txt') > filesused <- format(Sys.time(), 'Files_%b%d_%H%M_%S.txt') > setwd("/Users/gregg/R_system") > dire <- getwd() > print(dire) > flies <- list.files(pattern = "BIOtotal*")# flies instead of files > because of FEAR of reserved words!!! > for(i in 1:length(flies)) > { > s <- flies[i] > print(i) # print everything for debugging > print(s) > model <- load(s)Probably need to lapply(flies, load) and let them be named whatever they are named> > print(model)Probably now need to look at the workspace with ls(...) and somehow get the names of all the objects of class lm. Then lapply a predict function tomembers of that list> > z <- predict( model ,rclnow) ############## what will model > be????? #see aboveHow will you know whose model is which, ... do do you care?> > print(z) > write( z, fname , append = TRUE, ncolumns = L) > write( as.character(s) , filesused , append = TRUE, ncolumns = 1 ) > } > } >David Winsemius, MD Heritage Laboratories West Hartford, CT
On 24/04/2009 9:30 PM, greggallen at gmail.com wrote:> Dear Sir or Madam: > > This is an extension to a earlier post, about looping through several > thousand files, and testing student's models against a different > data-set, called rclnow, for "recall now". > > The problem is, that the instructor never specified to the students, > what they should name their "lm" object. > > So what they created was: > "arbitrary variable name" <- lm(Y~(V1+V2+.... more stuff), learn_data_frame) > > Now there are 1500+ files that are named: > "BIOtotals_students_name_class_name_date_other_info..." > "BIOtotals_more_stuff_2.." > "BIOtotal_more-stuff_3 > ...................... > ...................... > > > Now I always get this error in the "predict" statement: > > Error in UseMethod("predict") : no applicable method for "predict" > > Is there a way to pass the unknown object name to predict, or do we > need to ask the students to redo all the work with a given model name? > (I hope not!)predict() doesn't want an object name, it wants an object. load() returns an object name, and loads the object. So you can find the object by the name that gets loaded. If the students only created one object, you could do it as name <- load(s) model <- get(name) but that's dangerous: if you don't know what the student named the model, then you don't want to load it in with all your other variables. So a safer way to go is: studentvars <- new.env() names <- load(s, studentvars) Now figure out which of the names is the model (say names[1]), and do model <- get(names[1], envir=studentvars) Duncan Murdoch> > function(L) > { > fname <- format(Sys.time(), 'Totals_%b%d_%H%M_%S.txt') > filesused <- format(Sys.time(), 'Files_%b%d_%H%M_%S.txt') > setwd("/Users/gregg/R_system") > dire <- getwd() > print(dire) > flies <- list.files(pattern = "BIOtotal*")# flies instead of files > because of FEAR of reserved words!!! > for(i in 1:length(flies)) > { > s <- flies[i] > print(i) # print everything for debugging > print(s) > model <- load(s) > print(model) > z <- predict( model ,rclnow) ############## what will model be????? > print(z) > write( z, fname , append = TRUE, ncolumns = L) > write( as.character(s) , filesused , append = TRUE, ncolumns = 1 ) > } > } > > > THANKS for any help! > > Greg Allen > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.