简体   繁体   English

情节没有显示CDF

[英]Plot not showing CDF

I am trying to plot some cumulative distribution functions for different equations on the same plot, this is the code I have written: 我试图在同一个图上为不同的方程绘制一些累积分布函数,这是我写的代码:

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

Only one plot is shown cant seem to figure out why, or if someone has a better approach would be great. 只有一个情节似乎无法弄清楚为什么,或者如果有人有更好的方法会很棒。 Thanks 谢谢

I assuming there is an ecdf missing in front of your NPV30 assignment in the sample code. 我假设有一个ecdf在你的面前丢失NPV30示例代码分配。 I think the actual reason it is not displaying the values is that the plot.ecdf method simply does not support the add parameter. 我认为它没有显示值的实际原因是plot.ecdf方法根本不支持add参数。 But you can roll your own pretty easily. 但你可以轻松地自己动手。

The values you chose for the functions give such different distributions that the cumulative distribution plot is pretty boring - just rectangles. 您为函数选择的值给出了不同的分布,累积分布图非常无聊 - 只是矩形。 So I adjusted the defining values for NPV50 and NPV70 to make the three distributions more similar. 所以我调整了NPV50NPV70的定义值,使三个分布更相似。

So here is the code I used: 所以这是我使用的代码:

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

and here is the resulting output: 这是结果输出:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM