0

My problem is as follows:

I have a dataset of 6000 observation containing information from costumers (each observation is one client's information).

I'm optimizing a given function (in my case is a profit function) in order to find an optimal for my variable of interest. Particularly I'm looking for the optimal interest rate I should offer in order to maximize my expected profits.

I don't have any doubt about my function. The problem is that I don't know how should I proceed in order to apply this function to EACH OBSERVATION in order to obtain an OPTIMAL INTEREST RATE for EACH OF MY 6000 CLIENTS (or observations, as you prefer).

Until now, it has been easy to find the UNIQUE optimal (same for all clients) for this variable that would maximize my profits (This is, the global maximum I guess). But what I need to know is how I should proceed in order to apply my optimization problem to EACH of my 6000 observations, INDIVIDUALLY, in order to have the optimal interest rate to offer to each costumer (this is, 6000 optimal interest rates, one for each of them).

I guess I should do something similar to a for loop, but my experience in this area is limited, and I'm quite frustrated already. What's more, I've tried to use mapply(myfunction, mydata) as usual, but I only get error messages.

This is how my (really) simple code now looks like:

profits<- function(Rate)
  sum((Amount*(Rate-1.2)/100)*
        (1/(1+exp(0.600002438-0.140799335888812*
                    ((Previous.Rate - Rate)+(Competition.Rate - Rate))))))

And results for ONE optimal for the entire sample:

> optimise(profits, lower = 0, upper = 100, maximum = TRUE)
$maximum
[1] 6.644821

$objective
[1] 1347291

So the thing is, how do I rewrite my code in order to maximize this and obtain the optimal of my variable of interest for EACH of my rows?

Hope I've been clear! Thank you all in advance!

DRZP
  • 29
  • 1
  • 2
    It is always good to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This makes it easier for others to help you. – Jaap May 13 '16 at 20:42
  • You can use `apply(dataset, 1, FUN=function(obs) ...)` If dataset is a dataframe, then the argument `obs` in the function is a named vector. Check if you alternativly can use `with()` or `transform()`. – jogo May 14 '16 at 10:56

1 Answers1

0

It appears each of your customers are independent. So you just put lapply() around the optimize() call:

lapply(customer_list, function(one_customer){
 optimise(profits, lower = 0, upper = 100, maximum = TRUE)
 })

This will return a very big list, where each list element has a $maximum and a $objective. You can then run lapply to total the $maximums, to find just how rich you have become!

Darren Cook
  • 27,837
  • 13
  • 117
  • 217
  • Or you could use a `for` loop (which might be slower, but you could use `<-` assignment to store the maximum in the data frame on the observation's row and have all of the data tightly associated. With 6000 variables, there is likely little loss of time, but some added value in keeping the results tied to the data. – sconfluentus May 13 '16 at 21:13