在 R 中使用神经网络进行时间序列预测的示例 [英] Example of Time Series Prediction using Neural Networks in R
本文介绍了在 R 中使用神经网络进行时间序列预测的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都有一个简短的教育示例,如何使用神经网络(R 中的 nnet)进行预测?这是一个在 R 中的时间序列示例
T = seq(0,20,length=200)Y = 1 + 3*cos(4*T+2) +.2*T^2 + rnorm(200)情节(T,Y,类型=l")
非常感谢
大卫
解决方案
我认为你可以使用 caret
包,特别是 train
函数
这个函数为一个数字设置一个调整参数的网格分类和回归例程.
require(quantmod)需要(网络)需要(插入符号)T = seq(0,20,length=200)y = 1 + 3*cos(4*T+2) +.2*T^2 + rnorm(200)数据 <- data.frame( y, x1=Lag(y,1), x2=Lag(y,2))名称(数据)<- c('y','x1','x2')dat <- dat[c(3:200),] #删除前 2 个观察值#拟合模型模型 <- train(y ~ x1+x2 ,日期,方法='nnet',linout=真,跟踪 = FALSE)ps <-预测(模型,数据)#检查结果情节(T,Y,类型=l",col = 2)行(T[-c(1:2)],ps, col=3)图例(5, 70, c("y", "pred"), cex=1.5, fill=2:3)
Anyone's got a quick short educational example how to use Neural Networks (nnet in R) for the purpose of prediction? Here is an example, in R, of a time series
T = seq(0,20,length=200)
Y = 1 + 3*cos(4*T+2) +.2*T^2 + rnorm(200)
plot(T,Y,type="l")
Many thanks
David
解决方案
I think you can use the caret
package and specially the train
function
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)
这篇关于在 R 中使用神经网络进行时间序列预测的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文