[R] Constraint in nls with factor in model?

Manuel Morales Manuel.A.Morales at williams.edu
Wed Feb 18 20:32:31 CET 2009


Hi all,

I'm trying to fit a model using the shorthand coeff[factor] instead of
coding dummy variables. Is there a way to keep this notation when
specifying constraints? See example below:

x = runif(200)
b0 = c(rep(0,100),runif(100))
b1 = 1
fac <- as.factor(rep(c(0,1), each=100))
y = b0+b1*x+rnorm(200,sd=0.05)

## No constraint - works
nls(y~b0[fac]+b1*x, start=list(b0=c(1,1),b1=1),algorithm="port")

## Constraint - doesn't work
nls(y~b0[fac]+b1*x, start=list(b0=c(1,1),b1=1), algorithm="port",
    upper=c(100,100,.5))

## Constraint w/ dummy variable - works
nls(y~b01+I(as.numeric(fac)-1)*b02+b1*x, start=list(b01=1,b02=1,b1=1),
    algorithm="port", upper=c(100,100,.5))

Thanks!

Manuel


-- 
http://mutualism.williams.edu
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090218/b8141dfa/attachment-0002.bin>


More information about the R-help mailing list