7

I am trying to prepare a neural net to forecast number of claims for a product based on two parameters 'no' & 'age'. Following data set is the input to neuralnet.

structure(list(no = c(25305.4104099149, 49282.7650363303, 71596.161588407, 
93100.2399492689, 120575.89348652, 138907.168168798, 152853.150129645, 
164658.048266216, 203323.951054253, 217964.514231364, 232098.010631853, 
245528.300551639, 257729.677107825, 273017.858354583, 289943.942081732, 
307253.529762711, 322779.210756104, 338484.424561413, 354509.62945598, 
376508.167449508, 392559.686167136, 406403.704572922, 418237.95321136, 
428306.956736623, 443032.309329306, 462815.029777392, 483057.035564531, 
501119.337852308, 516468.28989971, 529231.965438745, 546230.529378035), 
age = c(63.5793740593707, 102.316649334314, 139.062062015527, 
159.908188195329, 221.139098010716, 243.371632144127, 255.656705912817, 
321.979062244126, 302.183543005839, 354.719062375634, 369.989444935937, 
415.562730056562, 445.18103403067, 487.443822982359, 522.664771025013, 
531.055799381952, 588.227179384567, 627.155320232965, 631.325866647729, 
656.228738193787, 674.252217317525, 717.171080443709, 741.672049752712, 
788.251261134812, 798.113504685438, 831.731613476353, 834.814816968948, 
868.754851062387, 891.362029551517, 902.022293484355, 940.795814337874), 
claims = c(430.964844652385, 732.996578820216, 1702.3121722574,
2251.25233558302, 2197.47809502525, 2567.04757960458, 3031.86042202782, 
3156.90611199034, 3863.87816105778, 4111.89975688297, 3775.93067659216, 
4012.49766196774, 4312.44312947351, 4180.22855748422, 5089.44484309535, 
4257.88997259059, 4880.90586497903, 4463.20376379347, 4240.41527392955, 
4784.76670484109, 5402.00394657619, 4599.18095060565, 4003.91468429224, 
4029.72081951048, 3774.73142127963, 3920.30299815048, 5640.00980484863, 
5609.58082520698, 4689.03553448074, 5021.68591677232, 6583.74468086371), 
expense = c(152020.866139235, 435514.001634924, 752077.230564814, 
1206688.79158373, 1291739.60434588, 1421308.36224772, 2050740.38970347, 
1975198.4497045, 2274222.98020964, 2579595.43870509, 2129258.22735162, 
2135819.30924201, 2670328.44657756, 2908678.20678848, 2647633.44523976, 
2416617.98013342, 2312104.28655066, 2603487.56885879, 2598480.12097434, 
2747610.29007465, 2856983.01477582, 2453661.76656217, 2557917.28443019, 
2952529.81656875, 2177766.2760928, 2077444.9802322, 3542576.76934085, 
4050503.17869956, 3737028.1474149, 3497074.2505681, 3541174.73116362)),
.Names = c("no", "age", "claims", "expense"), row.names = c(NA, -31L), 
class = "data.frame")

Neural network that I am trying is

claimnet = neuralnet(claims~no+age,data=claimdata,hidden=10,threshold=0.01,err.fct='sse')

Output/fitted that I am getting from claimnet$net.result is 3913.491497 for all 31 records. Same is the result when I try compute with this neuralnet. I think there must be some parameters that must be passed to get proper output.

Please let me know where am I going wrong.

vrajs5
  • 4,066
  • 1
  • 27
  • 44
  • possible duplicate of [Generating prediction using a back-propagation neural network model on R returns same values for all observation](http://stackoverflow.com/questions/19206052/generating-prediction-using-a-back-propagation-neural-network-model-on-r-returns) – sashkello Jan 30 '14 at 01:58

3 Answers3

7

It seems that without the skip layer connections, the neural network architecture is unable to correctly approximate the model. I can't find such an option in neuralnet but you can use nnet as an alternative.

library(nnet)
res <- nnet(claims ~ .,
    data=claimdata[,1:3],
    size=10, linout=TRUE, skip=TRUE, MaxNWts=10000, trace=FALSE, maxit=100)
predict(res, newdata=claimdata[,1:2])
gd047
  • 29,749
  • 18
  • 107
  • 146
  • 1
    Hello! Do you know if `nnet` can support time series forecasting? An example is `neuralnet(time1+time2+time3~., other options)`. I get one output from `nnet` whenever I try to apply the formula mentioned it just gives one column of prediction. –  Jan 20 '14 at 14:56
7

I may be wrong, but I'm pretty sure you need to normalize your data prior to training an ANN. You could normalize your data from -1 to 1 using the standard score approach. Also, ensure your data is normally distributed. However, I'm not sure if that's mandatory with neural networks.

Using your data this is what I did.

plot(density(claimdata$no));shapiro.test(claimdata$no)
plot(density(claimdata$age));shapiro.test(claimdata$age)
plot(density((claimdata$claims)^2));shapiro.test(claimdata$claims^2)
claimdata$claimsSQ<- claimdata$claims^2

fml<- as.formula("claimsSQ ~ no + age");
data_Train<- claimdata[complete.cases(claimdata),];
scMeans<- apply(data_Train,2,mean);
scSTDEV<- apply(data_Train,2,sd);
sc_Train<- scale(data_Train);

#Select training samples
inTrain <- sample(1:nrow(sc_Train), floor(.9*nrow(sc_Train)));
# Get the predictor data 
trainingPredictors <- sc_Train[inTrain, ];
# Get Data not used in Training set
testPredictors <- sc_Train[-inTrain,];

train.nnet<- nnet(fml,data=trainingPredictors,linout=T,
                  size = 2, rang = 0.1,decay = 5e-4, maxit = 200);

res.nnet<- predict(train.nnet,testPredictors);

results<-  cbind(claimdata$claims[-inTrain],
                 sqrt(res.nnet*scSTDEV[5]+scMeans[5]));results;

Also, have a look at, "Chapter 3 - Data Preparation in Neural Network Data Analysis in Foreign-Exchange-Rate Forecasting With Artificial Neural Networks International Series in Operations Research & Management Science Volume 107, 2007, pp 39-62".

Hope this helps,

Cheers.

JSB
  • 86
  • 1
  • 1
1

@George Dontas and @Jean B.: Here, Lean Yu et al. state at page 55: "As to univariate time series analysis with neural networks, nonstationarity is a problem for time series analysis (Moody, 1995). Therefore, data detrending and deseasonalization and data stationarity are also important issues in complex data analysis. For trending and seasonal data and nonstationary data, difference or log-difference (Weigend and Gershenfeld, 1994; Tseng et al., 2002; Moody, 1995) is a simple and effective treatment method that is widely used."

As far as I am concerned, this means if someone works with time series, stationarity should be ensured by differencing or log-differencing while architecturing an ANN.

Cenk
  • 325
  • 4
  • 16