-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmvcd-prec.Rmd
152 lines (138 loc) · 5.9 KB
/
fmvcd-prec.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
---
title: "PREC - filling in missing values in climate data"
author: "IVA"
date: '24.1.17'
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
### The calculation of all values of the vector by the neural network
```{r compute}
compute_prec_all <- function(net.train, Time){
return(compute(net.train, Time)$net.result)
}
```
### filling the gaps
```{r filling}
fillin_missing_data <- function(vector_cli, nnet_cli, NORM_TEMP){
for(i in (1: length(vector_cli))){
if(is.na(vector_cli[i])){
vector_cli[i] <- as.integer(nnet_cli[i] * NORM_TEMP)
}
}
return(vector_cli)
}
```
### The construction of neural models and the calculation of missing data
```{r nnet_cli}
library(neuralnet)
NORM_TEMP <- 450.0; NORM_TIME <- 370.0; NUM_TRAIN <- 65; HIDDEN <- 11
NUM_TRAIN <- 265; HIDDEN <- 32
nnet_prec_cli <- function(vector_cli, vec_type = "temp"){
if( length(vector_cli) < 365) warning("Your year has fewer than 365 days")
Temp <- vector_cli / NORM_TEMP
Time <- (1:length(Temp)) / NORM_TIME
year_data <- data.frame(Time, Temp)
if(nrow(year_data) - nrow(na.omit(year_data)) == 0){
cat("The vector does not contain missing values\n")
return(vector_cli)
} else {
train <- na.omit(year_data)
train <- train[sample(nrow(train), NUM_TRAIN), ]
net.train <- neuralnet(Temp~Time, train, hidden = HIDDEN, threshold = 0.01, linear.output=TRUE)
cat("Error=", net.train$result.matrix[1],"\n")
nnet_cli <- compute_all(net.train, Time)
fillin_vector_cli <- fillin_missing_data(vector_cli, nnet_cli, NORM_TEMP)
}
return(fillin_vector_cli)
}
```
## model neural network
```{r model_neural_network}
require(nnet)
require(neuralnet)
#filled_path <- "C:/Users/teipolykom/git/fmvcd/krest/"
filled_path <- "/home/larisa/Dropbox/git/fmvcd/krest/"
setwd(filled_path); getwd()
cli_dataset <-read.table("1967.cli", header=FALSE, sep="")
names(cli_dataset) <- c("day", "month", "year", "prec", "temp");
cli_dataset[cli_dataset$prec == -9999, 4] <- NA
print(summary(cli_dataset[, c(4)]))
prec_vector <- cli_dataset[, 4]
# scale inputs: divide by 191 to get 0-1 range
prec_vector_normalize <- prec_vector / max(na.omit(prec_vector))
plot(1: length(prec_vector), prec_vector_normalize)
train <- data.frame(time= (1: length(prec_vector_normalize)), prec=prec_vector_normalize)
train <- data.frame(time= (1: length(prec_vector)), prec=prec_vector/1.0)
train <- na.omit(train)
nnet.fit <- nnet(prec ~ time, data=train, size=21)
neuralnet.fit <- neuralnet(prec ~ time, train, hidden = 11, threshold = 0.1, linear.output=TRUE)
cat("Error =", neuralnet.fit$result.matrix[1],"\n")
neuralnet.compute <- compute(neuralnet.fit, train[,1])$net.result*191
# multiply 191 to restore original scale
nnet.predict <- predict(nnet.fit)*191.0
prec_stat <- c(0L, 0L, 0L, NA, 4L, 0L, 0L, 2L, NA, NA, 5L, NA, NA, NA, 0L,
NA, NA, 2L, 0L, 6L, 11L, 8L, 0L, NA, 4L, 0L, NA, 0L, 0L, NA,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, NA, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 11L, 8L, 0L, 5L, 2L, NA, 2L, NA, 2L, 0L, 0L, 5L, 4L,
4L, 22L, 11L, 0L, 0L, 2L, 5L, NA, NA, 16L, 11L, 2L, 2L, 0L, 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 0L, NA, 0L, 0L,
0L, 0L, 5L, 6L, 0L, 0L, 2L, 9L, 8L, 4L, 0L, 16L, 0L, 0L, 2L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L)
# https://heuristically.wordpress.com/2011/11/17/using-neural-network-for-regression/
# https://www.rdocumentation.org/packages/caretEnsemble/versions/2.0.0/topics/caretEnsemble
# https://www.researchgate.net/post/How_can_I_change_standardized_predicted_values_of_neural_network_into_non-standard_values
# http://www.di.fc.ul.pt/~jpn/r/neuralnets/neuralnets.html
# http://www.parallelr.com/r-deep-neural-network-from-scratch/
# https://github.com/PatricZhao/ParallelR
# http://playground.tensorflow.org/#activation=tanh&batchSize=10&dataset=circle®Dataset=reg-plane&learningRate=0.03®ularizationRate=0&noise=0&networkShape=4,2&seed=0.26118&showTestData=false&discretize=false&percTrainData=50&x=true&y=true&xTimesY=false&xSquared=false&ySquared=false&cosX=false&sinX=false&cosY=false&sinY=false&collectStats=false&problem=classification&initZero=false&hideText=false
# http://beyondvalence.blogspot.ru/2014/04/r-comparing-multiple-and-neural-network.html
# http://machinelearningmastery.com/non-linear-regression-in-r/
#http://www.dataminingblog.com/standardization-vs-normalization/
```
```{r}
library(nnet)
# load data
data(longley)
x <- longley[,1:6]
y <- longley[,7]
# fit model
fit <- nnet(Employed~., longley, size=12, maxit=500, linout=T, decay=0.01)
# summarize the fit
summary(fit)
# make predictions
predictions <- predict(fit, x, type="raw")
# summarize accuracy
mse <- mean((y - predictions)^2)
print(mse)
```
##
```{r libs, echo=FALSE}
standardize <- function(){
# standardize them in range of 0 to 1
(x-xmin)/(xmax-xmin)
}
# https://jsbin.com
# http://css3generator.com/
# http://enjoycss.com/
# http://www.css3maker.com/
#
```