r - 如何以图形方式表示分位数函数并在图形/绘图上表示其中位数?
问题描述
所以我有这个功能:
y=-2*log(1-x);
练习说我必须在绘图中表示这个函数,然后计算它的中位数 并在绘图上表示/识别它。
解决方案
什么时候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)
图上的大蓝点位于中位数。
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)
图中的空心圆圈表示中位数。
推荐阅读
- wpf - 如何实现在 WPF 树视图中选择树节点的命令
- docker - Flask、NGINX、uWSGI认证后随机重定向问题
- c# - 按 CTRL + S 时,Visual Studio 会自动创建异常文件
- python - 在 python 的子类构造函数中将对象作为列表传递
- java - 逻辑问题:列表响应的 REST-API 缓存
- reactjs - 如何在nextjs中获取上一页url
- upgrade - 如何以零停机时间将 istio 1.4.3 升级到最新版本
- python - 有人可以澄清 .env 文件在 VS Code 中的工作位置吗
- macos - react-native init AwesomeProject 给出错误 EPERM: operation not allowed, chmod '/node_modules/@react-native-com...'
- bots - 嘿,我一直在做一个不和谐的机器人,有一个问题,ts1005,有什么解决办法吗?