dalmacyali commited on
Commit
2e3c91b
·
verified ·
1 Parent(s): 9db6460

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +98 -108
app.R CHANGED
@@ -218,47 +218,43 @@ ui <- fluidPage(
218
  )
219
  )
220
  ),
221
- tabPanel("Multilayer Perceptron",
222
- sidebarLayout(
223
- sidebarPanel(
224
- fileInput("mlp_fileInput", "Choose CSV/XLSX/XSX File", accept = c(".csv", ".xlsx", ".xsx")),
225
- uiOutput("mlp_preprocessUI"),
226
- numericInput("mlp_hiddenLayers", "Number of Hidden Layers", value = 1),
227
- numericInput("mlp_neurons", "Neurons in Hidden Layer", value = 5),
228
- numericInput("mlp_epochs", "Number of Epochs", value = 100),
229
- actionButton("mlp_trainButton", "Train MLP Model"),
230
- HTML("<div>
231
- <h2>Çok Katmanlı Perseptron Modeli</h2>
232
- <p>Çok katmanlı perseptron modeli, derin öğrenme ve yapay sinir ağlarının temel yapı taşlarından biridir. Bu model, karmaşık veri yapılarını öğrenebilme ve tahmin etme yeteneğiyle ön plana çıkar.</p>
233
-
234
- <h3>Kullanım Adımları:</h3>
235
- <ol>
236
- <li><strong>Veri Dosyası Yükleme:</strong> <code>fileInput</code> aracı ile CSV, XLSX veya XSX formatlarında veri dosyanızı yükleyin.</li>
237
- <li><strong>Ön İşleme ve Değişken Seçimi:</strong> Dinamik olarak oluşturulan UI üzerinden hedef değişken, öngörücü değişken(ler) ve koşullu değişken seçin.</li>
238
- <li><strong>Model Parametreleri:</strong> Gizli katman sayısı, bir gizli katmandaki nöron sayısı ve dönem sayısı gibi model parametrelerini ayarlayın.</li>
239
- <li><strong>Modeli Eğit:</strong> <code>Train MLP Model</code> butonu ile çok katmanlı perceptron modelinizi eğitin.</li>
240
- </ol>
241
-
242
- <h3>Model Çıktısı ve Değerlendirme:</h3>
243
- <p>Model eğitimi tamamlandıktan sonra, eğitim sürecini ve modelin performansını gösteren grafikler ve özet metinler kullanıcıya sunulur. Model, verilen parametrelere göre en iyi yapılandırmayı öğrenmeye çalışır.</p>
 
 
 
 
 
 
 
 
 
 
 
 
 
244
 
245
- <h3>Uygulama Alanları:</h3>
246
- <p>Çok katmanlı perseptron modelleri, görüntü ve ses işleme, metin sınıflandırma ve finansal tahminler gibi geniş bir uygulama alanına sahiptir.</p>
247
-
248
- <h3>Sonuçların Yorumlanması:</h3>
249
- <p>Modelin başarımı, hata oranları, adım sayısı ve ağırlık dağılımları üzerinden değerlendirilir. Eğitim sürecinde elde edilen ağırlık değerleri, modelin öğrenme kapasitesini ve veri üzerindeki genelleme yeteneğini gösterir. Ayrıca, modelin gerçek dünya verileri üzerindeki performansını test etmek için ayrı bir test veri seti kullanılması önerilir.</p>
250
- </div>")
251
-
252
- ),
253
- mainPanel(
254
- tabsetPanel(
255
- tabPanel("Multilayer Output", plotOutput("mlp_trainingPlot", width = "100%", height = "700px")),
256
- tabPanel("Evaluation", verbatimTextOutput("mlp_evaluation")),
257
- tabPanel("GW Plot", plotOutput("mlp_gwplot", width = "100%", height = "700px"))
258
- )
259
- )
260
- )
261
- ),
262
 
263
  )
264
  )
@@ -361,83 +357,77 @@ server <- function(input, output, session) {
361
 
362
  ##Multiple Perceptron Model
363
  # Reactive expression for data input
364
- dataMLP <- reactive({
365
- req(input$mlp_fileInput)
366
- inFile <- input$mlp_fileInput
367
- if (grepl("\\.csv$", inFile$name)) {
368
- read.csv(inFile$datapath, stringsAsFactors = FALSE)
369
- } else if (grepl("\\.(xlsx|xls)$", inFile$name)) {
370
- readxl::read_xlsx(inFile$datapath)
371
- } else {
372
- stop("Unsupported file type")
373
- }
374
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
375
 
376
- output$mlp_preprocessUI <- renderUI({
377
- req(dataMLP())
378
- varNames <- names(dataMLP())
379
- tagList(
380
- selectInput("mlp_targetVariable", "Select Target Variable", choices = varNames),
381
- selectInput("mlp_variables", "Select Predictor Variables", choices = varNames, multiple = TRUE),
382
- selectInput("mlp_covariate", "Select Covariate Variable", choices = varNames),
383
- tags$p(HTML("Please select Covariate Variable from Predictor Variables"))
384
- )
385
  })
386
 
387
- observeEvent(input$mlp_trainButton, {
388
- req(dataMLP(), input$mlp_targetVariable, input$mlp_variables)
389
- data <- dataMLP()
390
- data <- na.omit(data)
391
-
392
- # Define the formula for the neural network
393
- formula <- as.formula(paste(input$mlp_targetVariable, "~", paste(input$mlp_variables, collapse = "+")))
394
-
395
- # Train the neural network model
396
- nn <- neuralnet(formula, data, hidden = c(input$mlp_neurons), linear.output = FALSE, threshold = 0.01, stepmax = input$mlp_epochs)
397
 
398
- # Plot the neural network
399
- output$mlp_trainingPlot <- renderPlot({
400
- plot(nn,rep = "best")
401
- })
 
402
 
 
403
 
404
- # Print the result matrix of the neural network
405
- output$mlp_evaluation <- renderPrint({
406
- print(nn$result.matrix)
407
- # Neural Network Model Performance Summary
408
- cat("\nNeural Network Model Performance Summary:\n")
409
-
410
- # If the error is not within a reasonable range, you could give more context:
411
- if (nn$result.matrix["error", ] > 200) {
412
- cat("The model error of", nn$result.matrix["error", ], "is above the expected threshold. This may indicate that the model does not fit the data well. Consider collecting more data, feature engineering, or adjusting the model's complexity.\n")
413
- } else {
414
- cat("The model error of", nn$result.matrix["error", ], "is within the acceptable range, suggesting the model has learned the patterns from the data effectively.\n")
415
- }
416
-
417
- # Comment on the number of steps
418
- cat("The model took", nn$result.matrix["steps", ], "steps to converge, which indicates ", ifelse(nn$result.matrix["steps", ] < 3000, "an efficient training process.", "that the maximum set steps were reached without sufficient convergence."), "\n")
419
-
420
- # Comment on the weights
421
- cat("The model's weights have been optimized through training. Each weight reflects the importance of the corresponding input feature for predicting the output. For instance, the weight for 'Price' to the first hidden neuron is", nn$result.matrix["Price.to.1layhid1", ], ".\n")
422
-
423
- # Mention the threshold
424
- cat("The threshold for stopping the training was set to", nn$result.matrix["reached.threshold", ], ", and the model reached an error close to this threshold, which is a good sign of model convergence.\n")
425
-
426
- # Add a note on the usage of the model
427
- cat("This trained model can now be used to make predictions on new data. It's important to validate the model's performance on a separate test set to ensure its predictive accuracy.\n")
428
- })
429
 
430
- output$mlp_gwplot <- renderPlot({
431
- req(input$mlp_covariate) # Make sure input$mlp_variables is available
432
- selected_var <- input$mlp_covariate # This should be a vector of selected variable names
433
- if (length(selected_var) == 1) { # gwplot may only accept a single variable
434
- gwplot(nn, selected.covariate = selected_var, min = -2.5, max = 5)
435
- } else {
436
- cat("Please select a single predictor variable to view its weight distribution.")
437
- }
438
- })
439
 
 
 
 
 
 
 
 
 
 
 
 
440
  })
 
 
441
 
442
  }
443
 
 
218
  )
219
  )
220
  ),
221
+ tabPanel("Multilayer Perceptron",
222
+ sidebarLayout(
223
+ sidebarPanel(
224
+ fileInput("mlp_fileInput", "Choose CSV/XLSX/XSX File", accept = c(".csv", ".xlsx", ".xls")),
225
+ uiOutput("mlp_preprocessUI"),
226
+ numericInput("mlp_hiddenLayers", "Number of Hidden Layers", value = 1),
227
+ numericInput("mlp_neurons", "Neurons in Hidden Layer", value = 5),
228
+ numericInput("mlp_epochs", "Number of Epochs", value = 100),
229
+ actionButton("mlp_trainButton", "Train MLP Model"),
230
+ HTML("<div>
231
+ <h2>Çok Katmanlı Perseptron Modeli</h2>
232
+ <p>Çok katmanlı perseptron modeli, derin öğrenme ve yapay sinir ağlarının temel yapı taşlarından biridir. Bu model, karmaşık veri yapılarını öğrenebilme ve tahmin etme yeteneğiyle ön plana çıkar.</p>
233
+ <h3>Kullanım Adımları:</h3>
234
+ <ol>
235
+ <li><strong>Veri Dosyası Yükleme:</strong> <code>fileInput</code> aracı ile CSV, XLSX veya XSX formatlarında veri dosyanızı yükleyin.</li>
236
+ <li><strong>Ön İşleme ve Değişken Seçimi:</strong> Dinamik olarak oluşturulan UI üzerinden hedef değişken, öngörücü değişken(ler) ve koşullu değişken seçin.</li>
237
+ <li><strong>Model Parametreleri:</strong> Gizli katman sayısı, bir gizli katmandaki nöron sayısı ve dönem sayısı gibi model parametrelerini ayarlayın.</li>
238
+ <li><strong>Modeli Eğit:</strong> <code>Train MLP Model</code> butonu ile çok katmanlı perceptron modelinizi eğitin.</li>
239
+ </ol>
240
+ <h3>Model Çıktısı ve Değerlendirme:</h3>
241
+ <p>Model eğitimi tamamlandıktan sonra, eğitim sürecini ve modelin performansını gösteren grafikler ve özet metinler kullanıcıya sunulur. Model, verilen parametrelere göre en iyi yapılandırmayı öğrenmeye çalışır.</p>
242
+ <h3>Uygulama Alanları:</h3>
243
+ <p>Çok katmanlı perseptron modelleri, görüntü ve ses işleme, metin sınıflandırma ve finansal tahminler gibi geniş bir uygulama alanına sahiptir.</p>
244
+ <h3>Sonuçların Yorumlanması:</h3>
245
+ <p>Modelin başarımı, hata oranları, adım sayısı ve ağırlık dağılımları üzerinden değerlendirilir. Eğitim sürecinde elde edilen ağırlık değerleri, modelin öğrenme kapasitesini ve veri üzerindeki genelleme yeteneğini gösterir. Ayrıca, modelin gerçek dünya verileri üzerindeki performansını test etmek için ayrı bir test veri seti kullanılması önerilir.</p>
246
+ </div>")
247
+ ),
248
+ mainPanel(
249
+ tabsetPanel(
250
+ tabPanel("Multilayer Output", plotOutput("mlp_trainingPlot", width = "100%", height = "700px")),
251
+ tabPanel("Evaluation", verbatimTextOutput("mlp_evaluation")),
252
+ tabPanel("GW Plot", plotOutput("mlp_gwplot", width = "100%", height = "700px"))
253
+ )
254
+ )
255
+ )
256
+ ),
257
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
 
259
  )
260
  )
 
357
 
358
  ##Multiple Perceptron Model
359
  # Reactive expression for data input
360
+ # Reactive expression for data input
361
+ dataMLP <- reactive({
362
+ req(input$mlp_fileInput)
363
+ inFile <- input$mlp_fileInput
364
+ if (grepl("\\.csv$", inFile$name)) {
365
+ read.csv(inFile$datapath, stringsAsFactors = FALSE)
366
+ } else if (grepl("\\.(xlsx|xls)$", inFile$name)) {
367
+ readxl::read_xlsx(inFile$datapath)
368
+ } else {
369
+ stop("Unsupported file type")
370
+ }
371
+ })
372
+
373
+ output$mlp_preprocessUI <- renderUI({
374
+ req(dataMLP())
375
+ varNames <- names(dataMLP())
376
+ tagList(
377
+ selectInput("mlp_targetVariable", "Select Target Variable", choices = varNames),
378
+ selectInput("mlp_variables", "Select Predictor Variables", choices = varNames, multiple = TRUE),
379
+ selectInput("mlp_covariate", "Select Covariate Variable", choices = varNames),
380
+ tags$p(HTML("Please select Covariate Variable from Predictor Variables"))
381
+ )
382
+ })
383
+
384
+ observeEvent(input$mlp_trainButton, {
385
+ req(dataMLP(), input$mlp_targetVariable, input$mlp_variables)
386
+ data <- dataMLP()
387
+ data <- na.omit(data)
388
 
389
+ # Define the formula for the neural network
390
+ formula <- as.formula(paste(input$mlp_targetVariable, "~", paste(input$mlp_variables, collapse = "+")))
391
+
392
+ # Train the neural network model
393
+ nn <- neuralnet(formula, data, hidden = rep(input$mlp_neurons, input$mlp_hiddenLayers), linear.output = FALSE, threshold = 0.01, stepmax = input$mlp_epochs)
394
+
395
+ # Plot the neural network
396
+ output$mlp_trainingPlot <- renderPlot({
397
+ plot(nn, rep = "best")
398
  })
399
 
400
+ # Print the result matrix of the neural network
401
+ output$mlp_evaluation <- renderPrint({
402
+ print(nn$result.matrix)
403
+ cat("\nNeural Network Model Performance Summary:\n")
 
 
 
 
 
 
404
 
405
+ if (nn$result.matrix["error", ] > 200) {
406
+ cat("The model error of", nn$result.matrix["error", ], "is above the expected threshold. This may indicate that the model does not fit the data well. Consider collecting more data, feature engineering, or adjusting the model's complexity.\n")
407
+ } else {
408
+ cat("The model error of", nn$result.matrix["error", ], "is within the acceptable range, suggesting the model has learned the patterns from the data effectively.\n")
409
+ }
410
 
411
+ cat("The model took", nn$result.matrix["steps", ], "steps to converge, which indicates ", ifelse(nn$result.matrix["steps", ] < 3000, "an efficient training process.", "that the maximum set steps were reached without sufficient convergence."), "\n")
412
 
413
+ cat("The model's weights have been optimized through training. Each weight reflects the importance of the corresponding input feature for predicting the output. For instance, the weight for 'Price' to the first hidden neuron is", nn$result.matrix["Price.to.1layhid1", ], ".\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
414
 
415
+ cat("The threshold for stopping the training was set to", nn$result.matrix["reached.threshold", ], ", and the model reached an error close to this threshold, which is a good sign of model convergence.\n")
 
 
 
 
 
 
 
 
416
 
417
+ cat("This trained model can now be used to make predictions on new data. It's important to validate the model's performance on a separate test set to ensure its predictive accuracy.\n")
418
+ })
419
+
420
+ output$mlp_gwplot <- renderPlot({
421
+ req(input$mlp_covariate)
422
+ selected_var <- input$mlp_covariate
423
+ if (selected_var %in% colnames(nn$covariate)) {
424
+ gwplot(nn, selected.covariate = selected_var, min = -2.5, max = 5)
425
+ } else {
426
+ cat("Please select a valid predictor variable to view its weight distribution.")
427
+ }
428
  })
429
+ })
430
+
431
 
432
  }
433