【问题标题】:How to fix error in gam() in mgcv 'Error in terms.formula(formula, data = data) : invalid model formula in ExtractVars'如何修复mgcv中gam()中的错误'terms.formula(公式,数据=数据)中的错误:ExtractVars中的模型公式无效'
【发布时间】:2020-02-23 09:48:09
【问题描述】:

在 mgcv 包中安装 gam() 时,我无法解决这个看似简单的错误消息。非常感谢任何帮助。

'terms.formula(formula, data = data) 中的错误:ExtractVars 中的模型公式无效'

我已经阅读了可以在 stackoverflow 上找到的所有类似帖子,但仍然无法解决这个问题。其他一些帖子似乎表明,如果变量名包含空格,则可能会发生这种情况,但我的错误并非如此。

加载包

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)

Data available at here

【问题讨论】:

  • 您是否也因为在平滑函数中使用因子变量而收到错误(而不是作为随机效应)
  • 不,我没有收到在平滑函数中使用因子的错误,但我是使用 GAM 的新手,所以很想知道这是否通常不建议/可能?
  • 不,我认为在平滑中使用因子是不明智的(除非它们被用作随机效果)。例如,假设在普通线性回归中,您在连续变量上添加 x^2 项,您不会尝试对分类变量进行这种转换......同样适用于样条曲线。
  • 谢谢,非常感谢

标签: r gam mgcv


【解决方案1】:

我认为你有一个误解和一个错字。请注意te() 平滑,你有by = "fs" 我认为你打算使用bs = "fs"

其次,如果您想要”fs” 平滑,则不要使用te(),而是使用s() 进行设置。你在哪里

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

你想要

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

假设您希望abun_adjust_dist 的水平随机平滑RHDV2_arrive_cat

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多