Home > Blockchain >  R Iteration using while loop and if statement
R Iteration using while loop and if statement

Time:02-18

I am new to R and I have the following problem: I need to solve a numerical optimisation problem where I realocate one unit of labor input (V20) such that overall productivity difference increases. I have to iterate the process until it has converged. For this, the productivity difference (Diff20) in each row has to be equal after optimisation.

My approach is to calculate the maximum and the minimum productivity differences (Diff_max, Diff_min) and shift one unit of labor input (V20) from the row with the highest productivity difference Diff_max (which represents a decrease in productivity as more labor input was needed for a certain level of output) to the row with the lowest productivity difference Diff_min (which represents an increase in productivity as less labor input was needed for a certain level of output).

I tried to combine a while loop with the if condition for that row to be the Diff_max or Diff_min.

Unfortunately, this does not work at all, so I cannot even offer a propper error message.

Does anybody of you have an idea how to solve/ approach this problem? It would be really great if you could help me! Many thanks in advance!

df <- data.frame("PF"=c("PF1","PF2","PF3","PF4","PF5"),"V19"=c(2.6,5.9,0.5,0.1,3.0),"V20"=c(2.1,7.2,0.3,0.2,2.3), "F19"=c(26,80,10,7,24), "F20"=c(28,90,3,5,13), "A19"=c(4,320,5,0,80), "A20"=c(2.7,458,0,0,45), "B19"=c(1,6,0,0,5), "B20"=c(2,5,0,0,3))


df$num19 <- df$V19*220
df$denom19 <- (df$F19 df$A19 df$B19)
df$num20 <- df$V20*220
df$denom20 <- (df$F20 df$A20 df$B20)


df$rat19 <- df$num19/df$denom19
df$rat20 <- df$num20/df$denom20
df$Diff20 <- df$rat20 - df$rat19
df$Diff_mean <- mean(df$Diff20)
df$Diff_max <- max(df$Diff20)
df$Diff_min <- min(df$Diff20)

while (df$Diff_max> df$Diff_mean) {
  if (df$Diff20 == df$Diff_max) {
    df$V20 <- df$V20 0.1
  }
  if (df$Diff20 == df$Diff_min) {
    df$V20 <- df$V20-0.1
  }
}

CodePudding user response:

As JKupzig pointed out, there is no update of the condition in the loop.

Running the code I noted two other things:

  1. By increasing the nominator for max values and decreasing it for min values it does not converge.
  2. At 0.1 the grid-size seems too large to converge, I made it smaller, so the minimal working example converges.
    df <- data.frame("PF"=c("PF1","PF2","PF3","PF4","PF5"),
                 "V19"=c(2.6,5.9,0.5,0.1,3.0),
                 "V20"=c(2.1,7.2,0.3,0.2,2.3), 
                 "F19"=c(26,80,10,7,24), 
                 "F20"=c(28,90,3,5,13), 
                 "A19"=c(4,320,5,0,80), 
                 "A20"=c(2.7,458,0,0,45), 
                 "B19"=c(1,6,0,0,5), 
                 "B20"=c(2,5,0,0,3))

df$num19 <- df$V19*220
df$denom19 <- (df$F19 df$A19 df$B19)
df$num20 <- df$V20*220
df$denom20 <- (df$F20 df$A20 df$B20)

df$rat19 <- df$num19/df$denom19
df$rat20 <- df$num20/df$denom20
df$Diff20 <- df$rat20 - df$rat19
# df$Diff_mean <- mean(df$Diff20)
# df$Diff_max <- max(df$Diff20)
# df$Diff_min <- min(df$Diff20)

df

while(round(max(df$Diff20),3) > round(mean(df$Diff20),3)){
  df[df$Diff20 == max(df$Diff20), "V20"] <-  df[df$Diff20 == max(df$Diff20), "V20"] - 0.00001
  df[df$Diff20 == min(df$Diff20), "V20"] <-  df[df$Diff20 == min(df$Diff20), "V20"]   0.00001
  df$num20 <- df$V20*220
  df$rat20 <- df$num20/df$denom20
  df$Diff20 <- df$rat20 - df$rat19
}

df
  • Related