-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
193 lines (142 loc) · 6.23 KB
/
server.R
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
##install.packages("MASS")
library(MASS)
##install.packages("neuralnet")
library(neuralnet)
##install.packages("caret")
library(caret)
##install.packages("randomForest")
library(randomForest)
##devtools::install_github('rstudio/DT', force=TRUE)
lin <- function(Entry1,Entry2,Entry3) {
res <- Entry1+Entry2+Entry3
return(res)
}
sq <- function(Entry1,Entry2,Entry3) {
res <- Entry1*Entry2 + Entry1*Entry3 + Entry2*Entry3
return(res)
}
cub <- function(Entry1,Entry2,Entry3) {
res <- Entry1*Entry2*Entry3
return(res)
}
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
output$table <- renderDataTable({
react <- input$Nb_layers+input$Nb_neurons+input$N_data+input$noise
r <- input$relation
noise <- 5
n <- input$N_data
m <- 10
data <- data.frame(Entry1 = rnorm(n,m,noise),Entry2 = rnorm(n,m,noise),Entry3 = rnorm(n,m,noise))
if (input$relation == "lin"){
data$Output <- lin(data$Entry1,data$Entry2,data$Entry3) + rnorm(n,0,input$noise*200)
} else if (input$relation == "sq") {
data$Output <- lin(data$Entry1,data$Entry2,data$Entry3) + sq(data$Entry1,data$Entry2,data$Entry3) + rnorm(n,0,input$noise*200)
} else if (input$relation == "cub") {
data$Output <- lin(data$Entry1,data$Entry2,data$Entry3) + sq(data$Entry1,data$Entry2,data$Entry3) + cub(data$Entry1,data$Entry2,data$Entry3) + rnorm(n,0,input$noise*200)
}
## Scale
maxValue <- apply(data,2,max)
minValue <- apply(data,2,min)
## SCALE BEFORE NEURAL NET !!!!
##data <- as.data.frame(scale(data,center=minValue,scale=maxValue-minValue))
data <- round(data,3)
save(data,file="data.Rdata")
head(data,3)
})
output$distPlot1 <- renderPlot({
react <- input$Nb_layers+input$Nb_neurons+input$N_data+input$noise
r <- input$relation
load("data.Rdata")
## Scale
maxValue <- apply(data,2,max)
minValue <- apply(data,2,min)
data <- as.data.frame(scale(data,center=minValue,scale=maxValue-minValue))
############################################################################################
## Divide into training and test
ind <- sample(1:nrow(data),0.8*nrow(data))
trainDF <- data[ind,]
testDF <- data[-ind,]
###############################################
form=as.formula("Output ~ Entry1+Entry2+Entry3")
c <- c(input$Nb_neurons)
for (i in 1:input$Nb_layers){c <- c(c,input$Nb_neurons)}
neuralModel <- neuralnet(formula=form,hidden=c,linear.output=T,data=trainDF,lifesign = "full")
save(neuralModel,file="neuralModel.Rdata")
## Predict for test data
predictions <- compute(neuralModel,testDF[,1:3])
##str(predictions)
predictions <- predictions$net.result*(max(testDF$Output)-min(testDF$Output))+min(testDF$Output)
actualValues <- (testDF$Output)*(max(testDF$Output)-min(testDF$Output))+min(testDF$Output)
RMSE_NN <- (sum((predictions-actualValues)^2)/nrow(testDF))^0.5
RMSE_NN <- round(RMSE_NN,3)
save(RMSE_NN,file="RMSE_NN.Rdata")
##RMSE_NN
##save(RMSE_NN,file="RMSE_NN.Rdata")
##plot(neuralModel)
##p_NN <- plot(predictions,actualValues)
predictions_nn <- predictions
actualValues_nn <- actualValues
###############################################
Model_lm <- train(form=form,data=trainDF,method="lm")
predictions <- predict(Model_lm,newdata=testDF)
predictions <- predictions*(max(testDF$Output)-min(testDF$Output))+min(testDF$Output)
actualValues <- (testDF$Output)*(max(testDF$Output)-min(testDF$Output))+min(testDF$Output)
RMSE_lm <- (sum((predictions-actualValues)^2)/nrow(testDF))^0.5
RMSE_lm <- round(RMSE_lm,3)
save(RMSE_lm,file="RMSE_lm.Rdata")
##RMSE_lm
predictions_lm <- predictions
actualValues_lm <- actualValues
##p_lm <- plot(predictions,actualValues)
###############################################
Model_rf <- randomForest(formula=form,data=trainDF)
predictions <- predict(Model_rf,newdata=testDF)
predictions <- predictions*(max(testDF$Output)-min(testDF$Output))+min(testDF$Output)
actualValues <- (testDF$Output)*(max(testDF$Output)-min(testDF$Output))+min(testDF$Output)
RMSE_rf <- (sum((predictions-actualValues)^2)/nrow(testDF))^0.5
RMSE_rf <- round(RMSE_rf,3)
save(RMSE_rf,file="RMSE_rf.Rdata")
##RMSE_rf
predictions_rf <- predictions
actualValues_rf <- actualValues
main_lm <- paste("Linear Regression: RMSE =",RMSE_lm)
main_rf <- paste("Random Forests: RMSE =",RMSE_rf)
main_NN <- paste("Neural Network: RMSE =",RMSE_NN)
par(mfrow=c(1,3),ps = 20, cex = 1, cex.main = 1)
plot(predictions_lm,actualValues_lm, main=main_lm,xlab="Predictions",ylab="Actual")
plot(predictions_rf,actualValues_rf, main=main_rf,xlab="Predictions",ylab="Actual")
plot(predictions_nn,actualValues_nn, main=main_NN,xlab="Predictions",ylab="Actual")
})
output$distPlot4 <- renderPlot({
react <- input$Nb_layers+input$Nb_neurons+input$N_data+input$noise
r <- input$relation
load("neuralModel.Rdata")
par(mfrow=c(1,1),mai=c(2, 2, 2, 2))
plot(neuralModel, main="Illustration of the Neural Network")
})
output$distPlot5 <- renderPlot({
react <- input$Nb_layers+input$Nb_neurons+input$N_data+input$noise
r <- input$relation
load("RMSE_lm.Rdata")
load("RMSE_rf.Rdata")
load("RMSE_NN.Rdata")
v <- c(RMSE_lm,RMSE_rf,RMSE_NN)
col <- c("orange","orange","orange")
col[which(v==max(v))] <- "red"
col[which(v==min(v))] <- "green"
##v <- data.frame(RMSE=v,col=col)
names(v) <- c("Linear Regression","Random Forests","Neural Network")
par(mfrow=c(1,1),ps = 20, cex = 1, cex.main = 1,mai=c(2, 1, 1, 1))
barplot(v,ylab="RMSE", col=col)
})
})