首页 > 解决方案 > 如何以图形方式表示分位数函数并在图形/绘图上表示其中位数?

问题描述

所以我有这个功能:

y=-2*log(1-x);

练习说我必须在绘图中表示这个函数,然后计算它的中位数 并在绘图上表示/识别它

标签: r

解决方案


什么时候x是 0 和 1 之间的所有值,并且y= (-2)log 10 (1-x),您可以执行以下操作:

# evenly sample a bunch of points between 0 and 1
x = seq(0, 1, 0.0001)

y = -2*log10(1-x)

# calculate the median of y
Y = median(y) 
Y
#[1] 0.60206

# when x supposed to be all values between 0 and 1, 
# x is at half way between 0 and 1 when y is at median 
X = 0.5
#X = 1-10^(-Y/2) # algebraically, this works, too
#X
##[1] 0.5

# plot x and y, and identify the point of (X,Y)
library(ggplot2)

qplot(x,y, geom="line") + geom_point(aes(X,Y), col = "blue", size = 4)

ggplot 方法

图上的大蓝点位于中位数。

plot()具有基本功能的替代方法

y <- function(x) {-2*log10(1-x)}

Y  = y(0.5) # y median
Y
#[1] 0.60206

plot(y, 0, 1, ylab="y", xlab="x", lwd=2)
points(0.5, Y)

绘图法

图中的空心圆圈表示中位数。


推荐阅读