简体   繁体   中英

Trouble in GAM model in R software

I am trying to run the following code on R:

m <- gam(Flp_pop ~ s(Flp_CO, bs = "cr", k = 30), data = data, family = poisson, method = "REML")

My dataset is like this:

enter image description here

But when I try to execute, I get this error message:

"Error in if (abs(old.score - score) > score.scale * conv.tol) { : 
  missing value where TRUE/FALSE needed
In addition: There were 50 or more warnings (use warnings() to see the first 50)"

I am very new to R, maybe it is a very basic question. But does anyone know why this is happening? Thanks!

The Poisson distribution has support on the non-negative integers and you are passing a continuous variable as the response. Here's an example with simulated data

library("mgcv")
library("gratia")
library("dplyr")

df <- data_sim("eg1", seed = 2) %>% # simulate Gaussian response
  mutate(yabs = abs(y)) # make y non negative

mp <- gam(yabs ~ s(x2, bs = "cr"), data = df,
          family = poisson, method = "REML")
# fails

which reproduces the error you saw

Error in if (abs(old.score - score) > score.scale * conv.tol) { : 
  missing value where TRUE/FALSE needed
In addition: There were 50 or more warnings (use warnings() to see the first 50)

The warnings are of the form:

$> warnings()[1]                                                               
Warning message:
In dpois(y, y, log = TRUE) : non-integer x = 7.384012

Indicating the problem; the model is evaluating the probability mass for your response data given the estimated model and you're evaluating this at the indicated non-integer value, which returns a 0 mass plus the warning.

If we'd passed the original Gaussian variable as the response, which includes negative values, the function would have errored out earlier:

mp <- gam(y ~ s(x2, bs = "cr"), data = df,
          family = poisson, method = "REML")

which raises this error:

r$> mp <- gam(y ~ s(x2, bs = "cr"), data = df, 
              family = poisson, method = "REML")                                
Error in eval(family$initialize) : 
  negative values not allowed for the 'Poisson' family

An immediate but not necessarily advisable solution is just to use the quasipoisson family

mq <- gam(yabs ~ s(x2, bs = "cr"), data = df,
          family = quasipoisson, method = "REML")

which uses the same mean variance relationship as the Poisson distribution but not the actual distribution so we can get away with abusing it.

Better would be to ask yourself why you were trying to fit a model that is ostensibly for counts to a response that is a continuous (non-negative) variable?

If the answer is you had a count but then normalised it in some way (say by dividing by some measure of effort like area surveyed or length of observation time) then you should use an offset of the form + offset(log(effort_var)) added to the model formula, and use the original non-normalised integer variable as the response.

If you really have a continuous response and the poisson was an over sight, try fitting with family = Gamma(link = "log")) or family = tw() .

If it's something else, you should edit your question to include that info and perhaps we here can help or the question could be migrated to CrossValidated if the issue is more statistical in nature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM