Paul Bernal
2019-Jan-30 18:50 UTC
[R] Setting xreg parameter to generate forecasts of mlp models
Dear friends, Hope you are all doing great. So far, I?ve been able to generate forecasts using R?s mlp function (which allows fitting of multiple layer perceptron models to time series data). I basically fitted an mlp model and then tried to use the forecast function with the xreg parameter to do the forecasts. Here the details of what I am working with for better reference: R version: R version 3.5.1 (2018-07-02) -- "Feather Spray" Packages used: library(forecast) library(glmnet) library(neuralnet) library(nnfor) library(tseries) library(lubridate) This is my training data: (my exogenous variable is the one called FuelPrice) structure(list(TransitDate = structure(c(134L, 105L, 219L, 20L, 248L, 190L, 162L, 48L, 332L, 304L, 276L, 76L, 135L, 106L, 220L, 21L, 249L, 191L, 163L, 49L, 333L, 305L, 277L, 77L, 136L, 107L, 221L, 22L, 250L, 192L, 164L, 50L, 334L, 306L, 278L, 78L, 137L, 108L, 222L, 23L, 251L, 193L, 165L, 51L, 335L, 307L, 279L, 79L, 138L, 109L, 223L, 24L, 252L, 194L, 166L, 52L, 336L, 308L, 280L, 80L, 139L, 110L, 224L, 25L, 253L, 195L, 167L, 53L, 337L, 309L, 281L, 81L, 140L, 111L, 225L, 26L, 254L, 196L, 168L, 54L, 338L, 310L, 282L, 82L, 141L, 112L, 226L, 27L, 255L, 197L, 169L, 55L, 339L, 311L, 283L, 83L, 142L, 113L, 227L, 28L, 256L, 198L, 170L, 56L, 340L, 312L, 284L, 84L, 143L, 114L, 228L, 29L, 257L, 199L, 171L, 57L, 341L, 313L, 285L, 85L, 115L, 86L, 200L, 1L, 229L, 172L, 144L, 30L, 314L, 286L, 258L, 58L, 116L, 87L, 201L, 2L, 230L, 173L, 145L, 31L, 315L, 287L, 259L, 59L, 117L, 88L, 202L, 3L, 231L, 174L, 146L, 32L, 316L, 288L, 260L, 60L, 118L, 89L, 203L, 4L, 232L, 175L, 147L, 33L, 317L, 289L, 261L, 61L, 119L, 90L, 204L, 5L, 233L, 176L, 148L, 34L, 318L, 290L, 262L, 62L, 120L, 91L, 205L, 6L, 234L, 177L, 149L, 35L, 319L, 291L, 263L, 63L, 121L, 92L, 206L, 7L, 235L, 178L, 150L, 36L, 320L, 292L, 264L, 64L, 122L, 93L, 207L, 8L, 236L, 179L, 151L, 37L, 321L, 293L, 265L, 65L, 123L, 94L, 208L, 9L, 237L, 180L, 152L, 38L, 322L, 294L, 266L, 66L, 124L, 95L, 209L, 10L, 238L, 181L, 153L, 39L, 323L, 295L, 267L, 67L, 125L, 96L, 210L, 11L, 239L, 182L, 154L, 40L, 324L, 296L, 268L, 68L, 126L, 97L, 211L, 12L, 240L, 183L, 155L, 41L, 325L, 297L, 269L, 69L, 127L, 98L, 212L, 13L, 241L, 184L, 156L, 42L, 326L, 298L, 270L, 70L, 128L, 99L, 213L, 14L, 242L, 185L, 157L, 43L, 327L, 299L, 271L, 71L, 129L, 100L, 214L, 15L, 243L, 186L, 158L, 44L, 328L, 300L, 272L, 72L, 130L, 101L, 215L, 16L, 244L, 187L, 159L, 45L, 329L, 301L, 273L, 73L, 131L, 102L, 216L, 17L, 245L, 188L, 160L, 46L, 330L, 302L, 274L, 74L, 132L, 103L, 217L, 18L, 246L, 189L, 161L, 47L, 331L, 303L, 275L, 75L, 133L, 104L, 218L, 19L, 247L), .Label = c("1-Apr-00", "1-Apr-01", "1-Apr-02", "1-Apr-03", "1-Apr-04", "1-Apr-05", "1-Apr-06", "1-Apr-07", "1-Apr-08", "1-Apr-09", "1-Apr-10", "1-Apr-11", "1-Apr-12", "1-Apr-13", "1-Apr-14", "1-Apr-15", "1-Apr-16", "1-Apr-17", "1-Apr-18", "1-Apr-90", "1-Apr-91", "1-Apr-92", "1-Apr-93", "1-Apr-94", "1-Apr-95", "1-Apr-96", "1-Apr-97", "1-Apr-98", "1-Apr-99", "1-Aug-00", "1-Aug-01", "1-Aug-02", "1-Aug-03", "1-Aug-04", "1-Aug-05", "1-Aug-06", "1-Aug-07", "1-Aug-08", "1-Aug-09", "1-Aug-10", "1-Aug-11", "1-Aug-12", "1-Aug-13", "1-Aug-14", "1-Aug-15", "1-Aug-16", "1-Aug-17", "1-Aug-90", "1-Aug-91", "1-Aug-92", "1-Aug-93", "1-Aug-94", "1-Aug-95", "1-Aug-96", "1-Aug-97", "1-Aug-98", "1-Aug-99", "1-Dec-00", "1-Dec-01", "1-Dec-02", "1-Dec-03", "1-Dec-04", "1-Dec-05", "1-Dec-06", "1-Dec-07", "1-Dec-08", "1-Dec-09", "1-Dec-10", "1-Dec-11", "1-Dec-12", "1-Dec-13", "1-Dec-14", "1-Dec-15", "1-Dec-16", "1-Dec-17", "1-Dec-90", "1-Dec-91", "1-Dec-92", "1-Dec-93", "1-Dec-94", "1-Dec-95", "1-Dec-96", "1-Dec-97", "1-Dec-98", "1-Dec-99", "1-Feb-00", "1-Feb-01", "1-Feb-02", "1-Feb-03", "1-Feb-04", "1-Feb-05", "1-Feb-06", "1-Feb-07", "1-Feb-08", "1-Feb-09", "1-Feb-10", "1-Feb-11", "1-Feb-12", "1-Feb-13", "1-Feb-14", "1-Feb-15", "1-Feb-16", "1-Feb-17", "1-Feb-18", "1-Feb-90", "1-Feb-91", "1-Feb-92", "1-Feb-93", "1-Feb-94", "1-Feb-95", "1-Feb-96", "1-Feb-97", "1-Feb-98", "1-Feb-99", "1-Jan-00", "1-Jan-01", "1-Jan-02", "1-Jan-03", "1-Jan-04", "1-Jan-05", "1-Jan-06", "1-Jan-07", "1-Jan-08", "1-Jan-09", "1-Jan-10", "1-Jan-11", "1-Jan-12", "1-Jan-13", "1-Jan-14", "1-Jan-15", "1-Jan-16", "1-Jan-17", "1-Jan-18", "1-Jan-90", "1-Jan-91", "1-Jan-92", "1-Jan-93", "1-Jan-94", "1-Jan-95", "1-Jan-96", "1-Jan-97", "1-Jan-98", "1-Jan-99", "1-Jul-00", "1-Jul-01", "1-Jul-02", "1-Jul-03", "1-Jul-04", "1-Jul-05", "1-Jul-06", "1-Jul-07", "1-Jul-08", "1-Jul-09", "1-Jul-10", "1-Jul-11", "1-Jul-12", "1-Jul-13", "1-Jul-14", "1-Jul-15", "1-Jul-16", "1-Jul-17", "1-Jul-90", "1-Jul-91", "1-Jul-92", "1-Jul-93", "1-Jul-94", "1-Jul-95", "1-Jul-96", "1-Jul-97", "1-Jul-98", "1-Jul-99", "1-Jun-00", "1-Jun-01", "1-Jun-02", "1-Jun-03", "1-Jun-04", "1-Jun-05", "1-Jun-06", "1-Jun-07", "1-Jun-08", "1-Jun-09", "1-Jun-10", "1-Jun-11", "1-Jun-12", "1-Jun-13", "1-Jun-14", "1-Jun-15", "1-Jun-16", "1-Jun-17", "1-Jun-90", "1-Jun-91", "1-Jun-92", "1-Jun-93", "1-Jun-94", "1-Jun-95", "1-Jun-96", "1-Jun-97", "1-Jun-98", "1-Jun-99", "1-Mar-00", "1-Mar-01", "1-Mar-02", "1-Mar-03", "1-Mar-04", "1-Mar-05", "1-Mar-06", "1-Mar-07", "1-Mar-08", "1-Mar-09", "1-Mar-10", "1-Mar-11", "1-Mar-12", "1-Mar-13", "1-Mar-14", "1-Mar-15", "1-Mar-16", "1-Mar-17", "1-Mar-18", "1-Mar-90", "1-Mar-91", "1-Mar-92", "1-Mar-93", "1-Mar-94", "1-Mar-95", "1-Mar-96", "1-Mar-97", "1-Mar-98", "1-Mar-99", "1-May-00", "1-May-01", "1-May-02", "1-May-03", "1-May-04", "1-May-05", "1-May-06", "1-May-07", "1-May-08", "1-May-09", "1-May-10", "1-May-11", "1-May-12", "1-May-13", "1-May-14", "1-May-15", "1-May-16", "1-May-17", "1-May-18", "1-May-90", "1-May-91", "1-May-92", "1-May-93", "1-May-94", "1-May-95", "1-May-96", "1-May-97", "1-May-98", "1-May-99", "1-Nov-00", "1-Nov-01", "1-Nov-02", "1-Nov-03", "1-Nov-04", "1-Nov-05", "1-Nov-06", "1-Nov-07", "1-Nov-08", "1-Nov-09", "1-Nov-10", "1-Nov-11", "1-Nov-12", "1-Nov-13", "1-Nov-14", "1-Nov-15", "1-Nov-16", "1-Nov-17", "1-Nov-90", "1-Nov-91", "1-Nov-92", "1-Nov-93", "1-Nov-94", "1-Nov-95", "1-Nov-96", "1-Nov-97", "1-Nov-98", "1-Nov-99", "1-Oct-00", "1-Oct-01", "1-Oct-02", "1-Oct-03", "1-Oct-04", "1-Oct-05", "1-Oct-06", "1-Oct-07", "1-Oct-08", "1-Oct-09", "1-Oct-10", "1-Oct-11", "1-Oct-12", "1-Oct-13", "1-Oct-14", "1-Oct-15", "1-Oct-16", "1-Oct-17", "1-Oct-90", "1-Oct-91", "1-Oct-92", "1-Oct-93", "1-Oct-94", "1-Oct-95", "1-Oct-96", "1-Oct-97", "1-Oct-98", "1-Oct-99", "1-Sep-00", "1-Sep-01", "1-Sep-02", "1-Sep-03", "1-Sep-04", "1-Sep-05", "1-Sep-06", "1-Sep-07", "1-Sep-08", "1-Sep-09", "1-Sep-10", "1-Sep-11", "1-Sep-12", "1-Sep-13", "1-Sep-14", "1-Sep-15", "1-Sep-16", "1-Sep-17", "1-Sep-90", "1-Sep-91", "1-Sep-92", "1-Sep-93", "1-Sep-94", "1-Sep-95", "1-Sep-96", "1-Sep-97", "1-Sep-98", "1-Sep-99"), class = "factor"), Transits = c(98L, 80L, 95L, 89L, 92L, 96L, 86L, 98L, 84L, 90L, 95L, 90L, 99L, 85L, 91L, 90L, 88L, 97L, 93L, 97L, 87L, 92L, 87L, 86L, 85L, 82L, 90L, 89L, 101L, 94L, 92L, 109L, 101L, 103L, 96L, 89L, 102L, 87L, 101L, 100L, 99L, 101L, 98L, 101L, 90L, 106L, 90L, 99L, 105L, 91L, 96L, 91L, 96L, 93L, 101L, 105L, 98L, 110L, 100L, 101L, 106L, 99L, 111L, 114L, 112L, 113L, 120L, 105L, 111L, 114L, 111L, 118L, 115L, 108L, 120L, 119L, 120L, 118L, 117L, 121L, 111L, 114L, 107L, 121L, 109L, 106L, 116L, 105L, 119L, 120L, 123L, 126L, 117L, 127L, 128L, 132L, 138L, 120L, 132L, 134L, 136L, 144L, 152L, 155L, 146L, 155L, 138L, 141L, 146L, 123L, 133L, 123L, 137L, 133L, 143L, 132L, 126L, 134L, 129L, 138L, 134L, 132L, 139L, 130L, 152L, 150L, 153L, 161L, 152L, 154L, 154L, 138L, 149L, 137L, 144L, 146L, 152L, 140L, 151L, 168L, 148L, 157L, 152L, 153L, 166L, 157L, 156L, 166L, 168L, 179L, 188L, 190L, 185L, 184L, 185L, 202L, 191L, 175L, 197L, 187L, 195L, 204L, 218L, 220L, 212L, 220L, 211L, 221L, 204L, 196L, 209L, 205L, 217L, 211L, 212L, 224L, 206L, 225L, 206L, 219L, 232L, 220L, 242L, 241L, 261L, 252L, 261L, 269L, 251L, 264L, 261L, 266L, 274L, 236L, 270L, 263L, 276L, 276L, 300L, 303L, 301L, 318L, 294L, 308L, 308L, 269L, 303L, 302L, 318L, 282L, 311L, 305L, 304L, 309L, 298L, 295L, 295L, 281L, 280L, 287L, 313L, 276L, 296L, 307L, 307L, 309L, 287L, 286L, 290L, 261L, 285L, 279L, 286L, 284L, 267L, 271L, 259L, 268L, 243L, 242L, 237L, 208L, 250L, 237L, 267L, 257L, 276L, 277L, 269L, 282L, 264L, 270L, 270L, 251L, 272L, 271L, 288L, 266L, 283L, 266L, 270L, 282L, 272L, 264L, 269L, 253L, 269L, 283L, 288L, 275L, 301L, 292L, 283L, 287L, 261L, 265L, 269L, 234L, 251L, 261L, 262L, 249L, 256L, 255L, 253L, 253L, 233L, 234L, 235L, 217L, 244L, 232L, 261L, 236L, 252L, 242L, 252L, 251L, 230L, 240L, 254L, 226L, 267L, 245L, 263L, 261L, 286L, 281L, 265L, 274L, 250L, 260L, 265L, 242L, 251L, 249L, 251L, 247L, 248L, 234L, 206L, 219L, 194L, 218L, 209L, 192L, 207L, 200L, 208L, 208L, 209L, 213L, 216L, 219L, 195L, 217L, 217L, 197L, 210L, 211L, 229L), FuelPrice = c(106.2962963, 95.80555556, 91.23703704, 85.83101852, 82.15277778, 66.95, 72.78888889, 123.9444444, 137.8055556, 149.6527778, 142.9777778, 143.9282407, 158.20625, 102.7791667, 74.96833333, 74.06666667, 77.825, 76.76458333, 78.446875, 77.33, 76.28472222, 80.45138889, 87.44166667, 77.05, 66.845, 65.91666667, 70.875, 78.13541667, 85.585, 89.8875, 93.805, 96.71875, 98.10625, 103.645, 97.3375, 81.59375, 80.665, 82, 84.4875, 86.335, 85.23125, 77.4875, 69.375, 72.16875, 70.6875, 72.155, 65.4875, 60.725, 69.25, 76.24375, 75.16875, 78.985, 88.5, 92.7375, 101.68, 101.5375, 84.57, 92.075, 98.9875, 97.549, 104.05, 105.0375, 107.355, 109.13125, 112.55, 102.68, 89.325, 87.4, 89.385, 93.7625, 96.1125, 109.46, 115.7125, 108.4125, 111.06, 116.4125, 108.49, 95.25, 99.1125, 103.19, 117.725, 129.375, 124.64, 124.175, 117.26, 103.775, 97.1375, 97.45, 98.63, 98.575, 98.8625, 103.38, 105.525, 111.8, 112.5, 94.64375, 80.35, 70.2875, 68.5, 80.2625, 78.37, 70.2375, 69.49, 65.55, 70.8875, 78.28, 73.275, 63.275, 66.136, 61.2, 66.925, 79.97, 84.5375, 86.2625, 97.52, 117.825, 125.7, 136.52, 137.5, 137.91, 137.5375, 142.3375, 161.04, 144.2, 146.95, 160.35, 150.7375, 145.45, 162.55, 173.2, 161.4, 141.93, 126.5625, 133.5375, 134.4, 128.8875, 136.5875, 131.66, 128.4, 134.62, 142.4875, 125.6625, 108.15, 111.4270833, 112.875, 114.4791667, 130.1083333, 151.5729167, 153.5333333, 149.6458333, 153.34375, 160.725, 172.475, 168.15625, 147.65, 156.6770833, 186.6333333, 199.7708333, 180.59375, 158.1145833, 155.7083333, 162.8854167, 181.8645833, 172.2916667, 159.8020833, 166.1583333, 164.8020833, 158.1666667, 165.8, 166.4375, 165.0104167, 172.1583333, 188.1354167, 181.3645833, 180.5416667, 187.7291667, 185.2291667, 210.125, 185.4166667, 176.1333333, 188.4270833, 197.4479167, 224.75, 262.0166667, 259.2083333, 257.1458333, 263.55, 274.7604167, 316.7583333, 312.5, 291.90625, 289.1166667, 311.3645833, 322.6145833, 327.375, 343.8104167, 347.53125, 328.9583333, 340.3229167, 336.21875, 299.65, 288.1770833, 281.3854167, 283.075, 269.0848115, 293.6611771, 306.037149, 332.2265119, 348.3577214, 352.4784017, 380.0569654, 378.3900648, 391.8031857, 425.3555616, 497.6723369, 478.389556, 472.9277721, 468.7605749, 513.636037, 526.8660164, 574.4350103, 634.5420791, 723.7430698, 682.4256674, 598.350616, 436.4950719, 251.2828542, 231.6342402, 259.3991786, 265.2687372, 259.7176591, 289.5685318, 345.4262834, 403.2597536, 407.2184805, 445.5154004, 436.5097536, 444.1123203, 479.7510267, 467.6647844, 481.161807, 466.013347, 469.0002567, 484.7790554, 462.4086242, 446.6830082, 445.9609856, 454.1185832, 447.362423, 474.3616667, 493.6333333, 507.7695833, 535.071875, 600.5708333, 642.528125, 673.0433333, 653.39375, 662.5119792, 670.9591667, 660.3786458, 660.23125, 660.9192708, 681.3197917, 662.8329167, 707.9286458, 725.3364583, 739.5675, 725.7286458, 681.8526042, 599.7833333, 611.5536458, 661.4755365, 667.1883047, 648.13125, 623.67897, 616.3776824, 634.486588, 658.5445279, 634.0532189, 614.5563725, 606.2872146, 606.1289954, 609.5603248, 615.9174014, 618.5638489, 615.0463415, 608.7503722, 615.1243797, 614.0424242, 616.316838, 610.4884319, 603.2643979, 603.8573298, 609.691, 600.1080163, 592.0074792, 576.779661, 511.1634006, 454.6536765, 365.6265015, 292.1762048, 342.7007576, 333.6017802, 336.7719298, 370.0886452, 360.3370968, 316.5678457, 255.5741051, 249.9473684, 246.452381, 229.0865385, 184.9615385, 155.6425339, 161.2596154, 176.7019231, 191.7991453, 228.8461538, 245.875, 249.4307692, 241.2403846, 258.3384615, 285.6689977, 276.8942308, 323.1230769, 337.7115385, 326.7980769, 309.2574519, 314.3772894, 309.9615385, 299.1490385, 308.4807692, 316.7596154, 333.7692308, 340.9807692, 373.3173077, 371.4692308, 395.0576923, 379.6442308, 372.9192308, 393.9711538, 440.5865385)), class = "data.frame", row.names = c(NA, -341L )) #Now, since the mlp() function needs a ts object as an input, I did: TransitSeries <- ts(CarTransitData$Transits, start=c(1990,1), end=c(2018,5), frequency=12) TransitModel <- mlp(TransitSeries) # I then worked with the regressor (FuelPrice): FuelPriceSeries <- ts(CarTransitData$FuelPrice, start=c(1990,1), end=c(2018,5), frequency=12) FuelModel <- mlp(FuelPriceSeries) # Then generated the forecasts for FuelPrice: FuelForecast <- forecast(FuelModel, h=12) # Finally, I try to generate the forecasts: Forecast1 <- forecast(TransitModel, h=48, xreg=FuelForecast) and I get the error: Error in if (length(xreg.lags) != x.n) { : argument is of length zero I need help on how to incorporate the xreg argument to generate forecasts for mlp models. Any help will be greatly appreciated, Cheers, Paul [[alternative HTML version deleted]]