I am trying to model a dependent variable as a threshold function of
my independent variable.
What I mean is that I want to fit different intercepts to y following 2
breakpoints, but with fixed slopes.
I am trying to do this with using ifelse statements in the nls function.
Perhaps, this is not an appropriate approach.

I have created a very simple example to illustrate what I am trying to do.

#Creating my independent variable
x <- seq(0, 1000)
#Creating my dependent variable, where all values below threshold #1 are 0,
all values above threshold #2 are 0 and all values within the two
thresholds are 1
y <- ifelse(x < 300, 0, ifelse(x>700, 0, 1))
#Adding noise to my dependent variable
y <- y + rnorm(length(y), 0, 0.01)
#I am having trouble clearly explaining the model I want to fit but perhaps
the plot is self explanatory
plot(x, y)
#Now I am trying to adjust a nonlinear model to fit the two breakpoints,
with known slopes between the breakpoints (here, respectively 0, 1 and 0)
threshold.model <- nls(y~ifelse(x<b1, 0, ifelse(x>b2, 0, 1)),
start=list(b1=300, b2=700), trace=T)

I have played around with this function quite a bit and systematically get
an error saying: singular gradient matrix at initial parameter estimates
I admit that I don't fully understand what a singular gradient matrix
implies.
But it seems to me that both my model and starting values are sensible
given the data, and that the break points are in fact estimable.
Could someone point to what I am doing wrong?

More generally, I am interested in knowing
(1) whether I can use such ifelse statements in the function nls
(1) whether I can even use nls for this model
(3) whether I can model this with a function that would allow me to assume
that the errors are binomial, rather than normally distributed
(ultimately I want to use such a model on binomial data)

I am using R version 2.15.1 on 64-bit Windows 7

Any guidance would be greatly appreciated.
Veronique

	[[alternative HTML version deleted]]

