Hi I collect a list of calls to a package in a function (routine) so that I do not need to repeat the same sets of codes from program to program. In the following, inserting the function into each program works. Then, I place the function elsewhere in a PC folder, and include in with a 'source' command. This does not work; it complains about a function (fn below) not defined. Compiling the function into a library file does not work either (with all sorts of error messages saying this and that not defined). Steven Yen fn <- function(beta){ f<-... (define f in this routine) return(f) } max.calls<-function(method){ # ******************************************************* # Call maxLike with alternative algorithms # ******************************************************* many<-c("NR","BFGS","BFGSR","BHHH","SANN","CG","NM") if (method %in% many){ ML<-maxLik(logLik=fn,grad=NULL,hess=NULL,start, method,print.level,constraints=NULL, ...)} return(ML) } # This works: ML<-max.calls(method) # Does not work: source("z:\\R\\yenlib\\lib\\max.calls.R") ML<-max.calls(method) Error: object 'fn' not found [[alternative HTML version deleted]]
You did put the declaration of the function fn into the file you are sourcing, didn't you? If it were me I would 1 - make fn a parameter of max.calls 2 - use the ellipsis ... so I could pass other arguments in to MaxLike 3 - fix the errors I got from making it a package. It does not lie when it tells you things are undefined and, who knows, they may need to be defined. On 03/10/2015 09:47, Steven Yen wrote:> Hi > I collect a list of calls to a package in a function (routine) so that I do > not need to repeat the same sets of codes from program to program. In the > following, inserting the function into each program works. Then, I place > the function elsewhere in a PC folder, and include in with a 'source' > command. This does not work; it complains about a function (fn below) not > defined. > Compiling the function into a library file does not work either (with all > sorts of error messages saying this and that not defined). > Steven Yen > > fn <- function(beta){ > f<-... (define f in this routine) > return(f) > } > > max.calls<-function(method){ > # ******************************************************* > # Call maxLike with alternative algorithms > # ******************************************************* > many<-c("NR","BFGS","BFGSR","BHHH","SANN","CG","NM") > if (method %in% many){ > ML<-maxLik(logLik=fn,grad=NULL,hess=NULL,start, > method,print.level,constraints=NULL, ...)} > return(ML) > } > # This works: > ML<-max.calls(method) > > # Does not work: > source("z:\\R\\yenlib\\lib\\max.calls.R") > ML<-max.calls(method) > > Error: object 'fn' not found > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >-- Michael http://www.dewey.myzen.co.uk/home.html
Thanks Michael. I am a new hand with R so this is over my head. I will slowly explore all options suggested but for now I'd glad to get one option to work. How do you 'declare the function into the routine I am sourcing, i.e., max.calls? Is there something I can read? On Sat, Oct 3, 2015 at 8:47 AM, Michael Dewey <lists at dewey.myzen.co.uk> wrote:> You did put the declaration of the function fn into the file you are > sourcing, didn't you? > > If it were me I would > 1 - make fn a parameter of max.calls > 2 - use the ellipsis ... so I could pass other arguments in to MaxLike > 3 - fix the errors I got from making it a package. It does not lie when it > tells you things are undefined and, who knows, they may need to be defined. > > > On 03/10/2015 09:47, Steven Yen wrote: > >> Hi >> I collect a list of calls to a package in a function (routine) so that I >> do >> not need to repeat the same sets of codes from program to program. In the >> following, inserting the function into each program works. Then, I place >> the function elsewhere in a PC folder, and include in with a 'source' >> command. This does not work; it complains about a function (fn below) not >> defined. >> Compiling the function into a library file does not work either (with all >> sorts of error messages saying this and that not defined). >> Steven Yen >> >> fn <- function(beta){ >> f<-... (define f in this routine) >> return(f) >> } >> >> max.calls<-function(method){ >> # ******************************************************* >> # Call maxLike with alternative algorithms >> # ******************************************************* >> many<-c("NR","BFGS","BFGSR","BHHH","SANN","CG","NM") >> if (method %in% many){ >> ML<-maxLik(logLik=fn,grad=NULL,hess=NULL,start, >> method,print.level,constraints=NULL, ...)} >> return(ML) >> } >> # This works: >> ML<-max.calls(method) >> >> # Does not work: >> source("z:\\R\\yenlib\\lib\\max.calls.R") >> ML<-max.calls(method) >> >> Error: object 'fn' not found >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. >> >> > -- > Michael > http://www.dewey.myzen.co.uk/home.html >[[alternative HTML version deleted]]
Does an object called 'fn' exist anywhere after you call source()? Start looking by typing fn and see if it is the global environment. You will have to show what is in the file "z:\\R\\yenlib\\lib\\max.calls.R". Some people like to start such files with things like remove(list=objects()) (This is a really bad practice, but I've seen it on this list.) Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Oct 3, 2015 at 1:47 AM, Steven Yen <syen04 at gmail.com> wrote:> Hi > I collect a list of calls to a package in a function (routine) so that I do > not need to repeat the same sets of codes from program to program. In the > following, inserting the function into each program works. Then, I place > the function elsewhere in a PC folder, and include in with a 'source' > command. This does not work; it complains about a function (fn below) not > defined. > Compiling the function into a library file does not work either (with all > sorts of error messages saying this and that not defined). > Steven Yen > > fn <- function(beta){ > f<-... (define f in this routine) > return(f) > } > > max.calls<-function(method){ > # ******************************************************* > # Call maxLike with alternative algorithms > # ******************************************************* > many<-c("NR","BFGS","BFGSR","BHHH","SANN","CG","NM") > if (method %in% many){ > ML<-maxLik(logLik=fn,grad=NULL,hess=NULL,start, > method,print.level,constraints=NULL, ...)} > return(ML) > } > # This works: > ML<-max.calls(method) > > # Does not work: > source("z:\\R\\yenlib\\lib\\max.calls.R") > ML<-max.calls(method) > > Error: object 'fn' not found > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Thanks Bill. Simplified content of max.calls.R (with repeated calls to maxLik removed) are shown below in the message. No, fn does not exist in the environment. I call a routine (say probit.R compiled into a library) to use maxLik. Inside this routine, 1. In probit.R. likelihood function is defined yet in another nested routine; 2. Function "max.calls" is also nested in that probit.R; Then, a call to max.calls works. What I am trying to accomplish is, instead of inserting the identical function (or set of lines) in every routine like probit.R, I like to either compile max.calls.R or source it from inside probit.R. Thanks. On Sat, Oct 3, 2015 at 4:47 AM, Steven Yen <syen04 at gmail.com> wrote:> Hi > I collect a list of calls to a package in a function (routine) so that I > do not need to repeat the same sets of codes from program to program. In > the following, inserting the function into each program works. Then, I > place the function elsewhere in a PC folder, and include in with a 'source' > command. This does not work; it complains about a function (fn below) not > defined. > Compiling the function into a library file does not work either (with all > sorts of error messages saying this and that not defined). > Steven Yen > > fn <- function(beta){ > f<-... (define f in this routine) > return(f) > } > > max.calls<-function(method){ > # ******************************************************* > # Call maxLike with alternative algorithms > # ******************************************************* > many<-c("NR","BFGS","BFGSR","BHHH","SANN","CG","NM") > if (method %in% many){ > ML<-maxLik(logLik=fn,grad=NULL,hess=NULL,start, > method,print.level,constraints=NULL, ...)} > return(ML) > } > # This works: > ML<-max.calls(method) > > # Does not work: > source("z:\\R\\yenlib\\lib\\max.calls.R") > ML<-max.calls(method) > > Error: object 'fn' not found > >[[alternative HTML version deleted]]
In line On 03/10/2015 23:56, Steven Yen wrote:> Thanks Bill. Simplified content of max.calls.R (with repeated calls to > maxLik removed) are shown below in the message. No, fn does not exist in > the environment.Which explains why R cannot find it. I call a routine (say probit.R compiled into a library) to> use maxLik. Inside this routine, > 1. In probit.R. likelihood function is defined yet in another nested > routine;If I understand you correctly it is that function which you need to pass to max.calls. So alter max.calls as below> 2. Function "max.calls" is also nested in that probit.R; > Then, a call to max.calls works. > > What I am trying to accomplish is, instead of inserting the identical > function (or set of lines) in every routine like probit.R, I like to either > compile max.calls.R or source it from inside probit.R. Thanks. > > > On Sat, Oct 3, 2015 at 4:47 AM, Steven Yen <syen04 at gmail.com> wrote: > >> Hi >> I collect a list of calls to a package in a function (routine) so that I >> do not need to repeat the same sets of codes from program to program. In >> the following, inserting the function into each program works. Then, I >> place the function elsewhere in a PC folder, and include in with a 'source' >> command. This does not work; it complains about a function (fn below) not >> defined. >> Compiling the function into a library file does not work either (with all >> sorts of error messages saying this and that not defined). >> Steven Yen >> >> fn <- function(beta){ >> f<-... (define f in this routine) >> return(f) >> } >> >> max.calls<-function(method){max.calls <- function(method, fn = NULL) { if(is.null(fn)) warning("You forgot to supply fn)>> # ******************************************************* >> # Call maxLike with alternative algorithms >> # ******************************************************* >> many<-c("NR","BFGS","BFGSR","BHHH","SANN","CG","NM") >> if (method %in% many){ >> ML<-maxLik(logLik=fn,grad=NULL,hess=NULL,start, >> method,print.level,constraints=NULL, ...)} >> return(ML) >> } >> # This works: >> ML<-max.calls(method)Now in probit.R replace the call to max.calls(method) with max.calls(method, whatever_function_you defined_in_probit.R_for_the_likelihood) Of course I may have completely misunderstood what you are driving at.>> >> # Does not work: >> source("z:\\R\\yenlib\\lib\\max.calls.R") >> ML<-max.calls(method) >> >> Error: object 'fn' not found >> >> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >-- Michael http://www.dewey.myzen.co.uk/home.html
Does the following pattern resemble what you have? Here are a couple of functions that use the same code for finding the location of the minimum of a function fn: f1 <- function(x) { fn <- function(beta) { sum((x-beta)^2) } argMinFn <- function() { beta <- seq(0, 1, len=129) beta[which.min(vapply(beta, fn, 0))] } argMinFn() } f2 <- function(x) { fn <- function(beta) { sum(abs(x-beta)) } argMinFn <- function() { beta <- seq(0, 1, len=129) beta[which.min(vapply(beta, fn, 0))] } argMinFn() } used as > f1(1/(1:10)) # approx. mean of 1/(1:10) [1] 0.2890625 > f2(1/(1:10)) # approx median of 1/(1:10) [1] 0.171875 I think you are trying to avoid copying that argMinFn into every function of this sort so you move it out of the f1 and f2 functions: argMinFn.bad <- function() { beta <- seq(0, 1, len=129) beta[which.min(vapply(beta, fn, 0))] } and omit it from f1 and f2 f1a.bad <- function(x) { fn <- function(beta) { sum((x-beta)^2) } argMinFn.bad() } Then you get the sort of error you describe > f1a.bad(1/(1:10)) Error in match.fun(FUN) : object 'fn' not found The problem is that a function first looks for objects defined in its own environment, then in the environment in which the function was created, then the parent of that environment, etc. The stand-along argMinFn was defined in the global environment so it does not look in the environment of f1a.bad for fn. The fix is to make fn an argument to the stand-along argMinFn and have f1a.good pass fn into the environment of argMinFn via the argument list. argMinFn.good <- function(fn) { beta <- seq(0, 1, len=129) beta[which.min(vapply(beta, fn, 0))] } f1a.good <- function(x) { fn <- function(beta) { sum((x-beta)^2) } argMinFn.good(fn) } e.g > f1a.good(1/(1:10)) [1] 0.2890625 How functions look up variables is called 'scoping' and R's method is called 'lexical scoping'. You can find lots more information on this with google. Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Oct 3, 2015 at 3:56 PM, Steven Yen <syen04 at gmail.com> wrote:> Thanks Bill. Simplified content of max.calls.R (with repeated calls to > maxLik removed) are shown below in the message. No, fn does not exist in > the environment. I call a routine (say probit.R compiled into a library) to > use maxLik. Inside this routine, > 1. In probit.R. likelihood function is defined yet in another nested > routine; > 2. Function "max.calls" is also nested in that probit.R; > Then, a call to max.calls works. > > What I am trying to accomplish is, instead of inserting the identical > function (or set of lines) in every routine like probit.R, I like to either > compile max.calls.R or source it from inside probit.R. Thanks. > > > On Sat, Oct 3, 2015 at 4:47 AM, Steven Yen <syen04 at gmail.com> wrote: > >> Hi >> I collect a list of calls to a package in a function (routine) so that I >> do not need to repeat the same sets of codes from program to program. In >> the following, inserting the function into each program works. Then, I >> place the function elsewhere in a PC folder, and include in with a 'source' >> command. This does not work; it complains about a function (fn below) not >> defined. >> Compiling the function into a library file does not work either (with all >> sorts of error messages saying this and that not defined). >> Steven Yen >> >> fn <- function(beta){ >> f<-... (define f in this routine) >> return(f) >> } >> >> max.calls<-function(method){ >> # ******************************************************* >> # Call maxLike with alternative algorithms >> # ******************************************************* >> many<-c("NR","BFGS","BFGSR","BHHH","SANN","CG","NM") >> if (method %in% many){ >> ML<-maxLik(logLik=fn,grad=NULL,hess=NULL,start, >> method,print.level,constraints=NULL, ...)} >> return(ML) >> } >> # This works: >> ML<-max.calls(method) >> >> # Does not work: >> source("z:\\R\\yenlib\\lib\\max.calls.R") >> ML<-max.calls(method) >> >> Error: object 'fn' not found >> >> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.