0

I am trying to fit a distribution using the following code:

fit.gamma <- fitdist(x, distr = "gamma", method = "mle")

I get the following error:

Error in fitdist(x, distr = "gamma", method = "mle") : data must be a numeric vector of length greater than 1

X is a numeric variable. It looks like this when plotted.1

Why do I get this error. Any tips are greatly appreciated.

Stata_user
  • 562
  • 3
  • 14
  • 2
    Try `class(x)`, better yet `str(x)`. What do they return? Can you post the output of `dput(x)` or if it's big of `dput(head(x, 20))` in the question, please? – Rui Barradas Feb 17 '19 at 21:54

2 Answers2

0
> class(x)
[1] "numeric"
> str(x)
 atomic [1:18839] 7 175 386 375 397 333 378 394 330 346 ...
 - attr(*, "na.action")=Class 'omit'  int [1:17] 1 209 267 286 288 297 299 300 304 305 ...
> dput(head(x, 20))
c(7, 175, 386, 375, 397, 333, 378, 394, 330, 346, 306, 344, 308, 
278, 291, 284, 252, 294, 277, 241)

Thanks

Stata_user
  • 562
  • 3
  • 14
  • I found the solution myself. I loaded the dataset from .dta format using read.dta. There must a bug in this package, because I tried saving it as csv and loading it using read_csv and the error message disappeared. – Stata_user Feb 19 '19 at 10:06
0

The issue appears to be that you used na.omit, which doesn't return a vector like fitdist is expecting.

Instead of this

x <- na.omit(x.init)
fit.gamma <- fitdist(x, distr = "gamma", method = "mle")

try converting the output of na.omit to a vector

x <- c(na.omit(x.init))
fit.gamma <- fitdist(x, distr = "gamma", method = "mle")
Jake Greene
  • 5,539
  • 2
  • 22
  • 26