使用nnet预测股价 [英] stock price prediction by using nnet
问题描述
stock<-structure(list(week = c(1L, 2L, 5L, 2L, 3L, 4L, 3L, 2L, 1L, 5L,
1L, 3L, 2L, 4L, 3L, 4L, 2L, 3L, 1L, 4L, 3L),
close_price = c(774000L,
852000L, 906000L, 870000L, 1049000L, 941000L, 876000L, 874000L,
909000L, 966000L, 977000L, 950000L, 990000L, 948000L, 1079000L,
NA, 913000L, 932000L, 1020000L, 872000L, 916000L),
vol = c(669L,
872L, 3115L, 2693L, 575L, 619L, 646L, 1760L, 419L, 587L, 8922L,
366L, 764L, 6628L, 1116L, NA, 572L, 592L, 971L, 1181L, 1148L),
obv = c(1344430L, 1304600L, 1325188L, 1322764L, 1365797L,
1355525L, 1308385L, 1308738L, 1353999L, 1364475L, 1326557L,
1357572L, 1362492L, 1322403L, 1364273L, NA, 1354571L, 1354804L,
1363256L, 1315441L, 1327927L)),
.Names = c("week", "close_price", "vol", "obv"),
row.names = c(16L, 337L, 245L, 277L, 193L, 109L, 323L, 342L, 106L,
170L, 226L, 133L, 72L, 234L, 208L, 329L, 107L, 103L, 71L, 284L, 253L),
class = "data.frame")
我有一个名为Nam
的数据集,它具有349个观测值,我想使用nnet
到predict close_price
.
I have data set like this form called Nam
which has observations of 349 and I want to use nnet
to predict close_price
.
obs<- sample(1:21, 20*0.5, replace=F)
tr.Nam<- stock[obs,]; st.Nam<- stock[-obs,]
# tr.Nam is a training data set while st.Nam is test data.
library(nnet)
Nam_nnet<-nnet(close_price~., data=tr.Nam, size=2, decay=5e-4)
通过此声明,我认为我做了一定的功能来预测close_price
.
By this statement, I think I made a certain function to predict close_price
.
summary(Nam_nnet)
y<-tr.Nam$close_price
p<-predict(Nam_nnet, tr.Nam, type="raw")
我希望p
是close_price
的预测值,但只有值1.为什么p
没有连续值close_price
?
I expected p
to be the predicted value of close_price
, but it has only values of 1. Why doesn't p
have the continuous value of close_price
?
tt<-table(y,p)
summary(tt)
tt
推荐答案
我想我可以用一个可重现的示例做得更好,但是我认为问题可能是几个原因中的一个(或多个).首先,执行str(data)以确保每个变量的类型正确(因子,数字等).而且,神经网络通常对标准化,缩放和居中的数据有更好的响应,否则输入将被较大的数字输入过饱和,如果'week'变量为数字,则可能会出现这种情况.
I think I could do a bit better with a reproducible example but I think the problem may be one (or more) of several reasons. Firstly, do a str(data) to make sure each variable is of the correct type (factor, numeric, etc.). Also, Neural Nets usually respond better to standardized, scaled, and centered data otherwise the inputs get oversaturated with larger numeric inputs which might be the case if the 'week' variable is numeric.
总而言之,一定要检查每个变量的类型,以确保输入正确的格式,并考虑将数据缩放为平滑值,以使输入具有可比的幅度.
In summary, definitely check the types of each variable to make sure you are inputting the correct forms and consider scaling your data to be smooth and so the inputs are of comparable magnitudes.
这篇关于使用nnet预测股价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!