2017-03-16 98 views
1

我試圖繪製對同一地塊不同的方程一些累積分佈函數,這是我寫的代碼:情節沒有顯示CDF

library (triangle) 
library(lattice) 
library(latticeExtra) 
n = 1000 

W1 = rtriangle(n,3128,3250) 
W2 = rtriangle(n,3065,3149) 
SO = rtriangle(n,0.2,0.3) 

MCtab <- data.frame(W1,W2,SO) 

set.seed(1) 
for (n in 1:n) { 
NPV30 <- (1*W1 + 2*W2 + 3*SO)} 

set.seed(1) 
for (n in 1:n) { 
NPV50 <- ecdf((4*W1 + 5*W2 + 6*SO))} 
set.seed(1) 
for (n in 1:n) { 
NPV70 <- ecdf((7*W1 + 8*W2 + 9*SO))} 

MCtab2 <- data.frame(NPV30,NPV50,NPV70) 



plot(NPV30, verticals=TRUE, main= 'Polymer NPV CDF', do.points=FALSE, col='red') 
plot(NPV50, verticals=TRUE, do.points=FALSE, add=TRUE, col='brown') 
plot(NPV70, verticals=TRUE, do.points=FALSE, add=TRUE, col='orange') 

只有一個曲線如圖不能似乎找出原因,或者如果有人有更好的方法會很好。謝謝

+1

一種可能性是,後來的值太大初始待觀察情節維度。您可以添加包含所有數據點的最小值和最大值的ylim參數。 – lmo

+0

@lmo謝謝,這似乎是最有可能的原因 – John

+0

對我的答案的任何反饋? –

回答

0

我假設在示例代碼中NPV30賦值之前缺少ecdf。我認爲它沒有顯示值的實際原因是plot.ecdf方法根本不支持add參數。但你可以很容易地推出你自己的。

您爲這些函數選擇的值給出了不同的分佈,累積分佈圖很無聊 - 只是矩形。所以我調整了NPV50NPV70的定義值,使三個分佈更加相似。

因此,這裏是我使用的代碼:

library (triangle) 
library(lattice) 
library(latticeExtra) 
n = 50 

W1 = rtriangle(n,3128,3250) 
W2 = rtriangle(n,3065,3149) 
SO = rtriangle(n,0.2,0.3) 

MCtab <- data.frame(W1,W2,SO) 

set.seed(1) 
for (n in 1:n) { 
    NPV30 <- ecdf((1*W1 + 2*W2 + 3*SO))} 

set.seed(1) 
for (n in 1:n) { 
    NPV50 <- ecdf((1*W1 + 2.2*W2 + 3.1*SO))} 
set.seed(1) 
for (n in 1:n) { 
    NPV70 <- ecdf((1*W1 + 2.4*W2 + 3.2*SO))} 

MCtab2 <- data.frame(NPV30,NPV50,NPV70) 

qNPV30 <- quantile(NPV30) 
qNPV50 <- quantile(NPV50) 
qNPV70 <- quantile(NPV70) 
xmin <- min(c(qNPV30[1],qNPV50[1],qNPV70[1])) 
xmax <- max(c(qNPV30[5],qNPV50[5],qNPV70[5])) 
xdlt <- xmax-xmin 

npts <- 200 
x <- (1:npts)*xdlt/npts + xmin 
y30 <- NPV30(x) 
y50 <- NPV50(x) 
y70 <- NPV70(x) 

plot(x,y30,main= 'Polymer NPV CDF',xlim=c(xmin,xmax),type='l',col='red') 
lines(x,y50,xlim=c(xmin,xmax),type='l',col='blue',add=T) 
lines(x,y70,xlim=c(xmin,xmax),type='l',col='green',add=T) 

#plot(NPV30, verticals=TRUE, main= 'Polymer NPV CDF', do.points=FALSE, col='red') 
#plot(NPV50, verticals=TRUE, do.points=F, add=TRUE, col='blue') 
#plot(NPV70, verticals=TRUE, do.points=F, add=TRUE, col='green') 

,這裏是輸出結果:

enter image description here

+0

感謝您的澄清(道歉爲非常遲到的答覆) – John