1

Consider the following data:

datamichael <- data.frame(x=c(100,200,300,500), y=c(75,100,300,400),  row.names = LETTERS[1:4])
dataewan    <- data.frame(x=c(200,50,200,600),  y=c(100,100,400,300), row.names = LETTERS[1:4])
datatom     <- data.frame(x=c(100,150,400,200), y=c(100,100,400,300), row.names = LETTERS[1:4])

datamichaeldataewan <- rbind(datamichael,dataewan)
datamichaeldatatom  <- rbind(datamichael,datatom)
dataewandatamichael <- rbind(dataewan, datamichael)
dataewandatatom     <- rbind(dataewan, datatom)
datatomdatamichael  <- rbind(datatom, datamichael)
datatomdataewan     <- rbind(datatom, dataewan)

library(Benchmarking)
effmichaelewan <- dea(datamichaeldataewan$x,datamichaeldataewan$y, XREF=datamichael$x, YREF=datamichael$y)
effmichaeltom  <- dea(datamichaeldatatom$x,datamichaeldatatom$y, XREF=datamichael$x, YREF=datamichael$y)
effewanmichael <- dea(dataewandatamichael$x,dataewandatamichael$y, XREF=dataewan$x, YREF=dataewan$y)
effewantom     <- dea(dataewandatatom$x,dataewandatatom$y, XREF=dataewan$x, YREF=dataewan$y)
efftommichael  <- dea(datatomdatamichael$x,datatomdatamichael$y, XREF=datatom$x, YREF=datatom$y)
efftomewan     <- dea(datatomdataewan$x,datatomdataewan$y, XREF=datatom$x, YREF=datatom$y)

I know my name convention is a bit confusing. Now I want to calculate a weighted average for Michael. That is:

result1 <- (weighted.mean(eff(effmichaelewan), datamichaeldataewan$y)/
            weighted.mean(eff(effewanmichael), dataewandatamichael$y))
result2 <- (weighted.mean(eff(effmichaeltom), datamichaeldatatom$y)/
            weighted.mean(eff(efftommichael), datatomdatamichael$y))

Is it possible to that calculation with a loop? Because I have more than 2 merged data, which the data for Tom should be divided with.

2 Answers2

0

You could put your output (dataXXXXXX and effXXXXXXX) into two lists and then loop through it:

# First order them in the order they appear in your weighted mean calculation
datamichaeldataewan <- rbind(datamichael,dataewan)
dataewandatamichael <- rbind(dataewan, datamichael)
datamichaeldatatom <- rbind(datamichael,datatom)
datatomdatamichael <- rbind(datatom, datamichael)

effmichaelewan <- dea(datamichaeldataewan$x,datamichaeldataewan$y)
effewanmichael <- dea(dataewandatatom$x,dataewandatatom$y)
effmichaeltom <- dea(datamichaeldatatom$x,datamichaeldatatom$y)
efftommichael <- dea(datatomdatamichael$x,datatomdatamichael$y)

# add them to lists
data.list <- list(datamichaeldataewan,dataewandatamichael,datamichaeldatatom,datatomdatamichael)
eff.list <- list(effmichaelewan,effewanmichael,effmichaeltom,efftommichael)

# your loop
for (i in seq(1,3,2)){ # use every other entry as you add plus one to include the following entry in your weighted mean calculation
  result1 <- (weighted.mean(eff(eff.list[[i]]), data.list[[i]]$y)/
                weighted.mean(eff(eff.list[[i+1]]), data.list[[i+1]]$y))
  print(result1)
}
Alex
  • 504
  • 3
  • 10
0

You can do:

L <- list(datamichael, dataewan, datatom)

library(Benchmarking)
result <- function(x) {
  d1 <- rbind(x[[1]], x[[2]])
  d2 <- rbind(x[[2]], x[[1]])
  eff.d1 <- dea(d1$x, d1$y)
  eff.d2 <- dea(d2$x, d2$y)
  weighted.mean(eff(eff.d1), d1$y) / weighted.mean(eff(eff.d2), d2$y)
}
combn(L, 2, FUN=result)

To know about the names you can do:

n <- c("michael", "ewan", "tom")
combn(n, 2)

data:

datamichael <- data.frame(x=c(100,200,300,500), y=c(75,100,300,400),  row.names = LETTERS[1:4])
dataewan    <- data.frame(x=c(200,50,200,600),  y=c(100,100,400,300), row.names = LETTERS[1:4])
datatom     <- data.frame(x=c(100,150,400,200), y=c(100,100,400,300), row.names = LETTERS[1:4])
jogo
  • 12,469
  • 11
  • 37
  • 42
  • Thanks for your answer. If I use this I just get the result: `[1] 1 1 1`. Do you get the same result or is there something I am missing? –  Apr 30 '18 at 09:40
  • I have also tried to wrote `result(L)` or `result(datamichael)`, but that does not help. –  Apr 30 '18 at 10:07
  • Okay. But then I don't understand the result. Because if I use my own code I get the result: `result1=0.867...` and `result2=1`. This is the result I am interested in. –  Apr 30 '18 at 10:12
  • One question: Why do I get 3 results (I am only interested in 2 scores - I think it is the first and last score)? What is the middle score representing? –  Apr 30 '18 at 11:27
  • Ah, I see. But I am only interested in MichaelEwan and MichaelTom - so is it possible to only get the two first results? And one more question: I want to edit `eff.d2 <- dea(d2$x, d2$y)` to `eff.d2 <- dea(d2$x, d2$y, XREF=xname, YREF=yname)`, where `xname=(x2, x3)` and `yname=(y2, y3)`. That is, when I want the result for MichaelEwan (the first result), then `XREF=x2` and `YREF=y2`, and when I want the result for MichaelTom, then `XREF=x3` and `YREF=y3`. Is it possible to include that in the function? Hope you understand what I mean. –  Apr 30 '18 at 11:53
  • `head(combn(L, 2, FUN=result), 2)` is doing the full calculation and then reduces the result **or** `sapply(head(combn(L, 2, simplify = FALSE), 2), result)` is reducing the list of the combinations (and then doing the calculation). – jogo Apr 30 '18 at 11:57
  • Thanks a lot. It is a great help. BTW, do you know the answer for the other question? –  Apr 30 '18 at 12:07
  • Sorry. I mean this: I want to edit `eff.d2 <- dea(d2$x, d2$y)` to `eff.d2 <- dea(d2$x, d2$y, XREF=xname, YREF=yname)`, where `xname=(x2, x3)` and `yname=(y2, y3)`. That is, when I want the result for MichaelEwan (the first result), then `XREF=x2` and `YREF=y2`, and when I want the result for MichaelTom, then `XREF=x3` and `YREF=y3`. Is it possible to include that in the function? Hope you understand what I mean. –  Apr 30 '18 at 12:14
  • I have now changed my the description for my problem. I think it makes it more clear to see it their. As you can see I have added different XREF and YREF to each eff- calculation. So for the eff- calculation where Michael is the first name, we use XREF and YREF for Michaels data. For the calculation where Ewan is the first name, we use XREF and YREF for Ewans data and the same for Tom. –  Apr 30 '18 at 12:45
  • I have not changed it and edited the XREF and YREF, so it now match. Do you know the answer for the question? So now I get `result1=2.356...` and `result2=1.316...`. –  Apr 30 '18 at 12:58
  • No, I have no idea for this. But eventually I did not understood the question. – jogo Apr 30 '18 at 13:02
  • I found the solution. I should just wrote `eff.d1 <- dea(d1$x, d1$y, XREF=[[1]]$x, YREF=[[1]]$y)` and `eff.d2 <- dea(d2$x, d2$y, XREF=[[2]]$x, YREF=[[2]]$y)`. But one question: What if I have multiple inputs (e.g. `x` and `z`)? Because `eff.d1 <- dea(d1$x, d1$y, XREF=c([[1]]$x, [[1]]$z), YREF=[[1]]$y)` does not work. –  Apr 30 '18 at 13:38
  • hm, really `XREF=[[1]]$x` - not `XREF=x[[1]]$x` ? – jogo Apr 30 '18 at 13:55