DKK*_*DKK 9 r time-series neural-network
任何人都有一个简短的教育示例如何使用神经网络(R中的nnet)进行预测?以下是R中的时间序列示例
T = seq(0,20,length=200)
Y = 1 + 3*cos(4*T+2) +.2*T^2 + rnorm(200)
plot(T,Y,type="l")
Run Code Online (Sandbox Code Playgroud)
非常感谢
大卫
ags*_*udy 16
我认为你可以使用caret
包,特别是train
功能
Run Code Online (Sandbox Code Playgroud)This function sets up a grid of tuning parameters for a number of classification and regression routines.
require(quantmod)
require(nnet)
require(caret)
T = seq(0,20,length=200)
y = 1 + 3*cos(4*T+2) +.2*T^2 + rnorm(200)
dat <- data.frame( y, x1=Lag(y,1), x2=Lag(y,2))
names(dat) <- c('y','x1','x2')
dat <- dat[c(3:200),] #delete first 2 observations
#Fit model
model <- train(y ~ x1+x2 ,
dat,
method='nnet',
linout=TRUE,
trace = FALSE)
ps <- predict(model, dat)
#Examine results
plot(T,Y,type="l",col = 2)
lines(T[-c(1:2)],ps, col=3)
legend(5, 70, c("y", "pred"), cex=1.5, fill=2:3)
Run Code Online (Sandbox Code Playgroud)