Haroon Lone
Haroon Lone

Reputation: 2949

NaN produced for prediction Intervals with neural networks in R

I want to find prediction intervals of predictions computed via neural networks (package nnet). Specifically, I am using nnetpredInt package of R to compute such intervals. So, the code is

library(nnet)
library(nnetpredint)
xtrain <- tdata[,!colnames(tdata) %in% "power"]
ytrain <- tdata$power
train_model <- nnet(x = xtrain, y= ytrain,linout=FALSE, na.action = na.exclude, trace=FALSE,size=4)
yFit = train_model$fitted.values
pred_intervals <- nnetPredInt(train_model, xTrain = xtrain, yTrain = ytrain, newData = dframe)

This generates following Warnings:

Warning messages:
1: In sqrt(varEst) : NaNs produced
2: In qt(alpha/2, df = (nObs - nPara)) : NaNs produced

Why does this result in NaN? dput() of the training and testing data is:

tdata = structure(list(power = c(1578.209605, 186.636575166667, 1062.07912666667, 
1031.481235, 1584.38902166667, 276.973836666667, 401.620463333333, 
1622.50827666667, 273.825153333333, 1511.37474333333, 291.460865, 
215.138178333333, 247.509348333333, 1140.21383833333, 1680.63441666667, 
1742.44168333333, 592.162706166667, 1610.7307, 615.857495, 1664.13551, 
464.973065, 1956.2482, 1767.94469333333, 1869.02678333333, 1806.731, 
1746.3731, 549.216605, 1425.42390166667, 1900.32575, 1766.18103333333
), temperature = c(28, 28, 27, 31, 32, 32, 30.5, 33, 33, 30, 
32, 24, 30, 26, 28, 32, 34, 25, 32, 33, 35, 36, 36, 37, 35, 33, 
35, 33, 35, 32), prevday1 = c(252.675878333333, 1578.209605, 
186.636575166667, 1062.07912666667, 1031.481235, 1584.38902166667, 
276.973836666667, 401.620463333333, 1622.50827666667, 273.825153333333, 
1511.37474333333, 291.460865, 215.138178333333, 247.509348333333, 
1140.21383833333, 1680.63441666667, 1742.44168333333, 592.162706166667, 
1610.7307, 615.857495, 1664.13551, 464.973065, 1956.2482, 1767.94469333333, 
1869.02678333333, 1806.731, 1746.3731, 549.216605, 1425.42390166667, 
1900.32575), prevday2 = c(250.615931666667, 252.675878333333, 
1578.209605, 186.636575166667, 1062.07912666667, 1031.481235, 
1584.38902166667, 276.973836666667, 401.620463333333, 1622.50827666667, 
273.825153333333, 1511.37474333333, 291.460865, 215.138178333333, 
247.509348333333, 1140.21383833333, 1680.63441666667, 1742.44168333333, 
592.162706166667, 1610.7307, 615.857495, 1664.13551, 464.973065, 
1956.2482, 1767.94469333333, 1869.02678333333, 1806.731, 1746.3731, 
549.216605, 1425.42390166667), prev_instant1 = c(455.932271666667, 
367.837349666667, 1230.40137333333, 1080.74080166667, 1898.06056666667, 
326.103031666667, 302.770571666667, 1859.65283333333, 281.700161666667, 
1684.32288333333, 291.448878333333, 214.838578333333, 254.042623333333, 
1380.14074333333, 824.437228333333, 1660.46284666667, 268.004111666667, 
1715.02763333333, 1853.08503333333, 1821.31845, 1173.91945333333, 
1859.87353333333, 1887.67635, 1760.29563333333, 1876.05421666667, 
1743.10665, 366.382048333333, 1185.16379, 1713.98534666667, 1746.36006666667
), prev_instant2 = c(242.638122833333, 220.635857, 1784.77271666667, 
1195.45020333333, 590.114391666667, 310.141536666667, 1397.3184605, 
1747.44398333333, 260.10318, 1521.77355833333, 283.317726666667, 
206.678135, 231.428693833333, 235.600631666667, 232.455201666667, 
281.422625, 256.470893333333, 1613.82088333333, 1564.34841666667, 
1795.03498333333, 1551.64725666667, 1517.69289833333, 1596.66556166667, 
2767.82433333333, 2949.38005, 328.691775, 389.83789, 1805.71815333333, 
1153.97645666667, 1752.75968333333), prev_2_hour = c(313.393630708333, 
263.748829166667, 931.193606666667, 699.399163791667, 754.018962083334, 
272.22309625, 595.954508875, 1597.21487208333, 512.64361, 1236.42579666667, 
281.200373333334, 196.983981666666, 230.327737625, 525.483920416666, 
391.120302791667, 610.101280416667, 247.710625543785, 978.741044166665, 
979.658926666667, 1189.25306041667, 814.840889166667, 989.059700416665, 
1352.2367025, 1770.20417833333, 1847.11590666667, 843.191556416666, 
363.50806625, 904.924465041666, 841.746712500002, 1747.73452958333
)), .Names = c("power", "temperature", "prevday1", "prevday2", 
"prev_instant1", "prev_instant2", "prev_2_hour"), row.names = 3:32, class = "data.frame", na.action = structure(1:2, .Names = c("1", 
"2"), class = "omit"))

Testing data is:

dframe <- structure(list(temperature = 27, prevday1 = 1607.69296666667, 
                         prevday2 = 1766.18103333333, prev_instant1 = 1717.19306666667, 
                         prev_instant2 = 1577.168915, prev_2_hour = 1370.14983583333), .Names = c("temperature", 
                                                                                                  "prevday1", "prevday2", "prev_instant1", "prev_instant2", "prev_2_hour"
                         ), class = "data.frame", row.names = c(NA, -1L))

Upvotes: 1

Views: 921

Answers (1)

kostas
kostas

Reputation: 2019

The problem is that your neural network has many parameters, more than the number of training data.

If you look at the source code of the package nnetpredint, in the function calcSigmaEst (nnetGradient.R) you will see that nDegreeFree = nObs - nPara and in your case this value is negative.

If you set size=3 to create your model you will not get NaNs in the prediction intervals (irrespectively of whether you scale your data or not).

Upvotes: 2

Related Questions