Re: [R] nls and factor

2006-04-26 Thread Christian Ritz

Hi Manuel,

an alternative to the approach pointed out by Prof. Ripley is to use
the package 'drc' which allows one or more parameters in a non-linear
regression model to depend on a factor.  

You will need the latest version available at www.bioassay.dk (an older
version is available on CRAN).


Bconc - runif(30,0.1,10)
Aconc - runif(30,0.1,10)
At - runif(30,1,30)
Bt - runif(30,1,30)

BKm - 1
AKm - 0
EBoth - -0.41

yB - 100*exp(EBoth*Bt)*Bconc/(BKm+Bconc)+rnorm(30,0,1)
yA - 75*exp(EBoth*At)*Aconc/(AKm+Aconc)+rnorm(30,0,1)


## Constructing the dataset
tvalues - c(At,  Bt)
conc - c(Aconc, Bconc)
yfactor - factor(rep(c(A, B), c(30, 30)))
y - c(yA, yB)


## Defining the non-linear function
twodimMM - function()
{
fct - function(x, parm)
{
parm[, 1]*exp(parm[, 2]*x[, 1])*x[, 2]/(parm[, 3] + x[, 2])
}

ssfct - function(dataset)  # a very simple self starter function
{
return(c(90, -0.5, 0.8))
}

names - c(lev, Ev, km)  # parameter names

return(list(fct=fct, ssfct=ssfct, names=names))
}


library(drc)

model1 - multdrc(y~cbind(tvalues,conc), yfactor, fct=twodimMM())
summary(model1)


## Collapsing the Ev parameters into a single parameter
model2 - multdrc(y~cbind(tvalues,conc), yfactor,
collapse=data.frame(yfactor,1,yfactor), fct=twodimMM())

anova(model2, model1)  # model reduction is insignificant
summary(model2)



Christian

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] nls and factor

2006-04-25 Thread Manuel Gutierrez
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 [EMAIL PROTECTED]
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@stat.math.ethz.ch mailing list
  https://stat.ethz.ch/mailman/listinfo/r-help
  PLEASE do read the posting guide!
 http://www.R-project.org/posting-guide.html
 
 
 -- 
 Brian D. Ripley, 
 [EMAIL PROTECTED]
 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, UKFax:  +44 1865
 272595


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] nls and factor

2006-04-20 Thread Manuel Gutierrez
Is it possible to include a factor in an nls formula?
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@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] nls and factor

2006-04-20 Thread Andrew Robinson
Manuel,

I don't think that it works very easily.  Instead, try gnls() in the
nlme package.

Cheers

Andrew

On Thu, Apr 20, 2006 at 11:18:02AM +0200, Manuel Gutierrez wrote:
 Is it possible to include a factor in an nls formula?
 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@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

-- 
Andrew Robinson  
Department of Mathematics and StatisticsTel: +61-3-8344-9763
University of Melbourne, VIC 3010 Australia Fax: +61-3-8344-4599
Email: [EMAIL PROTECTED] http://www.ms.unimelb.edu.au

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] nls and factor

2006-04-20 Thread Manuel Gutierrez
Thanks Andrew. I am now trying but without much
success. I don't now how to give start values for the
factor?.
Could you give me an example solution with my toy
example?

a-as.factor(c(rep(1,50),rep(0,50)))
independ-1:100
respo-rep(NA,100)
respo[a==1]-(independ[a==1]^2.3)+2
respo[a==0]-(independ[a==0]^2.1)+3
library(nlme)
gnls(respo~independ^b+a,start=list(b=1.8))

Many thanks.
Manu

 --- Andrew Robinson [EMAIL PROTECTED]
escribió:

 Manuel,
 
 I don't think that it works very easily.  Instead,
 try gnls() in the
 nlme package.
 
 Cheers
 
 Andrew
 
 On Thu, Apr 20, 2006 at 11:18:02AM +0200, Manuel
 Gutierrez wrote:
  Is it possible to include a factor in an nls
 formula?
  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@stat.math.ethz.ch mailing list
  https://stat.ethz.ch/mailman/listinfo/r-help
  PLEASE do read the posting guide!
 http://www.R-project.org/posting-guide.html
 
 -- 
 Andrew Robinson  
 Department of Mathematics and Statistics   
 Tel: +61-3-8344-9763
 University of Melbourne, VIC 3010 Australia
 Fax: +61-3-8344-4599
 Email: [EMAIL PROTECTED]
 http://www.ms.unimelb.edu.au


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] nls and factor

2006-04-20 Thread Prof Brian Ripley
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@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


-- 
Brian D. Ripley,  [EMAIL PROTECTED]
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, UKFax:  +44 1865 272595

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html