绘制经验累积概率函数及其逆

时间:2016-07-23 15:25:27

标签: r plot cdf

我有数据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相交的位置添加垂直线?

3 个答案:

答案 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

答案 1 :(得分: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

答案 2 :(得分:0)

“x == 0”的“直线”让我怀疑你想要一个QQ剧情:

qqnorm(a)
qqline(a)