Skip to content

Instantly share code, notes, and snippets.

@stockedge
Created December 13, 2015 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stockedge/ae068fd378c941321b99 to your computer and use it in GitHub Desktop.
Save stockedge/ae068fd378c941321b99 to your computer and use it in GitHub Desktop.
移動平均を使って予測してみる
> sum(diag(tbl)) / sum(tbl)
[1] 0.4991274
>#予測力全く無し
#moveingAverage from http://www.cookbook-r.com/Manipulating_data/Calculating_a_moving_average/
close.ma <- movingAverage(diff(read.csv("C:\\usdjpy_d.csv")$Close), n=10)
data <- NULL
for (i in seq(1,(length(close.ma)-50),by=10)) {
data <- rbind(data, c(close.ma[i:(i+39)],Dir = close.ma[i+50]))
}
data <- as.data.frame(data)
data$Dir <- as.factor(data$Dir > 0)
library(randomForest)
train <- data[1:(nrow(data)/2),]
test <- data[(nrow(data)/2):nrow(data),]
model.rf <- randomForest(Dir~., train)
pred <- predict(model.rf, test)
tbl <- table(pred, test$Dir)
sum(diag(tbl)) / sum(tbl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment