Reputation: 3
I am trying unsuccessfully to plot the following function in R using curve. The function is the following:
y ~ GA(mu,sigma), with y >0, mu >0 and sigma >0
fGA <- function(y, mu, sigma) {
out <- (y^((1/sigma^2)-1))*exp(-y/sigma^2*mu)/(((sigma^2)*mu)^(1/sigma^2))*gamma(1/sigma^2)
return(out)
}
par(mfrow=c(2,2)) ##mu fixed value
curve(fGA(y, 0, 0.5), -10,10, col="green")
curve(fGA(y, 0, 1), -10,10, col="green")
curve(fGA(y, 0, 2), -10,10, col="green")
curve(fGA(y, 0, 3), -10,10, col="green")
par(mfrow=c(2,2)) ##sigma fixed value
curve(fGA(y, 0, 1), 0,5, col="blue")
curve(fGA(y, 0.5, 1), 0,5, col="blue")
curve(fGA(y, 1, 1), 0,5, col="blue")
curve(fGA(y, 2, 1), 0,5, col="blue")
Warning in min(x) : no non-missing arguments to min; returning Inf
Warning in max(x) : no non-missing arguments to max; returning -Inf
Error in plot.window(...) : finite values are needed for 'ylim'
What possibly is wrong?
Upvotes: 0
Views: 98
Reputation: 8811
I do not use curve
, but it seems that first your function fGA
need to use x
as an argument, I did that and worked. But fGA
it is returning Inf
, when mu = 0
, so returns an error
fGA <- function(x, mu, sigma) {
out <- (x^((1/sigma^2)-1))*exp(-x/sigma^2*mu)/(((sigma^2)*mu)^(1/sigma^2))*gamma(1/sigma^2)
return(out)
}
curve(fGA(x, 0.5, 1), 0,5, col="blue")
fGA(-10:10, 0, 0.5)
[1] -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf NaN Inf Inf Inf Inf Inf Inf Inf
[19] Inf Inf Inf
Upvotes: 1