14wml
14wml

Reputation: 4166

Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ for Gamma distribution plot

I'm trying to plot the pdf distribution of Gamma(alpha=29, beta = 3) on a graph, but I get the error: "Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ". Why?

x <- seq(0, 1000, by = 1)
y <- dgamma(x, shape = 3, rate = 1/29, scale = 1/rate, log = FALSE)
plot(x, y, xlabel = "x", ylabel = "Gamma(29,3)")

Upvotes: 0

Views: 9074

Answers (1)

MrFlick
MrFlick

Reputation: 206197

Seems like you are probably ignoring errors. For me, the line

y <- dgamma(x, shape = 3, rate = 1/29, scale = 1/rate, log = FALSE)

gives the error

Error in dgamma(length(x), shape = 3, rate = 1/29, scale = 1/rate, log = FALSE) : object 'rate' not found

so your y variable is never set. You must have a left over one from some other code that is a different length than x. You should check before plotting by looking at length(x) and `length(y). Use

y <- dgamma(x, shape = 3, rate = 1/29, log = FALSE)

instead. This will use the default value for rate which is what you want. Maybe you were looking at the default parameter values for the function? You can't specify names of other parameters when passing values for a parameter when you call a function, you can only do this when you define a function.

Also your plot() command returns an error. The parameter names are xlab= and ylab= and not xlabel= and ylabel=

Upvotes: 0

Related Questions