Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forecast with external regressors #244

Closed
larry77 opened this issue Mar 5, 2024 · 2 comments
Closed

Forecast with external regressors #244

larry77 opened this issue Mar 5, 2024 · 2 comments

Comments

@larry77
Copy link

larry77 commented Mar 5, 2024

First of all thanks for the great work.
It is not really an issue, but I see no possibility to open a discussion for this repo.
I deal with tabular data, in which one column is the variable I want to forecast, one is time, and several others are regressors which can be used to forecast my target variable.
See the reprex

library(tidyverse)


df <- structure(list(close = c(98.7105966925097, 96.2313346349953, 
98.6915690132989, 99.3951329324309, 98.5859745284845, 97.8071009923145, 
91.565816396672, 92.6475989269169, 94.735932953575, 106.983251925824, 
108.331852833761, 104.615419129126, 100.47561734456, 99.1315346651836, 
101.292054275768, 100.01154733467, 101.627999220702, 100.151479501293, 
99.47760478488, 99.8658179766848, 102.965624040528, 102.896699482559, 
112.113906703026, 118.968287428173, 112.426801775758, 111.361039489823, 
109.055895087784, 116.047494171261, 110.285743012413, 102.590279428283, 
104.047233895186, 102.772634452626, 99.3637851897572, 94.8988263559279, 
98.9528066613749, 96.5929146730194, 106.842683676146, 109.765531325619, 
102.936178255801, 105.992990037411, 99.3925649165122, 101.864474841856, 
103.500622756726, 101.173924064775, 96.8108390013624, 96.8582450247056, 
97.2289229372089, 96.5329475478178, 91.5005984071088, 89.4957050864705, 
89.9167852138967, 88.7731705433919, 91.3745411637118, 91.5933893767575, 
91.1491292394452, 93.6712899141147, 93.0073013569139, 85.9607458940066, 
83.8172894709377, 85.8137179385217, 85.6388367460616, 83.2797820104441, 
84.0156182402559, 84.505546045446, 84.0764774803294, 85.2275037567696, 
87.6027909062803, 88.4192473141954, 87.245588696847, 90.9265375176967, 
89.6315058239105, 91.0971770411269, 92.4200794025985, 94.7119257533165, 
93.7882580768199, 94.5551728167723, 96.5369483987356, 93.8815811566226, 
91.756159067423, 91.8551842969357, 94.8487091619709, 101.333185608745, 
100.340869525256, 102.750139860581, 104.874300780072, 104.497152342614, 
105.769481998912, 110.529852059369, 116.479797090716, 112.579313133667, 
114.273868837842), timestamp = structure(c(19696, 19697, 19698, 
19699, 19700, 19701, 19702, 19703, 19704, 19705, 19706, 19707, 
19708, 19709, 19710, 19711, 19712, 19713, 19714, 19715, 19716, 
19717, 19718, 19719, 19720, 19721, 19722, 19723, 19724, 19725, 
19726, 19727, 19728, 19729, 19730, 19731, 19732, 19733, 19734, 
19735, 19736, 19737, 19738, 19739, 19740, 19741, 19742, 19743, 
19744, 19745, 19746, 19747, 19748, 19749, 19750, 19751, 19752, 
19753, 19754, 19755, 19756, 19757, 19758, 19759, 19760, 19761, 
19762, 19763, 19764, 19765, 19766, 19767, 19768, 19769, 19770, 
19771, 19772, 19773, 19774, 19775, 19776, 19777, 19778, 19779, 
19780, 19781, 19782, 19783, 19784, 19785, 19786), class = "Date"), 
    high = c(0.352306070435904, 0.343960286860067, 0.330338595619463, 
    0.355199211944317, 0.440711428595164, 0.335627155803961, 
    0.300230447818673, 0.192545203456709, 0.231956718992288, 
    0.502508802060596, 0.740313121891548, 0.54224143242119, 0.455508365822704, 
    0.35905507583232, 0.505329582760521, 0.463287140384535, 0.383706922927321, 
    0.39541770943297, 0.381831587686781, 0.408271400787283, 0.451965765849236, 
    0.538263007436815, 0.631055972282448, 1, 0.804399694507166, 
    0.724983817219173, 0.749153185607335, 0.702785653127371, 
    0.781352347438019, 0.631929453514064, 0.462800122880898, 
    0.4915971828621, 0.411625182158456, 0.34886210335265, 0.331908220992048, 
    0.322541343747161, 0.558194561975725, 0.737959474287956, 
    0.64483930459665, 0.510297627578978, 0.477241750857009, 0.411741292430683, 
    0.578244590868104, 0.431546578674618, 0.374720907601149, 
    0.288374310582173, 0.304494062853583, 0.307449574451309, 
    0.272360357883913, 0.190787354296709, 0.127859454375021, 
    0.113105725374509, 0.151458082354723, 0.162230095857774, 
    0.19189961316316, 0.202264501831234, 0.235805508804232, 0.187241529166515, 
    0.0237082690238707, 0.0332835042265203, 0.0412381502352202, 
    0.0170491358105322, 0.0196845935762706, 0.00938026307996611, 
    0, 0.0386374998358764, 0.111082382913813, 0.0970959013401806, 
    0.108982640796354, 0.137244675586748, 0.143604183963041, 
    0.167840258991487, 0.191308586707997, 0.226490163521036, 
    0.221533844980917, 0.235325800671845, 0.293893043378147, 
    0.271491837674815, 0.209188639820317, 0.199647796013205, 
    0.362529953709619, 0.426214727255178, 0.376352320951623, 
    0.42386799432496, 0.468383563702033, 0.530646474411194, 0.665576136856392, 
    0.578257709894461, 0.7553181544524, 0.759096486168192, 0.684781579536105
    ), low = c(0.45858396806302, 0.469284848124347, 0.443550087220062, 
    0.483564197228645, 0.539104347011376, 0.472529287223802, 
    0.216925559416082, 0.29210020159954, 0.233783562743155, 0.405555965826375, 
    0.740256634199207, 0.726895997096313, 0.60966827905517, 0.403246852678162, 
    0.564542350366974, 0.590775861280328, 0.566317254163334, 
    0.551287206482594, 0.464820887767494, 0.556031626548357, 
    0.556677819332186, 0.564521246499625, 0.585518166046488, 
    1, 0.910207962439877, 0.97042715655205, 0.893006085085746, 
    0.869388757969728, 0.918707830044984, 0.520380549039816, 
    0.654013145357308, 0.600772715625083, 0.524452512778796, 
    0.409691919018865, 0.241137962641686, 0.361388930561609, 
    0.48100712982143, 0.818767037337344, 0.620498754392236, 0.635221043669664, 
    0.579274395310652, 0.58057383589616, 0.66016110324248, 0.60524654222526, 
    0.466122448927825, 0.340205777030698, 0.475063764895461, 
    0.486561452014005, 0.300815458528376, 0.0873136359363124, 
    0.221609793250299, 0.172832173522275, 0.192586533638999, 
    0.286447149319718, 0.280267461703868, 0.287571612230239, 
    0.34955692929012, 0.114298193373348, 0.0409676225695692, 
    0.0221472347699477, 0.108392747304312, 0.0462507071691563, 
    0, 0.0283024084183465, 0.0238334502605286, 0.0770268148160704, 
    0.114499922914884, 0.151850409209552, 0.166581476139377, 
    0.116356773610944, 0.217816024143794, 0.245011145518355, 
    0.282494063359999, 0.299258201773672, 0.319285333671058, 
    0.361322007937186, 0.416190755688001, 0.28903713737305, 0.21580436737515, 
    0.277105053806835, 0.258277529018039, 0.400878244030552, 
    0.526683749666599, 0.540620506967557, 0.639224149627102, 
    0.569263590212304, 0.709925066618501, 0.789229636936498, 
    0.940556939928069, 0.896262248987389, 0.942891261607773), 
    volume = c(0.159178797708374, 0.136023797700209, 0.115901921738471, 
    0.183756019721396, 0.268328302405094, 0.120714661896529, 
    0.194068543034191, 0.108110528429093, 0.126387373481916, 
    0.275837773468005, 0.716494784785395, 0.24185452060395, 0.144240130669764, 
    0.181660565830457, 0.231759288013477, 0.166326907640566, 
    0.191302384810517, 0.173457689054994, 0.194074622332925, 
    0.263795887689011, 0.283834675630238, 0.433613681435888, 
    0.417431015437204, 1, 0.358425292675841, 0.241929980819696, 
    0.26602599142848, 0.146518094634308, 0.299995687664107, 0.393466765612925, 
    0.210436650801066, 0.24400551108784, 0.165356455267928, 0.0779146091840286, 
    0.195383264726355, 0.143095283953021, 0.333093050844963, 
    0.397065838487603, 0.243307861440161, 0.123478479100369, 
    0.0810471412350938, 0.0983113003728025, 0.245330786986372, 
    0.0994140845324473, 0.0970620865461924, 0.115908527402224, 
    0.0581015797440973, 0.0348237093309171, 0.0747365020426526, 
    0.114491336090179, 0.0482984633087547, 0.0230381150837757, 
    0.0413654393733396, 0, 0.0267544925942717, 0.0286475057535545, 
    0.0547949838515532, 0.219980802439823, 0.187705568819311, 
    0.233928543718589, 0.026478423437715, 0.00266972416926254, 
    0.0405908679631698, 0.0466360132670807, 0.124457536141733, 
    0.0911118884539047, 0.178309013089969, 0.0456188712564735, 
    0.0421981168987437, 0.0861555916595821, 0.11041459374258, 
    0.111068146673966, 0.139587500420286, 0.125386855890125, 
    0.061080814748532, 0.0526366086431032, 0.115364034404825, 
    0.161101220903838, 0.128911898470234, 0.0604366238013439, 
    0.363221983806348, 0.34755265359959, 0.0853452430644858, 
    0.152186925162602, 0.164486130348524, 0.292604470334872, 
    0.350756670925037, 0.1459173662821, 0.246560485257055, 0.280861699731552, 
    0.323490119492833), ad = c(0.895040161811205, 0.808116873897327, 
    0.863043928431609, 0.908877666783657, 0.788088594493494, 
    0.781263190968751, 0.740952491477405, 0.779618753550039, 
    0.86050730920099, 1, 0.886245890517143, 0.794097764641266, 
    0.702224845117892, 0.759108902890472, 0.703460559640697, 
    0.607603275078501, 0.70124494876511, 0.683723240418769, 0.708341926457601, 
    0.646992568814872, 0.701422372047143, 0.664307339358374, 
    0.833182336848473, 0.75633883870587, 0.677868567390032, 0.550689106029081, 
    0.415275526128068, 0.516197406291169, 0.383399882114882, 
    0.320492553611869, 0.354494998884351, 0.331577585505293, 
    0.290296860359611, 0.229886438247118, 0.332265952281195, 
    0.351684587816436, 0.445739809342017, 0.366879042331775, 
    0.28208767894155, 0.331359022955926, 0.253035294752158, 0.279832760974238, 
    0.198014024140746, 0.157983600030951, 0.100574321762122, 
    0.164562421479303, 0.160509277118534, 0.096505026369051, 
    0.0349126171604172, 0.0473692443272689, 0.0729518879542451, 
    0.085959148647158, 0.144087664632392, 0.164546639862129, 
    0.137820224958695, 0.192508221835802, 0.152147001454404, 
    0.0503567222109793, 0, 0.0926036886344493, 0.0749027410413921, 
    0.0272594370444708, 0.0356201198661257, 0.0627480257727215, 
    0.0926893908912134, 0.0847076951285152, 0.0884737382785661, 
    0.118598418923307, 0.0721877717286707, 0.151381031756681, 
    0.14008804118258, 0.158775593229123, 0.19515614526279, 0.278494208330467, 
    0.309674163375252, 0.33780136665974, 0.357034000934539, 0.361962225166731, 
    0.378715434717008, 0.374518833775061, 0.361396646354054, 
    0.44530118915098, 0.473637494798486, 0.54141401057307, 0.610775918104219, 
    0.635074985282618, 0.541012871042244, 0.641736196312681, 
    0.712533533646443, 0.676406111324519, 0.771552251406591), 
    obv = c(0.215769365452672, 0.138564635683114, 0.210500483925132, 
    0.30020382085011, 0.188355364910318, 0.115159308238239, 0.022755653326587, 
    0.0926513375024093, 0.167332784768295, 0.281147583878087, 
    0.510347727342747, 0.405431389469949, 0.326075225853482, 
    0.236920580229393, 0.339193497348912, 0.254053945970507, 
    0.345733286708138, 0.258726551447731, 0.166321304681281, 
    0.276982954966464, 0.392891730311884, 0.237763558206278, 
    0.388654324708701, 0.69208986224128, 0.556649651306719, 0.451713554295282, 
    0.340467954858453, 0.42042059975112, 0.300280088859098, 0.155664323428174, 
    0.252353941577929, 0.14687437023485, 0.0619889302817057, 
    0, 0.0927479127159874, 0.013691525409104, 0.142498528719898, 
    0.288056705714323, 0.182759812832653, 0.256679570686397, 
    0.193870391821027, 0.261200163564623, 0.367026756314351, 
    0.299408222275496, 0.232405555204819, 0.304343133129486, 
    0.361144052858937, 0.310438406833274, 0.249281658900848, 
    0.177715170289087, 0.231949159420381, 0.184329552685385, 
    0.236748140419078, 0.278335253005663, 0.229742518673039, 
    0.278830935519823, 0.222895842716768, 0.123707103492959, 
    0.0329695833574453, 0.135810516666653, 0.0872900706078492, 
    0.0450038949525023, 0.0972196623824343, 0.151018341588765, 
    0.0768422191908081, 0.142286836262903, 0.230563883080772, 
    0.284096225253678, 0.23145960222147, 0.295606420784407, 0.225107420735276, 
    0.295777552590109, 0.373915432303895, 0.448334895614239, 
    0.390753867891333, 0.446123793545614, 0.517918796836518, 
    0.434147583299386, 0.358805093141572, 0.416217440479593, 
    0.552913657431629, 0.685506884949068, 0.621572254889414, 
    0.7030092741272, 0.787666820871008, 0.66946168859508, 0.802893883002686, 
    0.882689228122507, 0.988837815427572, 0.873707509138625, 
    1), rrt = c(0.244483227870048, 0.189098728634769, 0.20583184667526, 
    0.192602018802919, 0.345850453082302, 0.152168402428559, 
    0.687402731453068, 0.0984467023391477, 0.397824796688999, 
    0.877866702587609, 0.808107643413715, 0.233352706657427, 
    0.225641901386414, 0.406439594424975, 0.495879988934145, 
    0.296184525815072, 0.088669827076353, 0.164622379398119, 
    0.330122682698301, 0.196550425459554, 0.340634712671581, 
    0.601442503132466, 0.840761705603838, 0.965782744177498, 
    0.616586563885219, 0.255359352908757, 0.493777937718145, 
    0.408343084830308, 0.531719857974706, 1, 0.145164629522486, 
    0.364857365278548, 0.284335768223526, 0.355059020924508, 
    0.73336671230915, 0.386544974906583, 0.867462994353026, 0.624526018900245, 
    0.799965480876465, 0.343878516142751, 0.369379096320409, 
    0.149260354957915, 0.501290952456054, 0.156704693655608, 
    0.302663225914925, 0.320806704236885, 0.0369680939919164, 
    0.0189833605925878, 0.366173685538038, 0.648414799663179, 
    0.0419736961339596, 0.117574632852137, 0.210209730919573, 
    0, 0.127177959521737, 0.14632143465726, 0.107918375373831, 
    0.559711861664629, 0.134284829538902, 0.226936168971189, 
    0.0137044165333979, 0.0922502659291392, 0.236364089793484, 
    0.112679672021194, 0.0873555728522064, 0.091562950930339, 
    0.270913963285573, 0.11319752443867, 0.118775290620553, 0.365995942282873, 
    0.112219842944522, 0.130857697151719, 0.119117632398378, 
    0.204606700130828, 0.134220308091376, 0.0759281587995698, 
    0.146528029846838, 0.393937625471787, 0.362780303311843, 
    0.164218509220288, 0.793385031014824, 0.639670699703692, 
    0.159478327007823, 0.286097574030625, 0.198196951007282, 
    0.565794146955835, 0.654806703494826, 0.20562446459716, 0.408747047691774, 
    0.515768690822144, 0.194481730601363), sigmat = c(0.244063227439469, 
    0.188761670107159, 0.205468794418444, 0.192259454444581, 
    0.345304646530299, 0.151891327880868, 0.68678356955531, 0.0982626001251242, 
    0.397229794444664, 0.877529598389856, 0.807635021753072, 
    0.232948644508051, 0.225249103338412, 0.405837556048739, 
    0.495225486630751, 0.295695783030243, 0.0885032887361465, 
    0.164324691344712, 0.329593932956677, 0.196201695792759, 
    0.340094455157919, 0.600780144693087, 0.84034762645598, 0.965674822737418, 
    0.615928406842587, 0.254924154997333, 0.493124213362974, 
    0.407739536377961, 0.531055695901891, 1, 0.144899311488554, 
    0.364292261310308, 0.283861977692574, 0.354503682180614, 
    0.732790898493631, 0.385959678640644, 0.867103162746831, 
    0.623870631137925, 0.799479567310211, 0.343334794883511, 
    0.368809619227597, 0.148988147762006, 0.500634553661014, 
    0.156420066954384, 0.302166518560339, 0.320288508595598, 
    0.0368972343197473, 0.0189467511565862, 0.365607299758702, 
    0.647770144355299, 0.0418933853331156, 0.117356715146612, 
    0.209840007193026, 0, 0.126943352051329, 0.146054166839796, 
    0.10771743564636, 0.559045052534974, 0.134038000493114, 0.226541467701333, 
    0.0136779426813554, 0.0920772742369777, 0.235955677683182, 
    0.112470336680133, 0.0871914093615505, 0.0913911961675312, 
    0.270457698312235, 0.112987278780645, 0.118555275480307, 
    0.365429729168183, 0.112011316388394, 0.130616747706965, 
    0.118897019779015, 0.204245524624366, 0.133973589496128, 
    0.0757847782655815, 0.146260414145135, 0.393345905368038, 
    0.362217236508247, 0.163921483657974, 0.792888749381881, 
    0.639021693730726, 0.159189107261666, 0.285621529042608, 
    0.197845663006123, 0.565127350618088, 0.65416554101219, 0.205261729721857, 
    0.408143180858078, 0.515107977570175, 0.194136225809308)), row.names = c(NA, 
-91L), class = c("tbl_df", "tbl", "data.frame"))

df|>glimpse()
#> Rows: 91
#> Columns: 9
#> $ close     <dbl> 98.71060, 96.23133, 98.69157, 99.39513, 98.58597, 97.80710, …
#> $ timestamp <date> 2023-12-05, 2023-12-06, 2023-12-07, 2023-12-08, 2023-12-09,…
#> $ high      <dbl> 0.3523061, 0.3439603, 0.3303386, 0.3551992, 0.4407114, 0.335…
#> $ low       <dbl> 0.4585840, 0.4692848, 0.4435501, 0.4835642, 0.5391043, 0.472…
#> $ volume    <dbl> 0.1591788, 0.1360238, 0.1159019, 0.1837560, 0.2683283, 0.120…
#> $ ad        <dbl> 0.8950402, 0.8081169, 0.8630439, 0.9088777, 0.7880886, 0.781…
#> $ obv       <dbl> 0.21576937, 0.13856464, 0.21050048, 0.30020382, 0.18835536, …
#> $ rrt       <dbl> 0.24448323, 0.18909873, 0.20583185, 0.19260202, 0.34585045, …
#> $ sigmat    <dbl> 0.24406323, 0.18876167, 0.20546879, 0.19225945, 0.34530465, …

## In this example the "close" variable is what I want to predict, timestamp is
## obviously the time variable and the other variables are auxiliary variables
## which may be useful to predict "close"

Created on 2024-03-05 with reprex v2.1.0

I cannot find examples/tutorial of modeltime handling this case. I would like to try a couple of models (arima, lasso for instance, not super complicated stuff) to forecast my data by taking the extra variables into account.
Is there any tutorial where the use of external regressors is discussed?
Thanks a lot.

@joranE
Copy link
Contributor

joranE commented Mar 5, 2024

In basically any time series scenario, if you want to forecast using variables that are not derivable from the date index (day of week, month, etc) that means you have to supply future values of those variables when you pass data frames to generate predictions. That's the only difference, but of course now you have multiple forecasting problems.

@larry77
Copy link
Author

larry77 commented Mar 5, 2024

I see. Then I was looking for the impossible -- to incorporate extra info and still be able to forecast as if I had a vanilla arima model. Thanks for the answer and I can close this.

@larry77 larry77 closed this as completed Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants