在函数的公式部分插入变量有3种方式:
首先使用.,它将包括data data.frame 中除响应变量(在本例中为变量输出)之外的所有变量:
net <- neuralnet(Output ~ ., data, hidden=0) #apart from Output all of the other variables in data are included
如果您的 data.frame 仅具有 Output 和另外 30 个变量,请使用此选项。
第二如果您想使用名称向量从数据data.frame中包含,您可以尝试:
names <- c('var1','var2','var3') #choose the names you want
a <- as.formula(paste('Output ~ ' ,paste(names,collapse='+')))
> a
Output ~ var1 + var2 + var3 #this is what goes in the neuralnet function below
所以你可以使用:
net <- neuralnet( a , data, hidden=0) #use a in the function
如果您可以提供 30 个变量名称的向量,请使用它
第三只需在函数中使用您想要的列子集data data.frame 例如:
net <- neuralnet(Output ~ ., data=data[,1:31] , hidden=0)
使用它(或任何其他方便的子集)并选择您需要的 30 个变量以及输出变量。然后使用. 包含所有内容。
希望对你有帮助!