修改
好的,对不起,我会更加清楚,
我有50个场景(这里我是随机创建的),我将所有这些场景都放在一个矩阵中。我可以应用ecdf函数后,给我一个50 ecdf的列表。我想从我的50个场景的所有这些ecdf中计算出分位数90和10以及中位数。
这是一个基本代码:
ma <- matrix(ncol = 50, nrow = 200)
for (i in 1:50) {
x <- runif(1:200, min = 0, max = 100)
ma[,i] <- x
}
ma_ecdf <- apply(ma, 2, ecdf)
plot(ma_ecdf[[1]])
for (i in 1:50) {
lines(ma_ecdf[[i]])
}
所以我可以轻松地绘制所有这些,但我只想在图表上表示三个参数(Q10,Q50,Q90)。
修改
我确切地知道它是怎么做的,所以我分享它,如果有时候有人需要它。
你可以尝试代码,图形非常明确,并且很好地解释了我想做的事情。感谢那些试图帮助我的人!
ma_data <- matrix(ncol = 50, nrow = 200)
for (i in 1:50) {
a <- runif(1:200, min = 0, max = 100)
ma_data[,i] <- a
}
ma_ecdf <- apply(ma_data, 2, ecdf)
x <- seq(from = 0, to = 1, by =0.1)
ma <- matrix(ncol = 50, nrow = length(x))
for (i in 1:length(x)) {
prob <- x[i]
for (j in 1:length(ma_ecdf)){
ma[i,j] <- quantile(ma_ecdf[[j]], probs = prob)
}
}
q10 <- apply(ma, 1, quantile, probs = c(0.10))
q90 <- apply(ma, 1, quantile, probs = c(0.90))
med <- apply(ma, 1, median)
plot(ma_ecdf[[1]])
for (i in 2:50) {
lines(ma_ecdf[[i]])
}
lines(med, x, type = 'o', col = 'red', lwd = 2)
lines(q90, x, type = 'o', col = 'green', lwd = 2)
lines(q10, x, type = 'o', col = 'green', lwd = 2)
您可以选择使用分位数和中位数绘制所有ecdf,或者仅选择分位数和中位数以使其更清晰。
答案 0 :(得分:1)
efun
- 函数提取ecdf的环境,然后计算所需的分位数。 (建议您不能将函数列表传递给* apply系列的成员是完全错误的,尽管我认为应用可能会对某些函数执行暴力。但是为什么要使用apply不明确。 )
绘图请求不清楚,所以我只提供matplot
。 (我认为有趣的部分已经完成。)
efun <- function(fn) { e <- environment(fn); quantile( e$x, prob=c(0.1, 0.5, 0.9) ) }
sapply(ma_ecdf, efun)
#------------
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
10% 11.20981 9.694139 11.07211 8.129253 10.18672 10.20660 13.42461 10.09662 9.155876
50% 50.86365 45.399646 50.09159 52.472317 45.83210 47.82140 52.37679 45.30424 51.370869
90% 90.73561 90.308808 87.72453 90.409360 89.66196 88.30103 92.96570 87.55434 91.887313
[,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18]
10% 11.52653 10.13625 10.04209 10.2433 10.14813 8.114616 13.86385 11.24537 8.682568
50% 49.29966 50.59389 52.60945 45.3897 51.34410 46.912610 54.52146 50.86271 49.204883
90% 87.62950 92.15806 91.54697 89.8588 92.53752 91.157058 86.73797 93.53906 90.686209
[,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27]
10% 13.42698 9.83722 12.50920 9.042764 12.68967 10.81326 7.331495 10.97554 12.82455
50% 53.85215 53.03308 53.53052 46.258026 53.21290 47.76353 40.680560 47.83468 48.76479
90% 88.38150 87.50191 89.57422 93.140304 91.31335 92.64003 87.679489 86.44366 87.89013
[,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35]
10% 9.478504 11.97249 9.288765 8.023545 9.167379 11.97052 10.81782 9.129501
50% 51.256558 50.08606 42.848092 49.300343 51.131813 51.21670 43.35010 47.818362
90% 91.601705 86.56648 84.462400 91.899195 86.919949 90.47939 90.89439 89.810636
[,36] [,37] [,38] [,39] [,40] [,41] [,42] [,43] [,44]
10% 16.61909 9.579045 10.96399 14.04819 8.941116 11.42047 11.16979 10.74832 8.836482
50% 57.78916 49.060583 52.84561 54.79853 48.950509 56.18923 46.80874 46.82841 50.649137
90% 92.04979 91.504328 90.61166 91.75224 89.978594 91.10922 88.41800 86.04107 92.654152
[,45] [,46] [,47] [,48] [,49] [,50]
10% 12.34652 10.10571 7.374205 11.54974 7.079834 11.04556
50% 48.43045 56.15222 47.624488 45.90533 52.843572 51.11976
90% 88.18035 91.85914 91.297291 89.62237 92.382659 91.92695
如果需要将所有的quantile_0.10值链接在一起(并且使用函数的序列作为x坐标来使用中位数和第90个百分位数,那么这个图就是结果:
png();matplot( 1:50, t(sapply(ma_ecdf, efun)) , type="b"); dev.off()