给出以下样本数据:
library(Metrics)
obs=data.frame(replicate(10,runif(100)))
pred=data.frame(replicate(10,runif(100)))
obs1=as.data.frame(lapply(obs, function(cc) cc[ sample(c(TRUE, NA), prob = c(0.85, 0.15), size = length(cc), replace = TRUE) ]))
pred1=as.data.frame(lapply(pred, function(cc) cc[ sample(c(TRUE, NA), prob = c(0.85, 0.15), size = length(cc), replace = TRUE) ]))
pred1[,1]=NA
result=mapply(function(x, y) {if(all(is.na(y))) NA else mae(x, y, ), mse(x,y),rmse(x,y),se(x,y)
}, obs1,pred1,SIMPLIFY = F,USE.NAMES = TRUE)
我想通过mae(obs1[,1],pred1[,1])
计算说mapply
等。如何使用base R functions
或plyr
通过一次通话为所有其他功能执行相同操作?
在输出中,result
的rownames是column names
或obs1
的{{1}},而同名是[{1}}等。
答案 0 :(得分:1)
您必须编写自己的函数来指定要应用的各种函数:
multi.fun <- function(x,y) {
c(mae = mae(x,y), mse = mse(x,y))
}
然后你可以这样做:
obs=data.frame(replicate(10,runif(100)))
pred=data.frame(replicate(10,runif(100)))
obs1=as.data.frame(lapply(obs, function(cc) cc[ sample(c(TRUE, NA), prob = c(0.85, 0.15), size = length(cc), replace = TRUE) ]))
pred1=as.data.frame(lapply(pred, function(cc) cc[ sample(c(TRUE, NA), prob = c(0.85, 0.15), size = length(cc), replace = TRUE) ]))
mapply(multi.fun, obs1, pred1)