Reputation: 33
How can I plot CDF and PDF in R for
f <- function(x) {((2*a*b)/(x^3))*((exp(-b/(x^2))^a))}
with range 0 to infinity
Upvotes: 3
Views: 5101
Reputation: 1021
I would use something like this (because I like ggplot2):
a <-1
b <- 2
f <- function(x) {((2*a*b)/(x^3))*((exp(-b/(x^2))^a))}
x <- seq(1, 20)
pdf <- f(x)
cdf <- cumsum(pdf)
library(ggplot2)
df <- data.frame(x, pdf, cdf)
ggplot(df, aes(x, pdf))+geom_line()
ggplot(df, aes(x, cdf))+geom_line()
Upvotes: 1
Reputation: 1707
You should specify a
and b
as arguments to the function, with default values. Then curve
can be used to plot the function.
f <- function(x,a=0.5,b=4.5) {((2*a*b)/(x^3))*((exp(-b/(x^2))^a))}
curve(f)
The way your code is now, a
and b
are most likely resolving to whatever is in the global environment, which you may not want later and could cause problems reproducing your results.
Upvotes: 1