我正在使用神经网络包运行一个神经网络,但我遇到了一个问题,如果网络没有达到阈值错误预测以后就行不通了。我在这里找到了答案:R: Error in nrow[w] * ncol[w] : non-numeric argument to binary operator, while using neuralnet package
我打开了calculate.neuralnet函数,如答案中所述,注释掉了第65和66行,并保存了它。再次运行神经网络后,我现在得到了错误
Error in calculate.neuralnet(learningrate.limit = learningrate.limit, :could not find function "generate.startweights"
当我使用相同的fixInNamespace函数访问generate.startweights函数时,它的存在和可访问性。我已经重新安装了这个包并取消注释了那些行,但是我仍然得到关于generate.startweights的相同错误,这是我之前没有得到的。我尝试用我的问题评论原始解决方案,但我没有必要的50声誉。代码
regData <- read.csv('RegistrationandVoterData2.csv')
voteData <- read.csv('RegDataVote.csv')
mergeReg = regData[, 3:ncol(regData)]
mergeVote = voteData[, 6:ncol(voteData)]
mergeTotal = merge(mergeReg, mergeVote, by = c('RGPREC', 'RGPREC_KEY'), all = FALSE)
n<-names(mergeTotal[, c(3:66, 68:79)])
scaled <- scale(mergeTotal[, c(3:66, 68:79)])
scaleDF = as.data.frame(scaled)
names(scaleDF) = n
scaleDF$perDem = mergeTotal$DEM.y / (mergeTotal$REP.y + mergeTotal$DEM.y)
f <- as.formula(paste("perDem ~", paste(n[!n %in% "perDem"], collapse = " + ")))
smp_size <- floor(0.30 * nrow(mergeTotal))
## set the seed to make your partition reproductible
train_ind <- sample(seq_len(nrow(mergeTotal)), size = smp_size)
train <- scaleDF[train_ind, ]
smp_size2 = floor(0.2 *nrow(mergeTotal[-train_ind, ]))
test_ind = sample(seq_len(nrow(mergeTotal[-train_ind, ])), size = smp_size2)
test = scaleDF[-train_ind, ][test_ind, ]
errors = matrix(NA, 24, 2)
for(it in 2:25){
nn <- neuralnet(f,data=train,hidden=c(it),linear.output=T)
pr.nn <- compute(nn,test[, -ncol(test)])
preds = pr.nn$net.result
act = scaleDF$perDem[-train_ind][test_ind]
MSE.nn <- sum((act - preds)^2, na.rm = TRUE)/length(act)
pr.tr <- compute(nn,train[, -ncol(train)])
predsTr = pr.tr$net.result
actTr = scaleDF$perDem[train_ind]
MSE.nnTr <- sum((actTr - predsTr)^2, na.rm = TRUE)/length(actTr)
errors[it, ] = c(MSE.nnTr, MSE.nn)
}