簡體   English   中英

term.formula(formula) 中的錯誤:'.' 在公式中,沒有“數據”參數

[英]Error in terms.formula(formula) : '.' in formula and no 'data' argument

我正在嘗試使用神經網絡進行預測。

創建一些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'

制作數據框:

dt <- data.frame(x, y)

現在,我遇到了錯誤

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

term.formula(formula) 中的錯誤:'.' 在公式中,沒有“數據”參數

例如,在 lm(linear model) 中這是有效的。

正如我的評論所說,這看起來像是非導出函數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

為上一個答案提供更簡單的替代方案,您可以使用reformulate()dt名稱創建一個公式:

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

reformulate()不需要使用paste()並自動將術語添加在一起。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM