-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathneural_nets.R
89 lines (79 loc) · 3.31 KB
/
neural_nets.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
#try neural nets
library(caret)
#use dataset called data for training and testing models
#split dataset
set.seed(156)
split <- createDataPartition(data$avg_song_popularity, p = 0.8, list = FALSE)
data.train <- data[split,]
data.test <- data[-split,]
#get packages
install.packages("neuralnet")
install.packages("forecast")
library(forecast)
#normalize
norm.object <- preProcess(data.train, method="range")
data.train.norm <- predict(norm.object, data.train)
data.test.norm <- predict(norm.object, data.test)
#test different types od NN models
try_hidden <- list(c(3,3),c(2,2))
#creat function that runs NN for each choice of try_hidden
NN <- function(h){
neuralnet(avg_song_popularity ~ avg_song_duration_ms+
avg_acousticness+
avg_danceability+
avg_energy+
avg_instrumentalness+
avg_key+
avg_liveness+
avg_loudness+
avg_audiomode+
avg_speechiness+
avg_tempo+
avg_time_signature+
avg_audio_valence,
data = data.train.norm, linear.output = F,
hidden = h)
}
# Now we can use lapply to run the neuralnet function over all the different
# values we want to try for "hidden"
NNmodels <- lapply(try_hidden,NN)
#plot models
lapply(NNmodels,plot)
# Now we'll write another custom function to help implement our prediction multiple times using different NNmodels
NNpredict <- function(myNNmodel){
neuralnet::compute(myNNmodel, data.test.norm[, c("avg_song_duration_ms",
"avg_acousticness",
"avg_danceability",
"avg_energy",
"avg_instrumentalness",
"avg_key",
"avg_liveness",
"avg_loudness",
"avg_audiomode",
"avg_speechiness",
"avg_tempo",
"avg_time_signature",
"avg_audio_valence")])
}
NNpredictions <- lapply(NNmodels,NNpredict)
NNpredictions_vector <- lapply(NNpredictions,function(p){return(as.vector(p$net.result))})
# check the out-of-sample accuracy
results <- lapply(NNpredictions_vector,accuracy,data.test.norm$avg_song_popularity)
results
# add names to the list of results to make it easier to follow what we did
model_names <- lapply(try_hidden,as.character)
names(results) <- model_names
results
#change results back to regular range for first model
NNpredictions_vector_original <- min(data.test$avg_song_popularity) + NNpredictions_vector[[1]]*max(data.test$avg_song_popularity)
# Calculate R2, OSR2, out of sample RMSE
mean_train <- mean(data.train$avg_song_popularity)
SSETest <- sum((NNpredictions_vector_original - data.test$avg_song_popularity)^2)
SSTTest <- sum((data.test$avg_song_popularity- mean_train)^2)
OSR2 <- 1 - SSETest/SSTTest
OSR2
#
RMSE <- sqrt(mean((NNpredictions_vector_original - data.test$avg_song_popularity)^2))
RMSE
#plot variable importnace
olden(NNmodels[[2]])