Generalized additive model data.frame.default erro

2019-02-26 20:30发布

问题:

I am trying to run a general additive model using the mgcv package, but I keep getting a model.frame.default error:

Error in model.frame.default(formula = Presence ~ Sex + wind_speed + baro +  : 
attempt to apply non-function

Here is the code I am using (I am using "bam()" because of the size of the dataset):

stormGAM <- bam(Presence~Sex+wind_speed+s(wind_direc)+baro+s(SST_C)+as.factor(daynight), 
            data=PJstorm_alldata, family=binomial, na.action=TRUE)

and here is what the data looks like:

'data.frame':   31795 obs. of  25 variables:
 $ Con_hour        : num  20127330 20127340 20127350 20127360 20127370 ...
 $ Year            : int  2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 ...
 $ Month           : int  7 7 7 7 7 7 7 7 7 7 ...
 $ Day             : int  3 3 3 3 3 3 3 3 3 3 ...
 $ Hour            : int  3 4 5 6 7 8 9 10 11 12 ...
 $ Timestamp       : POSIXct, format: "2012-07-03 03:00:00" "2012-07-03 04:00:00" "2012-07-03 05:00:00" ...
 $ Date            : Date, format: "2012-07-03" "2012-07-03" "2012-07-03" ...
 $ Region          : Factor w/ 1 level "Jervis Bay": 1 1 1 NA NA NA NA NA NA NA ...
 $ Station         : Factor w/ 17 levels "JB1","JB10","JB11",..: 12 12 12 NA NA NA NA NA NA NA ...
 $ ReceiverID      : Factor w/ 37 levels "VR2W-100736",..: 5 5 5 NA NA NA NA NA NA NA ...
 $ TagID           : Factor w/ 54 levels "A69-1303-32577",..: 43 43 43 NA NA NA NA NA NA NA ...
 $ Sex             : Factor w/ 2 levels "Female","Male": 1 1 1 NA NA NA NA NA NA NA ...
 $ wind_speed      : num  11 11 10 12 11 11 14 15 20 24 ...
 $ wind_direc      : num  277 282 278 272 252 269 256 244 220 207 ...
 $ sea_level_baro   : num  1018 1018 1018 1019 1019 ...
 $ baro            : num  1018 1018 1018 1019 1019 ...
 $ max_wind        : num  17 13 13 17 17 21 22 24 33 41 ...
 $ SST_C           : num  17.4 17.4 17.4 17.4 17.4 ...
 $ Presence        : int  1 1 1 0 0 0 0 0 0 0 ...
 $ gbirowsums      : int  1 1 1 0 0 0 0 0 0 0 ...
 $ Total_tagged    : int  1 1 1 1 1 1 1 1 1 1 ...
 $ Prop_Present    : num  1 1 1 0 0 0 0 0 0 0 ...
 $ sunrise         : POSIXct, format: "2012-07-03 07:05:34" "2012-07-03 07:05:34" "2012-07-03 07:05:34" ...
 $ sunset          : POSIXct, format: "2012-07-03 16:57:00" "2012-07-03 16:57:00" "2012-07-03 16:57:00" ...
 $ daynight        : chr  "night" "night" "night" "night" ...

I cannot seem to find anything obvious wrong with my formula. I have checked to make sure there are not errors with column lengths not matching, and I don't see any missing parentheses, commas, or +'s. I compared my code to some of my colleagues who have used the mgcv package, but I cannot figure out the issue. Any suggestions?

Thank you for any help.

回答1:

The problem is this

na.action = TRUE

na.action requires a function and you passed it a logical, (from ?bam)

na.action: a function which indicates what should happen when the data contain NAs. The default is set by the na.action setting of options, and is na.fail if that is unset. The factory-fresh default is na.omit.

Essentially, within model.frame() you were basically asking R to evaluate

TRUE(df)

which rightly throws an error as TRUE isn't a function yet was being called as one.

If you want to omit rows with NAs rather than fail if they occur, use

na.action = na.omit


标签: r gam mgcv