[R] make an model object (e.g. nlme) available in a user defined function (xyplot related)

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Jul 16 22:02:19 CEST 2010


On Mon, Jul 12, 2010 at 8:16 AM, Jun Shen <jun.shen.ut at gmail.com> wrote:
> Dear Deepayan,
>
> Thank you for taking the time to look into this issue.
>
> I have a data object called "Data", please find it at the end of the
> message. Then I can run the code below separately in the console.

[...]

> ####################################################################
>
> Then I have my real function as follows: If I run the code as,
>
>>compare.curves(Data=Data)
>
> The analytical part is working but not the plotting part (Error using
> packet 1, object 'model' not found)

I don't have much to help you, but the problem is not in lattice, but
in formula.nlme. The following modification of your function also
fails:

compare.curves <- function(curve='ascending',
                           Data=stop('A data object must be specified'),
                           parameter='EC50',random.pdDiag=FALSE,
                           start.values=c(Emax=100,E0=1,EC50=50,gamma=2),...)
{
    mymodel=as.formula('RESP ~ E0+(Emax-E0)*CP**gamma/(EC50**gamma+CP**gamma)')
    mod.nlme<-nlme(model=mymodel,data=Data,method='REML',
                   fixed=Emax+E0+EC50+gamma~1,
                   random= if (length(parameter)==1)

eval(substitute(variable~1,list(variable=as.name(parameter))))
                   else {
                       variable<-as.name(parameter[1])
                       for (i in 2:length(parameter)) variable<-
                           paste(variable,'+',as.name(parameter[i]))
                       formula<-as.formula(paste(variable,'~1'))
                       if (random.pdDiag) list(pdDiag(formula))
                       else formula
                   },
                   groups=~ID,
                   start=list(fixed=start.values)
                   )
    formula(mod.nlme)
}

-Deepayan



More information about the R-help mailing list