# [R] nls and factor

Manuel Gutierrez manuel_gutierrez_lopez at yahoo.es
Tue Apr 25 16:40:16 CEST 2006

```Thanks, it was actually p.249, at least in my MASS3.
but that solved my doubt.

I've have another doubt, can this factor interact with
one of the parameters in the model?

My problem is basically a Michaelis Menten term, where
this factor determines a different Km. The rest of the
parameters in the model are the same. But I don't know
how to write the nls formula, or if it is possible.

This is a toy example, B and A are the two "factors",
conc is the concentration and t is the temperature:

## Generate independent variables
Bconc<-runif(30,0.1,10)
Aconc<-runif(30,0.1,10)
At<-runif(30,1,30)
Bt<-runif(30,1,30)

## These are the parameters I want to calculate from
## my real data
BKm<-1
AKm<-0
EBoth<--0.41

# These are my simulated dependent variables
yB<-100*exp(EBoth*Bt)*Bconc/(BKm+Bconc)+rnorm(30,0,1)
yA<-75*exp(EBoth*At)*Aconc/(AKm+Aconc)+rnorm(30,0,1)

#The separate models
BModel<-nls(Response~lev*exp(Ev*t)*conc/(Km+conc),data=list(Response=yB,t=Bt,conc=Bconc),start=list(lev=90,Ev=-0.5,Km=0.8),trace=TRUE)

AModel<-nls(Response~lev*exp(Ev*t)*conc/(Km+conc),data=list(Response=yA,t=At,conc=Aconc),start=list(lev=90,Ev=-0.5,Km=0.8),trace=TRUE)

## I want to obtain a combined model of the form:
## Y=Intercept[1:2]*exp(Eboth*t)*conc/(Km[1:2]+conc)
## where I have a common E but two intercepts and two
## Kms (one of them should in fact be zero)

yBoth<-c(yB,yA)
concBoth<-c(Bconc,Aconc)
tBoth<-c(At,Bt)
AorB<-as.factor(c(rep(0,length(yA)),rep(1,length(yB))))

## Amongst other things I've tried
FullModel<-nls(Response~lev[AorB]*exp(Ev*t)*conc/(Km[AorB]+conc),data=list(Response=yBoth,t=tBoth,conc=concBoth),start=list(lev=c(90,70),Ev=-0.5,Km=c(0.8,0)),trace=TRUE)

## but i get to a singular gradient

Any other pointers,
thanks
Manuel

--- Prof Brian Ripley <ripley at stats.ox.ac.uk>
escribió:

> On Thu, 20 Apr 2006, Manuel Gutierrez wrote:
>
> > Is it possible to include a factor in an nls
> formula?
>
> Yes.  What do you intend by it?  If you mean what it
> would mean for a lm
> formula, you need A[a] and starting values for A.
>
> There's an example on p.219 of MASS4.
>
> > I've searched the help pages without any luck so I
> > guess it is not feasible.
> > I've given it a few attempts without luck getting
> the
> > message:
> > + not meaningful for factors in:
> > Ops.factor(independ^EE, a)
> >
> > This is a toy example, my realworld case is much
> more
> > complicated (and can not be solved linearizing an
> > using lm)
> > a<-as.factor(c(rep(1,50),rep(0,50)))
> > independ<-rnorm(100)
> > respo<-rep(NA,100)
> > respo[a==1]<-(independ[a==1]^2.3)+2
> > respo[a==0]<-(independ[a==0]^2.1)+3
> >
>
nls(respo~independ^EE+a,start=list(EE=1.8),trace=TRUE)
> >
> > Any pointers welcomed
> > Many Thanks,
> > Manu
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> >
>
> --
> Brian D. Ripley,
> ripley at stats.ox.ac.uk
> Professor of Applied Statistics,
> http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865
> 272861 (self)
> 1 South Parks Road,                     +44 1865
> 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865
> 272595
>

```