0

I created a model in a Dataset to extimate a price sensitivity, now for each individual I want to extimate an "optimal price" to obtain "maximum income"

The price is >0 and <14. The income is = data$money[i]*sow

I want to use the optimize function to find the maximum profit changing the price for each "i". I also would like to have a plot of my function.

My function:

MYF<-function(x,i) {
y<-exp(predict(ols, cbind(data[i,!names(data) %in% c("spread") ], spread=(x-data$price_of_system[i]))))
sow<-y/(1+y)
return(data$money[i]*sow)
}

So I want to look for the x(my_price) that maximize (data$money[i]*sow) for i=5 I use

max <- optimize(MYF, tol = 0.0001,lower=0,upper=14, maximum = TRUE, i=5)

But R returns: invalid function value in 'optimize'. I'd like to find also the way how to plot that. Thank you

Jaap
  • 81,064
  • 34
  • 182
  • 193
dax90
  • 1,088
  • 14
  • 29
  • I think the problem is the return value of `MYF`. `optimize` expects a sinlge value as return. Also see [here](http://stackoverflow.com/questions/6658941/r-question-about-optimizing-invalid-function-value-in-optimize).Of course without data is difficult to say what is happening. Consider making your problem clear with a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – alko989 May 26 '14 at 14:14

1 Answers1

1

Instead of specifying the lower and upper parameters, specify the interval instead. Your function should be

max <- optimize(MYF, interval = c(0,14), tol = 0.0001, maximum = TRUE, i = 5)

For your second question, here is an example. I think it might help you :

test.function <- function(x,i) {
  2 * x * i
}

curve(test.function(x, i = 5), xlim=c(0,14))
Julien D.
  • 58
  • 1
  • 7
  • I wanna plot how changing my x in the interval (0,14) the returned y evolves – dax90 May 26 '14 at 09:19
  • See my updated answer. Let me know if that's what you were looking for. – Julien D. May 26 '14 at 13:49
  • `lower` and `upper` are serving the same purpose as `interval`, so I don't think this is the problem here. I think the problem is the return value of the function `MYF`. – alko989 May 26 '14 at 14:12