dax90
dax90

Reputation: 1118

Optimize and plot a function in R

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

Upvotes: 0

Views: 2760

Answers (1)

Julien D.
Julien D.

Reputation: 58

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))

Upvotes: 1

Related Questions