在循环中除以一个加权平均值

时间:2018-04-27 07:40:18

标签: r loops weighted-average

考虑以下数据:

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)

我知道我的名字惯例有点令人困惑。现在我想计算迈克尔的加权平均值。那就是:

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))

是否可以通过循环进行计算?因为我有两个以上的合并数据,汤姆的数据应该用它来划分。

2 个答案:

答案 0 :(得分:0)

您可以将输出(dataXXXXXX和effXXXXXXX)放入两个列表中,然后遍历它:

# 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)
}

答案 1 :(得分:0)

你可以这样做:

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)

要了解您可以做的名字:

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

数据:

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])