2

我正在尝试使用 mgcv 包运行一般附加模型,但我不断收到 model.frame.default 错误:

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

这是我正在使用的代码(由于数据集的大小,我使用“bam()”):

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

这是数据的样子:

'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" ...

我似乎找不到我的公式有任何明显的错误。我已检查以确保没有列长度不匹配的错误,并且我没有看到任何缺少的括号、逗号或 + 号。我将我的代码与一些使用 mgcv 包的同事进行了比较,但我无法找出问题所在。有什么建议么?

感谢您的任何帮助。

4

1 回答 1

4

问题是这个

na.action = TRUE

na.action需要一个函数,并且您将其传递给逻辑,(来自?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
于 2016-07-15T17:02:59.753 回答