简体   繁体   English

在r中绘制累积分布函数

[英]Drawing Cumulative Distribution Function in r

I have the following pdf, 我有以下pdf,

f(x)=2(1-x)^2;  2=>x>1

Now i have to plot the cdf of the random variable in r . 现在我必须在r绘制随机变量的cdf

#cumulative distribution function

Fx <- function(x){
          (2/3) * (x^3-3*x^2+3*x-1)
      }

R <- runif(100,1,2)

cum_prob <- Fx(R) # determining cumulative probability

But i don't know which command should i use to draw the cdf . 但我不知道应该使用哪个命令来绘制cdf

hist() & barplot() are not appropriate for cdf. hist()barplot()不适合cdf。 Because as far i know plotting a cdf, it requires the values of random variable in X-axis, and cumulative probability in Y-axis. 因为据我所知绘制一个cdf,它需要X轴上的随机变量值和Y轴上的累积概率。

Also my variable is continuous. 我的变量也是连续的。

Responding to the code in the comment: 回应评论中的代码:

fx <- function(x)2*(x-1)^2 
integrate(fx,1,2)
#------------
0.6666667 with absolute error < 7.4e-15
plot(seq(1,2,by=0.01), cumsum(fx(seq(1,2,by=0.01))*0.01 ), type="l" ) 

You can use this: 你可以用这个:

Define density: 定义密度:

f <- function(x) (2*(1-x)^2)*(1<x & x<=2)

Plot density: 图密度:

plot(f,0,3)

Plot CDF: 情节CDF:

plot(Vectorize(function(X)integrate(f,0,X)$value),0,3,add=TRUE)

Note: 0 and 3 are limits of x axis in the plots; 注意:0和3是图中x轴的限制; you can change them. 你可以改变它们。 Also note your density is not normalized. 另请注意,您的密度未标准化。

在此输入图像描述

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

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