Skip to content
Prev 324716 / 398503 Next

how to compute maximum of fitted polynomial?

Hello,

As for the first question, you can use ?optim to compute the maximum of 
a function. Note that by default optim minimizes, to maximize you must 
set the parameter control$fnscale to a negative value.

fit <- lm(y ~ poly(x, 3))

fn <- function(x, coefs) as.numeric(c(1, x, x^2, x^3) %*% coefs)

sol <- optim(0, fn, gr = NULL, coef(fit), control = list(fnscale = -1),
	method = "L-BFGS-B", lower = 0, upper = 1)


As for the second question, I believe you can do something like

dfdx <- D( expression(a + b*x + c*x^2 + d*x^3), "x")

a <- coef(fit)[1]
b <- coef(fit)[2]
c <- coef(fit)[3]
d <- coef(fit)[4]
x <- sol$par
eval(dfdx)


See the help page for ?D


Hope this helps,

Rui Barradas

Em 04-06-2013 21:32, Joseph Clark escreveu: