Dear Bert: Thank you for your reply. But the "eval.parent" function does not seem to solve the problem. Below is the code. I have tried running it with
eval.parent(myknot) as well as with integers from 0 through 4, as in eval.parent(myknot, 4) Each attempt produces the same error: Error in eval(expr, p) : object "myknot" not found I'm replying to the list in case someone can explain this. Jake fnJunk <-function(myknot=44 , myMER ) { # Fit lmer (mer, lme4) models for WSER project, using spline (ns), and plot them for newdata. library(lme4) library(splines) if(missing(myMER)){ cat("myknot=", myknot) cat("fit another MER??") readline() myMER<-lmer(formula=Finished ~ Sex01 + ns(DAT$Age, knots= eval.parent(myknot, 0) ) + MinCC + PzC + (1 | NAME ) + (1 | Year), data=(DAT), family="binomial") } # more stuff below here } On Thu, Jul 17, 2008 at 6:19 PM, Bert Gunter <[EMAIL PROTECTED]> wrote: > As you may be aware, this is a case of FAQ for R 3.31, lexicographic > scoping. It **can** be tricky, especially in a complex situation like > yours. > Typically eval() and/or substitute are what you need in such situations, to > force evaluation in the appropriate environment. So try changing your > > ns(Dat$Age,knots=myknot) > > ## to > > ns(Dat$Age,knots=eval.parent(myknot)) > > If that doesn't work, await wiser advice. > > Cheers, > > Bert Gunter > Genentech > > -----Original Message----- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > On > Behalf Of Jacob Wegelin > Sent: Thursday, July 17, 2008 1:58 PM > To: [EMAIL PROTECTED] > Subject: [R] nested calls, variable scope > > Below is an example of a problem I encounter repeatedly when I write > functions. A call works at the command line, but it does not work inside a > function, even when I have made sure that all required variables are > available within the function. The only way I know to solve it is to make > the required variable global, which of course is dangerous. What is the > elegant or appropriate way to solve this? > > > # The call to ns works at the command line: > > > junk<-ns(DAT$Age, knots=74) > > # The call to lmer works at the command line, with the call to ns nested > within it: > > > junk2<-lmer(formula=Finished ~ Sex01 + ns(DAT$Age, knots= 74 ) + MinCC + > PzC + (1 | NAME ) + (1 | Year), data=myDATonly, family="binomial") > > But now I want to do this within a function such as the following: > > > myfn > function(myknot=74) { > library(lme4) > library(splines) > cat("myknot=", myknot, "\n") > myMER<-lmer(formula=Finished ~ Sex01 + ns(DAT$Age, knots= myknot) + > MinCC + PzC + (1 | NAME ) + (1 | Year), data=myDATonly, family="binomial") > } > > > myfn(74) > myknot= 74 > Error in ns(DAT$Age, knots = myknot) : object "myknot" not found > > # The bad way to do it: revise myfn to make myknot a global variable: > > myfn > function(myknot=74) { > library(lme4) > library(splines) > cat("myknot=", myknot, "\n") > myknot<<-myknot > myMER<-lmer(formula=Finished ~ Sex01 + ns(DAT$Age, knots= myknot > ) + MinCC + PzC + (1 | NAME ) + (1 | Year), data=myDATonly, > family="binomial") > } > > > z<-myfn(74) > myknot= 74 > > # Runs fine but puts a global variable into .GlobalEnv. > > > sessionInfo() > R version 2.6.2 (2008-02-08) > i386-pc-mingw32 > > locale: > LC_COLLATE=English_United States.1252;LC_CTYPE=English_United > States.1252;LC_MONETARY=English_United > States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252 > > attached base packages: > [1] splines stats graphics grDevices datasets utils methods > base > > other attached packages: > [1] lme4_0.999375-13 Matrix_0.999375-9 lattice_0.17-6 > > loaded via a namespace (and not attached): > [1] boot_1.2-32 grid_2.6.2 > > > > Thanks for any insight. > > Jacob A. Wegelin > [EMAIL PROTECTED] > Assistant Professor > Department of Biostatistics > Virginia Commonwealth University > 730 East Broad Street Room 3006 > P. O. Box 980032 > Richmond VA 23298-0032 > U.S.A. > http://www.people.vcu.edu/~jwegelin > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. > > [[alternative HTML version deleted]] ______________________________________________ R-help@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.