|
用nnet()预测,8个参数,3000组数据训练,用regression结果预测,得到的结果为定值,请问有没有高手解答一下,找了无数网站也没有解决方法。下面附上代码:
# basic code to apply neural network do the regression and then use it into the wind forecasting
# nnet function is used in the code
# the result can be compared to the one in the multi parameters regression
# preparation
# setting up the working directory
setwd("/Users/joeyfueng/Desktop/Rearch/Wind Power Forecasting/ERCOT/forec by ANN")
# loading the necessary packages
library(stats)
library(ggplot2)
library(fBasics)
library(MASS)
library(VGAM)
library(HyperbolicDist)
library(neuralnet)
library(nnet)
library(ROCR)
library(NeuralNetTools)
# setting up system time calculation
begtime <-Sys.time()
######!!!!!!! conduct the neural network regression!!!!!!!!
# read in forecasts and actuals and build up the data frame
fadata <- data.matrix(read.table("parameterdata.csv",header = FALSE,sep = ",")) #should change max if trainning with big data
parameters <- as.data.frame(fadata)
write.table(parameters, file = "datawithoutbreak.csv",row.names=FALSE, na="",col.names=FALSE, sep=",")
# set trainning dataset
x1 <- parameters$V1[1:1000]/26.90665245
x2 <- parameters$V2[1:1000]/359.9948425
x3 <- parameters$V3[1:1000]/16
x4 <- parameters$V3[2:1001]/16
x5 <- parameters$V3[3:1002]/16
x6 <- parameters$V4[1:1000]/1.306360245
x7 <- parameters$V5[1:1000]/313.1581421
x8 <- parameters$V6[1:1000]/103287.1953
y <- parameters$V3[7:1006]/16
datasets <- data.frame(x1,x2,x3,x4,x5,x6,x7,x8)
# using nnet package conducting the one hidden layer ANN method do the regression!!!!!!!!!!!!!!!!!!!!!!!!
a = nnet(y~., data=datasets,size=20,maxit=10000,decay=0.001,linout = TRUE, trace = FALSE,report=TRUE)
plotnet(a, alpha = 0.5)
# predict
# first set the data to be predicted
ws <- parameters$V1[1010:631292]/26.90665245
wd <- parameters$V2[1010:631292]/359.9948425
p1 <- parameters$V3[1008:631290]/16
p2 <- parameters$V3[1009:631291]/16
p3 <- parameters$V3[1010:631292]/16
dst <- parameters$V4[1010:631292]/1.306360245
temper <- parameters$V5[1010:631292]/313.1581421
pres <- parameters$V6[1010:631292]/103287.1953
fcdata <- data.frame(ws,wd,p1,p2,p3,dst,temper,pres)
# change name to match the model data
names(fcdata)[1] <- "x1"
names(fcdata)[2] <- "x2"
names(fcdata)[3] <- "x3"
names(fcdata)[4] <- "x4"
names(fcdata)[5] <- "x5"
names(fcdata)[6] <- "x6"
names(fcdata)[7] <- "x7"
names(fcdata)[8] <- "x8"
# predict with neural network
pp <- predict(a,fcdata)*16
# real power
rp <- parameters$V3[1014:631296]
plot(rp, pp,
main="ANN predictions vs Actual",
xlab="Actual")
# write the result in a csv file
# set a fataframe for forecast and actual power
fanda <- data.frame(pp,rp)
write.table(fanda, file = "MyData.csv",row.names=FALSE, na="",col.names=FALSE, sep=",") |
|