简体   繁体   English

如何在R中为我的观察结果的每个函数运行函数?

[英]How to run a function to EACH of my observations in R?

My problem is as follows: 我的问题如下:

I have a dataset of 6000 observation containing information from costumers (each observation is one client's information). 我有一个6000个观测值的数据集,其中包含来自客户的信息(每个观测值都是一个客户的信息)。

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). 问题是我不知道如何将该功能应用于每次观察,以获得每个6000个客户(或观察者,如您所愿)的最佳利率。

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). 到现在为止,一直很容易找到此变量的UNIQUE最优值(所有客户都相同),这将使我的利润最大化(这是我猜想的全局最大值)。 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). 但是我需要知道的是,应该如何将优化问题分别应用于我的6000个观测值中的每个观测值,以便为每个客户提供最优利率(即6000个最优利率,为每个人)。

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. 我想我应该做一些类似于for循环的事情,但是我在这方面的经验是有限的,我已经很沮丧了。 What's more, I've tried to use mapply(myfunction, mydata) as usual, but I only get error messages. 而且,我尝试照常使用mapply(myfunction,mydata),但只收到错误消息。

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? 所以问题是,我该如何重写我的代码以最大程度地提高代码并针对行中的每个EACH获得感兴趣的变量的最优值?

Hope I've been clear! 希望我已经清楚了! Thank you all in advance! 谢谢大家!

It appears each of your customers are independent. 看来您的每个客户都是独立的。 So you just put lapply() around the optimize() call: 因此,您只需将lapply()放在optimize()调用周围:

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 . 这将返回一个非常大的列表,其中每个列表元素都有一个$maximum和一个$objective You can then run lapply to total the $maximum s, to find just how rich you have become! 然后,您可以运行lapply来合计$maximum ,以查找您已变得有钱!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM