-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFire Forests_ANN_R.R
65 lines (44 loc) · 1.77 KB
/
Fire Forests_ANN_R.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
##### Neural Networks
# Load the Fire Forests data
fire <- read.csv(file.choose())
# custom normalization function
norm <- function(x) {
return((x - min(x))/ (max(x)- min(x)))
}
str(fire)
sum(is.na(fire))
View(fire)
summary(fire)
fire <- fire[ , -c(1, 2, 12:30)]
fire$area <- ifelse(fire$area > 50, 1, 0)
# apply normalization to entire data frame
fire_norm <- as.data.frame(lapply(fire[ , c(1:8)], norm))
fire_norm <- cbind(fire$area, fire_norm)
#######________________################___________________##############
# create training and test data
fire_train <- fire_norm[1:415, ]
fire_test <- fire_norm[416:517, ]
attach(fire_train)
## Training a model on the data ----
# train the neuralnet model
library(neuralnet)
# simple ANN with only a single hidden neuron
fire_model <- neuralnet(formula = `fire$area` ~ FFMC + DMC + DC + ISI + temp + RH + wind + rain, data = fire_train)
# visualize the network topology
plot(fire_model)
## Evaluating model performance
# obtain model results
# results_model <- NULL
results_model <- compute(fire_model, fire_test[2:9])
# obtain predicted strength values
str(results_model)
predicted_strength <- results_model$net.result
# examine the correlation between predicted and actual values
cor(predicted_strength, fire_test$`fire$area`)
## Improving model performance ----
# a more complex neural network topology with 5 hidden neurons
fire_model2 <- neuralnet(formula = `fire$area` ~ FFMC + DMC + DC + ISI + temp + RH + wind + rain, data = fire_train, hidden = c(12, 10, 8, 6, 4, 2, 1))
# evaluate the results as we did before
model_results2 <- compute(fire_model2, fire_test[2:9])
predicted_strength2 <- model_results2$net.result
cor(predicted_strength2, fire_test$`fire$area`)