Answer the question
In order to leave comments, you need to log in
How to implement a neural network in R language?
It is necessary to implement a neural network in the R language to predict the size of the pension depending on the average salary. For implementation, I use the neuralnet package. As an environment - the program Rgui for windows.
First, we have two data series - the average salary in the city for the last 10 years and the average pension for the last 10 years. This data is used to train the neural network:
#средняя зарплата за каждый год
traininginput <- c(0.225, 690, 2313, 2931, 4061, 4937, 5809, 7096, 8803, 10095, 12229, 13572)
#средняя пенсия за каждый год
trainingoutput <- c(0.118, 274, 949, 1270, 1668, 2001, 2434, 3028, 3393, 4519, 5594, 7610)
trainingdata <- cbind(traininginput,trainingoutput)
colnames(trainingdata) <- c("Input","Output")
net.pension <- neuralnet(Output~Input,trainingdata, hidden=10, threshold=0.01)
print(net.pension)
#Отправляем на вход среднюю зарплату на будущий год
testdata <- c(15851)
net.results <- compute(net.pension, testdata)
ls(net.results)
#Lets see the results
print(net.results)
Answer the question
In order to leave comments, you need to log in
First of all, your data must be normalized for use in neural network training. Besides,
hidden=10seems redundant to me.
library(neuralnet)
# 1. creating the initial data, plotting
data <- data.frame (
input = c(0.225, 690, 2313, 2931, 4061, 4937, 5809, 7096, 8803, 10095, 12229, 13572),
output = c(0.118, 274, 949, 1270, 1668, 2001, 2434, 3028, 3393, 4519, 5594, 7610)
)
plot(data$output ~ data$input, main="Distribution of the pension relative to the salary", xlab="Salary", ylab="Pension")
# 2. normalizing the data, plotting
min.input <- min(data$input)
min.output <- min(data$output)
range.input <- diff(range(data$input))
range.output <- diff(range(data$output))
data.norm <- data.frame (
input = (data$input - min.input) / range.input,
output = (data$output - min.output) / range.output
)
plot(data.norm$output ~ data.norm$input, main="Distribution of the pension relative to the salary (normalized)", xlab="Salary", ylab="Pension")
# 3. neural network
net <- neuralnet(output ~ input, data.norm)
# 4. test the output
testdata <- seq(0, 25000, by=500)
testdata.norm <- (testdata - min.input) / range.input
result <- round(compute(net, testdata.norm)$net.result * range.output + min.output)
plot(testdata, result, main="Predicred outcome", xlab="Salary", ylab="Pension")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question