【问题标题】:Error in as.data.frame.default when fitting a GAM拟合 GAM 时 as.data.frame.default 出错
【发布时间】:2021-12-04 04:25:31
【问题描述】:

我正在使用gam R 包。当我使用step.Gam() 执行逐步回归时,我收到此错误:

Error in as.data.frame.default(data, optional = TRUE) : 
  cannot coerce class ‘"function"’ to a data.frame

这是一个可重现的例子:

library(dplyr)
library(gam)    

data(mtcars)

df = mtcars %>% mutate(vs = as.factor(vs),
                       am = as.factor(am))

f0 = as.formula("mpg ~ cyl + disp + hp + drat + wt + qsec + vs + am + gear + carb")
m.gam.0 = gam(f0, data = df)
f.gam.step = gam.scope(frame = (df %>% 
                                  select(mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb) %>%
                                  as.data.frame()),
                       response = 1,
                       arg=c("df=4"))

m.gam.step = step.Gam(object = m.gam.0, scope = f.gam.step, direction="both")

为什么会出现这个错误?

【问题讨论】:

  • 尝试使用除 df 之外的其他变量,因为 df 是基本 R 函数。
  • 非常感谢!我已将变量的名称从“df”更改为其他名称,现在它可以工作了!

标签: r regression gam


【解决方案1】:

错误是因为 df 是基本 R 函数,因此它将变量 df 视为函数而不是数据帧。如果您将数据框名称更改为不是函数名称而不是 df 的其他名称,它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 2021-04-09
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    相关资源
    最近更新 更多