-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmvcd-v1.Rmd
101 lines (86 loc) · 3.52 KB
/
fmvcd-v1.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
---
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()
file_list <- list.files(pattern = "\\.cli$"); file_list
for (file in file_list){
# if the merged dataset doesn't exist, create it
if (!exists("dataset")){
dataset <- read.table(file, header=FALSE, sep="")
}
# 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)
}
}
dataset_krest <- dataset; names(dataset_krest) <- c("day", "month", "year", "prec", "temp")
```
#### To detect missing data.
```{r filter_cli}
dataset_krest_prec_missing <- dataset_krest[dataset_krest$prec == -9999, c(1,2,3,4)]; length(dataset_krest_prec_missing[,1])
dataset_krest_temp_missing <- dataset_krest[dataset_krest$temp == -9999, c(1,2,3,5)]; length(dataset_krest_temp_missing[,1]); # row.names.data.frame(dataset_krest_temp_missing)
dataset_krest_prec <- dataset_krest[dataset_krest$prec != -9999, c(1,2,3,4)]; summary(dataset_krest_prec)
dataset_krest_temp <- dataset_krest[dataset_krest$temp != -9999, c(1,2,3,5)]; summary(dataset_krest_temp)
```
#### 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)
```
#### replace missing data with appropriate design values.
+ Completely missing at random?
+ Missing at random?
+ Nonrandom pass?
```{r standart_packages}
#install.packages(c("VIM", "mice"))
require(dplyr); require(VIM); require(mice)
```
#### replace missing data with appropriate values calculated using the neural network.
```{r create_train}
#install.packages('neuralnet', dependencies=TRUE, repos='http://cran.rstudio.com/')
library(neuralnet)
train_out <- dataset_krest_temp$temp / 10.0;
train_in <- dataset_krest_temp[, c(1,2,3)]
#train_in <- c(1: length(train_out))
#train_in <- as.numeric(rownames(dataset_krest_temp))
#set.seed(1410) # Make the sample reproducible
#train.sample <- data.frame(NUM=train_in, TEMP=train_out); summary(train.sample)
#train.sample <- dataset_krest_temp
train.sample <- data.frame(train_in, temp=train_out); summary(train.sample)
```
#### Train neuralNet
```{r train_neuralnet}
rm(net.temp)
train.sample <- train.sample[train.sample$year == 1967,]
net.temp <- neuralnet(temp ~ day + month + year, train.sample, hidden = c(7,11,7), rep = 1, linear.output = TRUE, threshold = 0.01, stepmax = 10000)
#testdata <- as.numeric(rownames(dataset_krest_temp_missing))
testdata <- dataset_krest_temp_missing[,c(1,2,3)]
testdata <- train.sample[, c(1,2,3)]
net.results <- compute(net.temp, testdata) #Run them through the neural network
ls(net.results)
# Lets see the results
print(head(net.results$net.result))
plot(1:length(train.sample$temp), train.sample$temp)
```
#### syntetic
```{r}
s_in <- 1:length(train.sample$temp)
s_out <- train.sample$temp
tr <- data.frame(X=s_in, Y=s_out)
plot(tr)
net.tr<- neuralnet(Y ~ X, tr, hidden = c(4), rep = 1, linear.output = TRUE, threshold = 0.1, stepmax = 1000)
net.res <- compute(net.tr, tr$X)
print(head(net.res$net.result))
```
#### Links
+ http://r-analytics.blogspot.ru/2014/09/r.html#.WHW04dKLRhE
+ http://user2016.org/
+ https://earlconf.com/2017/
+ http://international.sfu-kras.ru/news/17388