【问题标题】:Error in terms.formula(formula) : '.' in formula and no 'data' argumentterms.formula(公式)中的错误:“。”在公式中,没有“数据”参数
【发布时间】:2013-07-22 18:18:29
【问题描述】:

我正在尝试使用神经网络进行预测。

创建一些 X:

x <- cbind(seq(1, 50, 1), seq(51, 100, 1))

创建 Y:

y <- x[,1]*x[,2]

给他们起个名字

colnames(x) <- c('x1', 'x2')
names(y) <- 'y'

制作data.frame:

dt <- data.frame(x, y)

现在,我得到了错误

model <- neuralnet(y~., dt, hidden=10, threshold=0.01)

terms.formula(formula) 中的错误:'.'在公式中,没有“数据” 论据

例如,在 lm(线性模型)中,这是可行的。

【问题讨论】:

  • neuralnet 正在通过非导出函数neuralnet:::generate.initial.variables 对公式进行大量操作。该功能存在错误。我建议您联系维护者并将此示例或问题链接发送给他们。

标签: r prediction r-formula


【解决方案1】:

正如我的评论所述,这看起来像是非导出函数neuralnet:::generate.initial.variables 中的一个错误。作为一种解决方法,只需从 dt 的名称构建一个长公式,不包括 y,例如

n <- names(dt)
f <- as.formula(paste("y ~", paste(n[!n %in% "y"], collapse = " + ")))
f

## gives
> f
y ~ x1 + x2

## fit model using `f`
model <- neuralnet(f, data = dt, hidden=10, threshold=0.01)

> model
Call: neuralnet(formula = f, data = dt, hidden = 10, threshold = 0.01)

1 repetition was calculated.

        Error Reached Threshold Steps
1 53975276.25     0.00857558698  1967

【讨论】:

  • 抱歉,我听不懂。为什么误差值这么大?所以我做了,但得到了恒定的预测值。你能帮帮我吗?
  • @luckyi 这可能是一个统计问题,不适合Stack Overflow 尝试在Cross Validated 上提问。
  • 这也发生在 ols() 的包 RMS 中。同样的修复在那里也有效
  • @GavinSimpson - 感谢您的解决方案,Gavin!我在新的 mpath 包中遇到了同样的公式错误。
  • 它不会从公式左侧删除 y 值。到了最后。
【解决方案2】:

提供比上一个答案更简单的替代方案,您可以使用 reformulate()dt 的名称创建公式:

f <- reformulate(setdiff(colnames(dt), "y"), response="y")

reformulate() 不需要使用 paste() 并自动将术语添加在一起。

【讨论】:

    【解决方案3】:

    扩展公式

    f <- formula(terms(f, data= dt))
    

    甚至更短

    f <- formula(dt, f)
    

    其中f 是公式,dt 是数据。


    例如,原始公式可能是:

    f <- as.formula("y ~ .")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多