0

I am trying to fit a Negative Binomial regression in R with the following code:

glm.nb(crash ~ offset(log(VMT)), data=df)

But getting this error:

Error in glm.fitter(x = X, y = Y, w = w, start = start, etastart = etastart, : NA/NaN/Inf in 'y'

How to fix this error? I intalled and loaded all the required packages (i.e. MASS, foreign, compactr).


Data:

crash <- c(46, 10, 9, 1, 0, 4, 14, 18, 7, 23, 16, 1, 2, 5, 3, 16)
VMT <- c(4800, 2769.5, 3990, 2808, 880, 2291, 3220, 3120, 2040, 2115.75, 2635, 1092, 1456, 2433.9, 921.85, 1256.51)
df <- data.frame(crash, VMT)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Asad Tan
  • 79
  • 1
  • 1
  • 7
  • With what little we know of the data, there's not much we can do. Perhaps you could shed some light on it (and make the question [reproducible](https://stackoverflow.com/questions/5963269)). – r2evans Mar 09 '20 at 04:20
  • The data looks like this: crash <- c(46, 10, 9, 1, 0, 4, 14, 18, 7, 23, 16, 1, 2, 5, 3, 16) VMT <- c(4800, 2769.5, 3990, 2808, 880, 2291, 3220, 3120, 2040, 2115.75, 2635, 1092, 1456, 2433.9, 921.85, 1256.51) – Asad Tan Mar 09 '20 at 04:35
  • I don't get any errors with the command and data you provided. – Edward Mar 09 '20 at 05:03
  • Which version of R are you using? I tried installing latest version of R (3.6.3) and all the required packages. Don't know why the error keeps coming. – Asad Tan Mar 09 '20 at 05:14
  • 2
    If you are getting an error, my advice is to show *exactly* what your inputs are, the function call you tried, and the error you got. You can just copy and paste text from your R session. Enclose the R stuff with three backticks before and three backticks after to tell Stackoverflow it's supposed to be not formatted as ordinary text. – Robert Dodier Mar 09 '20 at 06:15
  • you are fitting an intercept only model, is that intended? So you get this error because some of your values are way off... – StupidWolf Mar 09 '20 at 07:58
  • 1
    Asad, I added your data into your question, I hope you don't mind. It's generally better to have all required elements (non-base packages, minimal code, sample data) all within the question itself, since comments can be hidden from the user and/or just not read by them. – r2evans Mar 09 '20 at 13:46
  • 1
    Similar to Edward, I get no errors with your code, so either you are using different (larger?) data or your call to `glm.nb` is different. Your question is not reproducible by us, which makes it prone to being closed. Please update it with the information requested. Thanks! – r2evans Mar 09 '20 at 13:47
  • 1
    Also, try running the code from a clean R session (i.e. load only the `MASS` package and directly enter the code shown in your question) ... – Ben Bolker Mar 09 '20 at 14:20
  • I finally figured it out. It was because some of the columns I was using for fitting glm.nb had some NA values. I removed only those NA values, not the entire rows. Seems like the data frame of the predictor variables has to be in same shape. Thank you everyone. – Asad Tan Jul 30 '20 at 02:05

0 Answers0