首页 文章

条件错误 . 公式(公式):公式中的'.'并且没有'data'参数

提问于
浏览
25

我想用神经网络进行预测 .

创建一些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)

term.formula(公式)中的错误:' . '在公式中没有'数据'参数

例如,在lm(线性模型)中,这是有效的 .

2 回答

  • 5

    正如我的评论所述,这看起来像是非导出函数 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
    
  • 44

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

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

    reformulate() 不需要使用 paste() 并自动将这些术语添加到一起 .

相关问题