Reputation: 375
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.
library(mgcv)
join <- read.csv("join.csv", header = TRUE)
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)
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)
Upvotes: 0
Views: 1394
Reputation: 174908
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
.
Upvotes: 1