-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmvcd-v0.Rmd
126 lines (107 loc) · 4.53 KB
/
fmvcd-v0.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
---
title: "filling in missing values in climate data"
output:
html_document: default
html_notebook: default
---
```{r read_cli}
rm(list=ls())
#setwd("C:/Users/IVA/Dropbox/24516/trace2017/fmvcd/krest/"); getwd()
setwd("C:/Users/teipolykom/git/fmvcd/krest/"); getwd()
#setwd("Z:/home/larisa/Dropbox/git/fmvcd/krest/"); getwd()
#setwd("/home/larisa/Dropbox/git/fmvcd/krest/"); getwd()
file_list <- list.files(pattern = "\\.cli$"); file_list
for (file in file_list){
# if the merged dataset does exist, append to it
if (exists("dataset")){
temp_dataset <-read.table(file, header=FALSE, sep="")
dataset<-rbind(dataset, temp_dataset)
rm(temp_dataset)
}
# if the merged dataset doesn't exist, create it
if (!exists("dataset")){
dataset <- read.table(file, header=FALSE, sep="")
}
}
dataset_krest <- dataset; names(dataset_krest) <- c("day", "month", "year", "prec", "temp")
str(dataset_krest)
```
#### Replacement of -9999 to a default value NA
```{r set_NA}
dataset_krest[dataset_krest$prec == -9999, 4] <- NA;
dataset_krest[dataset_krest$temp == -9999, 5] <- NA; summary(dataset_krest)
```
#### Prediction - Neural network for regression
Esli vy zanimaetes' modelirovaniem chego ugodno na dannyh, to vam nuzhno imet' dve vyborki: trenirovochnuju i testovuju. Na trenirovochnoj vy stroite model', a na testovoj dolzhny proverit' ee kachestvo;)
```{r kresr1969}
#install.packages('neuralnet', dependencies=TRUE, repos='http://cran.rstudio.com/')
library(neuralnet)
library(pracma)
NORM_TEMP <- 450.0
krestTemp1969 <- dataset_krest[dataset_krest$year == 1969, 5]
summary(krestTemp1969) ###
Temp <- krestTemp1969 / NORM_TEMP
Time <- (1:length(Temp)) / 370.0
#Time <- 1.0 / (1:length(Temp))
year1966.data <- data.frame(Time, Temp)
cat("All days=", nrow(year1966.data), "\n")
cat("Days na.omit=", nrow(na.omit(year1966.data)),"\n")
year1966.data.train <- na.omit(year1966.data)
# Train, Test and Validation 70: 20: 10
train <- year1966.data.train[sample(nrow(year1966.data.train), 296), ]
test <- year1966.data.train[sample(nrow(year1966.data.train), 96), ]
validation <- year1966.data[is.na(year1966.data$Temp), ]
plot(train)
ptm <- proc.time(); tic()
net.temp <- neuralnet(Temp~Time, train, hidden=c(7,11,11,7), threshold=0.005, linear.output=TRUE)
toc(); cat("runTime= ", proc.time() - ptm,"\n")
cat("Error= ", net.temp$result.matrix[1],"\n")
#save(net.temp, file="net.temp65-11.RData")
# plot(net.temp)
# https://journal.r-project.org/archive/2010-1/RJournal_2010-1_Guenther+Fritsch.pdf
# https://www.r-bloggers.com/selecting-the-number-of-neurons-in-the-hidden-layer-of-a-neural-network/
# https://geektimes.ru/post/211610/
```
####
```{r compute}
computeTemp <- compute(net.temp, train[, c(1)])$net.result
summary(computeTemp)
plot(train[, c(1)], computeTemp, col='red')
points(train, col='blue')
computeTempAll <- compute(net.temp, Time)$net.result
summary(computeTempAll)
#plot(Time, Temp, col='blue')
#points(Time, computeTempAll, col='red')
```
#### filling the gaps and write the result to a file
```{r nnet}
filledKrestTemp1969 <- krestTemp1969
for(i in (1: length(filledKrestTemp1969))){
if(is.na(filledKrestTemp1969[i])){
filledKrestTemp1969[i] <- as.integer(computeTempAll[i] * NORM_TEMP)
}
}
summary(filledKrestTemp1969)
TEMP <- dataset_krest[dataset_krest$year == 1969, 5]
TIME <- 1: length(TEMP)
plot(TIME, TEMP, col='blue')
points(TIME, filledKrestTemp1969, col='red')
filledKrest1969 <- data.frame(dataset_krest[dataset_krest$year == 1969, c(1,2,3,4,5)], filledTemp=filledKrestTemp1969)
```
####
```{r save_file}
write.table(filledKrest1969, file="1969.cli.filled", row.names = FALSE)
# write.table(filledKrest1969, file="1969.cli.filled", row.names = FALSE)
```
#### Links
+ https://www.r-bloggers.com/using-neural-network-for-regression/
+ https://www.r-bloggers.com/fitting-a-neural-network-in-r-neuralnet-package/
+ https://www.r-bloggers.com/a-primer-on-universal-function-approximation-with-deep-learning-in-torch-and-r/
+ https://www.r-bloggers.com/practical-implementation-of-neural-network-based-time-series-stock-prediction-part-1/
+ https://heuristically.wordpress.com/2011/11/17/using-neural-network-for-regression/
+ https://beckmw.wordpress.com/2013/08/12/variable-importance-in-neural-networks/
+ https://beckmw.wordpress.com/tag/neuralnet/
+ http://r-analytics.blogspot.ru/2014/09/r.html#.WHW04dKLRhE
+ http://user2016.org/
+ https://earlconf.com/2017/
+ http://international.sfu-kras.ru/news/17388