简体   繁体   中英

How to fix error in gam() in mgcv 'Error in terms.formula(formula, data = data) : invalid model formula in ExtractVars'

I cannot solve this seemingly simple error message when fitting gam() in mgcv package. Any help greatly appreciated.

'Error in terms.formula(formula, data = data): invalid model formula in ExtractVars'

I have read all similar posts that can be found on stackoverflow, but still have not be able to solve this issue. Some other posts seem to suggest that this can occur if variable names include spaces, but this is not the case for my error.

load package

library(mgcv)

read in data

join <- read.csv("join.csv", header = TRUE)

define factors

join$site <- factor(join$site)
join$season <- factor(join$season)
join$RHDV_transmis_cat <- factor(join$RHDV_transmis_cat)
join$RHDV2_arrive_cat <- factor(join$RHDV2_arrive_cat)

run model

gam_1 <- gam(RHDV_transmis_cat ~  s(age) + s(weight) + s(site) + s(RCV) + s(season, bs = "cc") + s(preceeding_mth_temp) + s(preceeding_mth_rain) + s(RHDV2_arrive_cat) + s(abun_adjust_dist) + te(abun_adjust_dist, RHDV2_arrive_cat, by ="fs") + s(RHDV2_arrive_cat, season, bs = "re"), data = join, family = binomial, method = "REML", select = TRUE)

Data available at here

I think you have a misunderstanding and a typo. Note in the te() smooth that you have by = "fs" where I think you meant to use bs = "fs" .

Secondly, if you want an ”fs” smooth, you don't use te() , you use s() to set it up. Where you have

te(abun_adjust_dist, RHDV2_arrive_cat, by = "fs")

You want

s(abun_adjust_dist, RHDV2_arrive_cat, bs = "fs")

Assuming you want a random smooth of abun_adjust_dist for the levels of RHDV2_arrive_cat .

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