2016-07-23 46 views
2

我有數據cdecn繪製經驗累積概率函數及其逆

set.seed(0) 
cdecn <- sample(1:10,570,replace=TRUE) 
a <- rnorm(cdecn,mean(cdecn),sd(cdecn)) 

我已經建立,其顯示的累積概率的曲線圖。

aprob <- ecdf(a) 
plot(aprob) 

enter image description here

我想知道我怎麼可以切換x軸和y軸得到一個新的情節,即ECDF的倒數。

此外,對於新的情節,有沒有辦法在我的曲線相交的地方添加一條垂直線0?

回答

2

我們可以做到以下幾點。我對代碼的評論非常具有說服力。

## reproducible example 
set.seed(0) 
cdecn <- sample(1:10,570,replace=TRUE) 
a <- rnorm(cdecn,mean(cdecn),sd(cdecn)) ## random samples 

a <- sort(a) ## sort samples in ascending order 
e_cdf <- ecdf(a) ## ecdf function 
e_cdf_val <- 1:length(a)/length(a) ## the same as: e_cdf_val <- e_cdf(a) 

par(mfrow = c(1,2)) 

## ordinary ecdf plot 
plot(a, e_cdf_val, type = "s", xlab = "ordered samples", ylab = "ECDF", 
    main = "ECDF") 

## switch axises to get 'inverse' ECDF 
plot(e_cdf_val, a, type = "s", xlab = "ECDF", ylab = "ordered sample", 
    main = "'inverse' ECDF") 

## where the curve intersects 0 
p <- e_cdf(0) 
## [1] 0.01578947 

## highlight the intersection point 
points(p, 0, pch = 20, col = "red") 

## add a dotted red vertical line through intersection 
abline(v = p, lty = 3, col = "red") 

## display value p to the right of the intersection point 
## round up to 4 digits 
text(p, 0, pos = 4, labels = round(p, 4), col = "red") 

enter image description here

+0

不,不需要刪除您的答案!我只是想知道你是否有更好的方法來添加直線。我終於找到了一些東西,但可能不是很直接。 –

+0

切換軸的繪圖是我正在尋找的。我將添加第二條曲線到曲線,我想說明曲線之間的差異。所以我需要一個垂直線穿過點(x,0)。在這種情況下,我會想要一個大致x = 0.05的垂直線。所以我的問題是如何確定這一點。 – Dylan

1
cdecn <- sample(1:10,570,replace=TRUE) 
a <- rnorm(cdecn,mean(cdecn),sd(cdecn)) 

aprob <- ecdf(a) 
plot(aprob) 

# Switch the x and y axes 
x <- seq(0,1,0.001754386) 
plot(y=knots(aprob), x=x, ylab = "Fn(y)") 

enter image description here

# Add a 45 degree straight line at 0, 0 
my_line <- function(x,y,...){ 
    points(x,y,...) 
    segments(min(x), y==0, max(x), max(y),...) 
} 
lines(my_line(x=x, y = knots(aprob))) 

enter image description here

0

的 「當x == 0直線」 有點讓我懷疑你想要一個QQplot:

qqnorm(a) 
qqline(a)