#---------
##====[ANN :: MTCARS Example with Standardization and Log Transformation ]
=========
#install.packages("neuralnet")
require(neuralnet)
require(ggplot2)
# Scale standardization and Log transformation
trainingdata <- mtcars
devider1 <- max(log(trainingdata$mpg))
trainingdata$mpg <- log(trainingdata$mpg)/devider1
trainingdata$wt <- trainingdata$wt/max(trainingdata$wt)
trainingdata$hp <- trainingdata$hp/max(trainingdata$hp)
#-------
#Train the neural network
#Going to have 2 hidden layers
#Threshold is a numeric value specifying the threshold for the partial
#derivatives of the error function as stopping criteria.
# net.sqrt <- neuralnet(mpg~wt+hp ,trainingdata, hidden=c(10,10), rep=100,
threshold=0.01)
# net.sqrt <- neuralnet(mpg~wt+hp ,trainingdata, hidden=c(5,5), rep=1000, threshold=0.01)
net.sqrt <- neuralnet(mpg~wt+hp ,trainingdata, hidden=c(5,2), rep=1000, threshold=0.01)
print(net.sqrt)
# Plot the neural network (takes a while )
# plot(net.sqrt)
#Test the neural network on some training data
# testdata
# Works fine
net.results <- compute(net.sqrt , trainingdata[,c("wt","hp")])
# detransform prediction score to the original scale
mtcars$nnpred <- exp(as.numeric(net.results$net.result) * devider1 )
plot(mtcars$nnpred, mtcars$mpg)
mean( abs(100*(mtcars$nnpred-mtcars$mpg)/mtcars$mpg))
# 9.049620312
#---- comparison
lm1 <- lm(mpg~wt+hp ,data=trainingdata)
mtcars$lmpred <- predict(lm1, mtcars)
mean( abs(100*(mtcars$lmpred-mtcars$mpg)/mtcars$mpg))
# 9.742982991
'R 데이터 분석' 카테고리의 다른 글
R 연관성 규칙 생성 연습 [Association Rule Discovery in R] (0) | 2016.03.03 |
---|---|
[R 데이터 처리] 도로명 주소에서 동이름 추출 (0) | 2015.12.29 |
R d3netwrok sample using randomForest proximity of mtcars (0) | 2015.08.05 |
자신만의 컬러챠트 생성 예제 (0) | 2015.08.05 |
0720 클러스터링 군집화 연습 (0) | 2015.07.20 |