Marcelo
Marcelo

Reputation: 3

plot a function in R using curve

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

Answers (1)

Vin&#237;cius F&#233;lix
Vin&#237;cius F&#233;lix

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

Function

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)
}

Example 1 - mu = 0.5 and sigma = 1

curve(fGA(x, 0.5, 1), 0,5, col="blue")

enter image description here

Example 2 - mu = 0 and sigma = .5

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

Related Questions