0

I am trying to do a maximization in R that I have done previously in Excel with the solver. The problem is that I don't know how to deal with it (i don't have a good level in R).

let's talk a bit about my data. I have 26 Swiss cantons and the Swiss government (which is the sum of the value of the 26 cantons) with their population and their "wealth". So I have 27 observatios by variable. I'm not sure that the following descriptions are useful but I put them anyway. From this, I calculate some variables with while loops. For each canton [i]:

  • resource potential = mean(wealth2011 [i],wealth2012 [i],wealth2013 [i])
  • population mean = mean(population2011 [i],population2012 [i],population2013 [i])
  • resource potential per capita = 1000*resource potential [i]/population [i]
  • resource index = 100*resource potential capita [i]/resource potential capita [swiss government]

Here a little example of the kind of loops I used:

    RI=0
    i = 1
    while(i<28){
      RI[i]=resource potential capita [i]/resource potential capita [27]*100
      i = i+1
    }

The resource index (RI) for the Swiss government (i = 27) is 100 because we divide the resource potential capita of the swiss government (when i = 27) by itself and multiply by 100. Hence, all cantons that have a RI>100 are rich cantons and other (IR<100) are poor cantons. Until here, there was no problem. I just explained how I built my dataset.

Now the problem that I face: I have to create the variable weighted difference (wd). It takes the value of:

  • 0 if RI>100 (rich canton)
  • (100-RI[i])^(1+P)*Pop[i] if RI<100 (poor canton)

I create this variable like this: (sorry for the weakness of the code, I did my best).

wd=-1
i = 1
a = 0
c = 0
tot = 0
while(i<28){
  if(i == 27) {
    wd[i] = a
  } else if (RI[i] < 100) {
    wd[i] = (100-RI[i])^(1+P)*Pop[i]
    c = wd[i]
    a = a+c
  } else {
    wd[i]= 0
  }
  i = i+1
}

However, I don't now the value of "p". It is a value between 0 and 1. To find the value of p, I have to do a maximization using the following features:

  • RI_26 = 65.9, it is the minimum of RI in my data
  • RI_min = 100-((x*wd [27])/((1+p)*z*100))^(1/p), where x and z are fixed values (x = 8'677, z = 4'075'977'077) and wd [27] the sum of wd for each canton.

We have p in two equation: RI_min and wd. To solve it in Excel, I used the Excel solver with the following features:

  • p_dot = RI_26/RI_min* p ==> p_dot =[65.9/100-((x* wd [27])/((1+p)*z*100))^(1/p)]*p
  • RI_26 = RI_min ==>65.9 =100-((x*wd [27])/((1+p)*z*100))^(1/p)

In Excel, p is my variable cell (the only value allowed to change), p_dot is my objective to define and RI_26 = RI_min is my constraint.

So I would like to maximize p and I don't know how to do this in R. My main problem is the presence of p in RI_min and wd. We need to do an iteration to solve it but this is too far from my skills.

Is anyone able to help me with the information I provided?

S.Tripod
  • 1
  • 2

2 Answers2

0

you should look into the optim function. Here I will try to give you a really simple explanation since you said you don't have a really good level in R.

Assuming I have a function f(x) that I want to maximize and therefore I want to find the parameter x that gives me the max value of f(x).

First thing to do will be to define the function, in R you can do this with:

myfunction<- function(x) {...}

Having defined the function I can optimize it with the command:

optim(par,myfunction)

where par is the vector of initial parameters of the function, and myfunction is the function that needs to be optimized. Bear in mind that optim performs minimization, however it will maximize if control$fnscale is negative. Another strategy will be to change the function (i.e. changing the sign) to suit the problem.

Hope that this helps, Marco

Marco De Virgilis
  • 982
  • 1
  • 9
  • 29
  • Thank's for the answer Marco. I will try to do this but I am not sure of what and how i have to put in my function. If anyone want to helps me more with example ou more applied answer, I accept with pleasure. But thank's for the piece of advice, I'll work on it – S.Tripod Feb 21 '18 at 12:51
0

From the description you provided, if I'm not mistaken, it looks like that everything you need to do it's just an equation. In particular you have the following two expressions:

RI_min = 100-((x*y)/((1+p)*z*100))^(1/p)

and, since x,y,z are fixed, the only variable is p. Moreover, having RI_26 = RI_min this yields to:

65.9 =100-((x*y)/((1+p)*z*100))^(1/p)

Plugging in the values of x,y and z you have provided, this yields to

p=0.526639915936052

I don't understand what exactly you are trying to maximize.

Marco De Virgilis
  • 982
  • 1
  • 9
  • 29