0

I have a data frame x and y, and I know the maximum of y. I want to fit this data to a quadratic model. How can I do it in R knowing the maximum? If I didn't know the maximum, I would fit it with lm(y~x + I(x^2)). Can anyone has an idea about this? Thanks in advance!

Tetsujin no Oni
  • 7,300
  • 2
  • 29
  • 46
Chris
  • 1,248
  • 4
  • 17
  • 25
  • 1
    Do you really mean you know the maximum of y or you know the x coordinate of the maximum of y? – Spacedman Oct 27 '11 at 15:33
  • 1
    Would like to know the answer to the previous comment. If the answer is "maximum of y" then @Aaron's elegant answer works. If the answer is "x coordinate" then `lm(y~I((x-xcoordmax)^2))` should work. – Ben Bolker Oct 27 '11 at 16:45
  • Kinda makes you think twice about answering questions from userXXXXXX people with near-zero rep. Aaron deserves mega-points for that answer. – Spacedman Oct 28 '11 at 10:53
  • Oh, I don't know. Other people seem to have found it useful, even if the OP's disappeared. Plus it's like a five-minute answer if you've done anything like it before. But thanks for the kudos, @Spacedman. – Aaron left Stack Overflow Oct 28 '11 at 14:48
  • Sorry guys,I was out of town that is why I was not following your answers and comments. Thank you very much for your cooperation. – Chris Oct 30 '11 at 11:35
  • To answer the question, I know the maximum of y. Thanks Aaron, I found your answer useful. I am going through it. If it is ok with you, I will ask if something is unclear. I haven't done anything like this before that is why I appeared here to ask. I hope you didn't mind answering. Spacedman, I have edited my profile now:) – Chris Oct 30 '11 at 11:39
  • @Chris, glad you found it useful. To ask a question about my answer, add a "comment" to the answer; I will answer if I'm able, but then so can others. Welcome to SO! – Aaron left Stack Overflow Oct 31 '11 at 13:57

2 Answers2

8

You have to minimize the sum of squares subject to the constraint; lm doesn't allow for constraints like this, so you have to use a generic optimization function, such as optim. Here's one way it could be done.

Make up some data. Here I'll say the known maximum is 50.

set.seed(5)
d <- data.frame(x=seq(-5, 5, len=51))
d$y <- 50 - 0.3*d$x^2 + rnorm(nrow(d))
M <- 50

Make a function to get the quadratic curve for points at x with given quadratic and linear coefficients and given maximum M. The calculus is straightforward; see duffymo's answer for details.

qM <- function(a, b, x, M) {
  c <- M - (3*b^2)/(4*a)
  a*x^2 + b*x + c
}

Make a function that get the sum of squares between a quadratic curve with given quadratic and linear coefficients and the data in d.

ff <- function(ab, d, M) {
  p <- qM(ab[1], ab[2], d$x, M)
  y <- d$y
  sum((p-y)^2)
}

Get the ordinary lm fit to use as starting values.

m0 <- lm(y ~ I(x^2) + x, data=d)
start <- coef(m0)[2:3]

Optimize the coefficients in the ff function.

o <- optim(start, ff, d=d, M=M)
o$par

Make a plot showing how the fit has a max at 50; the original lm fit doesn't.

plot(d)
xs <- seq(-5, 5, len=101)
lines(xs, predict(m0, newdata=data.frame(x=xs)), col="gray")
lines(xs, qM(o$par[1], o$par[2], xs, M))
abline(h=50, lty=3)

image comparing lm fit with my fit

Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
0

I'd use calculus to calculate the expression for the maximum point. Differentiation will eliminate some of the constants in the equation, so the calculation is easier if you know what that max value needs to be.

If I recall correctly, a simple functions of 1 variable have a maximum at f'(x) = 0 and f''(x) < 0. Check me on this.

So if your function is f(x):

f(x) = a0 + a1*x + a2*x*x
f'(x) = a1 + 2*a2*x
f''(x) = 2*a2

Set the second equation equal to zero to get the stationary point, then put that value of x into the third one to find out if it's a max or min.

duffymo
  • 305,152
  • 44
  • 369
  • 561