Let's think about an extremely useful and realistic case, a simple "threshold" function: if x < thresh, it returns a, otherwise it returns b. A schematic of a possible set of data, the function, and its parameters might look like this:

thrfun <- function(x,a,b,thresh) {
ifelse(x<thresh,a,b)
}
Technical note: the reason we use ifelse is that if in R only operates on one item at a time-if x is a vector and you use
if (x<10) it is just looks at x[1]. ifelse
looks at every item in the vector, which is what we want in this case.
Now let's generate some data with a step function in them.
data.x <- seq(0,5,length=20) det.y <- thrfun(data.x,2,5,3) data.y <- det.y+rnorm(length(data.x),0,0.5) plot(data.x,data.y)This produces (for example) the following data:

likfun <- function(pvec,x=data.x,y=data.y) {
exp.y <- thrfun(x,pvec[1],pvec[2],pvec[3])
sum((exp.y-data.y)^2)
}
Now let's assume, for simplicity, that we know a and b and we're just trying to fit thresh to the data. We'll define thrvec as a set of values that we want to calculate the sum-of-squares for, and then use sapply to calculate likfun for each value.
thrvec <- seq(0,5,length=100) tmpfun <- function(z) likfun(c(2,5,z)) ssvec <- sapply(thrvec,tmpfun) plot(thrvec,ssvec,type="l",xlab="threshold parameter",ylab="S.S.")This gives us the following S.S. plot:
