Cross Validated Asked on December 15, 2021
I have a Matlab script that performs some autoregressions and am trying to replicate it in R, but can’t get them to match.
So as a MWE, I’m trying to get the same results in R as are obtained by this example in the Matlab docs:
load iddata9 z9
sys = ar(z9,4)
% sys =
% Discrete-time AR model: A(z)y(t) = e(t)
% A(z) = 1 - 0.8369 z^-1 - 0.4744 z^-2 - 0.06621 z^-3 + 0.4857 z^-4
%
% Sample time: 0.0039062 seconds
%
% Parameterization:
% Polynomial orders: na=4
% Number of free coefficients: 4
% Use "polydata", "getpvec", "getcov" for parameters and their uncertainties.
% Status:
% Estimated using AR ('fb/now') on time domain data "z9".
% Fit to estimation data: 79.38%
% FPE: 0.5189, MSE: 0.5108
So, as I understand it, sys
is an autoregressive model of order 4 based on the z9
timeseries.
Now, in order to replicate this in R, I’ll need the z9
timeseries. I’ll also need to find the appropriate function to call (ar()
, ar.ols()
, ar.mle()
, ar.yw()
or ar.burg()
).
According to the Matlab docs:
sys = ar(y,n)
estimates the parameters of an ARidpoly
modelsys
of ordern
using a least-squares method.
From this I conclude the best R function to use is ar.ols()
(least squares method).
Unlike the Matlab function ar()
, ar.ols()
by default determines the best model order via AIC. I’ll have to disable that, thereby forcing the use of a model of order order.max
:
z9 <-c(-0.78169732436269102, 4.2163489081188299, 8.3991425701037699, 12.3905782437681, 15.3017178972351, 17.5265414794188, 20.374451682603102, 19.860011961463801, 19.707656723336999, 18.890021696201199, 16.138707749128599, 14.103243742975501, 12.124169438212601, 8.9703828015333205, 4.9555659895425901, 2.8158800013430101, -0.96000196329166199, -3.8204986387165101, -6.4934611606999297, -9.0985645508532205, -11.5794949793918, -12.0987811836858, -13.6957180649972, -13.382169159841601, -13.0994179184617, -12.5463961952129, -12.3537955952199, -10.1105787443311, -9.2056372978981695, -6.5194934153247104, -4.20078270867391, -1.45307152281272, -1.0057723596955199, 2.6409248175226701, 4.2014838167328099, 6.0713940731081397, 7.35489377956607, 8.9245144939930992, 9.45396167761516, 9.6330395785713492, 9.6140799143354592, 9.7322733062332105, 8.8465247882219398, 8.0150059636162503, 6.26125054185552, 4.4448017895641296, 2.2850369374924901, 1.32183630207826, -0.63381135755833595, -1.9061892746534601, -2.95355098729042, -4.8058487422563996, -5.7329307815986796, -6.2984402414167002, -6.4762053521273497, -5.7162178901589504, -7.0723585312711403, -5.6339447369817002, -5.8700079655668604, -4.5904346815493398, -4.1373565790684799, -3.4477711422730901, -2.2463556654140402, -0.53443652348652404, 0.72239022447067103, 1.6337083414339, 1.85958511411488, 2.3747524807928202, 3.9560404066991501, 4.5893933460558998, 3.45344440663255, 4.9094359560814, 5.93306473016822, 4.5850810034178497, 3.9713706924486698, 3.2457283472268399, 2.1706359204572001, 2.0616381858567498, 1.6275862489070201, 0.133402921806686, -0.084571065390345798, -0.604893543241802, -2.0039938785578002, -2.0660003808098, -2.5119888641047701, -3.1647572797919099, -2.3743471081968801, -4.0591515094039998, -3.2233215927878902, -3.6399634284061402, -3.24536027972671, -2.28092730456288, -1.4996475901528601, -1.4531994254749001, -0.34556301115639199, -0.91030905995151801, 0.26974374459165201, 0.35598289154861501, 0.72144001500320398, 0.20574711987099001, 2.25264158850048, 3.0799828933489799, 2.57358344692256, 3.0997237181389599, 2.43428549334811, 1.6906897752051799, 1.6846760627910899, 1.45796344105633, 2.0847720873453399, 0.91864947403734998, 0.797634845865134, -0.33585684918852399, -0.17828781217815901, -1.2962022409468099, -1.19439558880353, -0.77342565557786203, -0.54794061625841095, -1.3420077137667099, -1.6465700301780599, -1.19698292411254, -1.33855520217333, -2.8317663478342401, -2.3512251884831699, -1.6728078156675801, -1.4565781064968399, -1.55748031626882, -0.81915331568836502, -0.66419009279650398, 0.62678941981035596, 0.031468336341288899, -0.065573230231712895, 0.56939036551418898, 0.60345199613562295, 1.2043488796592601, 1.04092005345169, 0.45159159084886902, 1.47684399949219, 0.95316403177807996, 0.74278514973356702, 0.488267100247333, 1.15197219185981, 0.51781607965112197, -0.40274844453201902, 1.25282280863386, -0.208819879234134, -0.098758612257166395, -0.23643541849636099, -1.2231715657588, -0.086375027451099498, -0.66534200041952496, -0.77749030754810899, -0.19608055860179299, -0.77842842317553096, -1.6725033529209199, -0.74319491970390095, -0.050537864805743099, 0.092953309434721396, -0.246652278221832, -1.0836457267741799, 0.45522300087265699, 0.0185150432879902, 0.274645961200707, 0.937771890509697, 0.60030639182323298, -1.37981067874803, -0.28958839067592601, 0.166739210348372, -0.0493062744571066, 0.66621297012920799, 0.63726678897759004, 0.16698007845024301, 1.03333084926331, -0.024666861171374701, 0.106379277629647, -0.15203427098987099, -0.367566879869832, 0.0074371055609401401, -0.81318798017594895, -0.36053375533968501, 0.133538773332392, 0.0619371792310911, -1.3653860121519901, -0.21909027092459099, -0.66366571172415301, -0.203946297920541, -0.320958064963353, -0.52774748628708701, -0.32614325788439602, 0.044954652159373401, 0.171825909654263, -0.493990050604241, -0.70963674456829895, -0.18410204378908901, 0.602319303837483, 0.32380182307224997, 0.40073434126110502, 0.023617459242779901, -0.107964482482869, 1.3316875477089201, 0.24715986897908601, 0.27830866710721403, 0.142616547049328, 0.28924251576306198, 0.0612530389535954, -0.34124201117827802, 0.38212998328463599, 0.80708659755626699, 0.129764739937864, -0.38414129202664798, -0.0309854710489481, 0.40776273652843198, 0.75143732336881397, -0.87378986266699898, -0.13749889451585201, -0.42611814018622701, 0.056535149089628298, -0.12895288893885801, -0.497118040833812, -1.08825982546337, -0.76709724111946698, -0.43328731483816002, -0.23317522568906099, 0.527622078681103, -0.32070688572769901, 0.19866422777953099, 0.49946161437242298, 0.192878938380871, 0.14550978774536399, -0.18132283180675801, 0.55969516581404, 0.96239994682048002, 0.388864903463567, 0.301914527909246, 0.25767352466038401, 0.64389452376323497, -0.055806798761705197, -0.16697953239805899, 0.33213014305603, -0.111420445029982, 0.46283488667789502, -0.73951036432601602, 0.63237391677356003, -0.41729243573991098, 0.048874292515301503, -0.31942129194285701, 0.26000457185224701, -0.81274801748473002, -0.64064578049395005, 0.71039035775398296, -0.588278016105654, -0.44086570758841198, -0.048416373891605601, -0.46419340258159197, 0.025193472369724398, 0.55769558166949995, 0.022417392282127802, 0.86921001151531097, -0.70172306503813997, -0.103793180119208, -0.38530048124978999, 0.24654942882174899, -0.56086339723128897, -0.80522236728471197, -0.120681882510209, -0.83291971555653899, -0.37872292002083102, -0.21006698511934199, -0.78044910043911897, 0.32621836736343701, 0.185401336135949, -0.29332679258948402, -0.68846562733324901, -0.14221497212199199, -0.10005627031034101, -1.1130730907923501, 0.61806962476396199, 0.174784001559569, -0.92519769865221302, 0.93719532548475903, -0.12996204340804901, -0.78917749953992999, -0.51284446145682006, 0.085978502812762495, -0.75926678415941695, -0.26885878778081601, -0.074602243908322804, -0.22749385103026501, 0.095901815822503705, -0.35298760357994002, -0.11376296208322, 0.114543040831869, -1.08424534059406, -0.92797160335670403, -0.49851636891871398, 0.077306019202830895, 0.139573527511685, -0.32810618971977201, 0.691531366077069, 0.59841097237659102, -0.061120850612385202, 1.68488008386997, -0.16645952114800899, 0.50163483797299102, -0.0279146984039941, -0.86501464708647902, 0.931524962853986, -0.54539302427422498, 0.018767274526705301, 0.93870233184836305, 0.29702042858393501, 0.15953404778312, -0.099256737817007307, 0.0964868461787739, 0.92036156702403005, -0.25293998776208498, -0.13021245051986099, -0.24360002950330001, -0.21774649645500899, -0.093586203926916703, 0.30657941084380302, 0.045671133510417003, -0.37460096024923301, 0.83554745301599198, -0.421929691277178, -0.14970312884966999, 0.0575149453307469, -0.82244344018558802, -0.48546363450831997, -0.62809505094820495, -1.31446761533712, 1.0824850056623101, -0.35143284579506101, 0.43110037946461599, 0.016687391231638302, -0.49213518353443397, -0.018462237866529601, 0.200984555665287, -0.57571811352352298, 0.59211597506533498, 0.58924176157708597, -0.059657876305430298, 0.29490677808086602, -0.62234521330382397, 0.27263851978799802, -0.077197642026890195, 0.79194931692252601, -0.48968379045343802, 0.99613832965598104, 0.0022540696172477798, 0.21701107266858299, 0.72120085983913196, 0.55013784483122996, -0.43708006571785901, -0.45469712522518502, -0.45260956723238399, 0.54379905055927102, -0.060111751118466297, -0.21365303154301499, 0.050553271577799402, 0.52738932165902597, -0.0701046880977991, 0.16346153794384599, -1.14972191112091, -0.32692546044498499, -0.52775920481585004, -0.31553492573053898, -0.37852284388921098, -0.21019963316977, -0.24638004741835201, 0.32668535177991898, 0.71453342566382405, -0.77215088389726805, 0.87089015100648803, 0.63534750974723597, 0.040261887666539001, 0.78337481355524097, 0.11585969945071201, -0.51627667617800099, 0.55836812664638202, 1.11146194789004, -0.39097080073490198, -0.50354610641962305, 0.56743751998149605, -0.0125906293029905, -0.68144306382477005, 0.61965676336444897, -0.074611431270262901, -0.39437833243831599, -0.286916493407329, -0.24448974525921199, 0.473344867649303, -0.57313222892603499, 0.239259126705891, 0.50659384082877001, -0.42595039818404901, 0.17061089780449101, -0.82397952303709798, 0.35045268730019102, -0.55988005151850595, 0.40360337088833198, 0.21641912063362301, -0.206800750686816, -0.74169766322151598, 0.66346415188086305, 0.073571257196196005, 0.835545579538158, 0.00098833967004349505, -0.89254223955884704, 0.84175876212567402, -0.14877233517487501, 0.45942691425209298, -0.36349700858323603, 0.30400902718270501, 0.41610586988753101, 0.50570405144280495, 0.24924242742941199, -0.0201440123129695, 0.073010700682865304, 0.152404046730718, -0.36760520661131701, 0.27715382203102101, -0.53006468141538299, -0.95037746187719596, 0.085640190750285797, 0.101177954728413, 0.112548910701415, 0.88839367357943499, 0.61813708782839205, 0.93326266490306597, 0.11117275202096, 0.45492302401441898, -0.29537927238455602, -0.11567509621641001, -0.87443761328286995, 0.034032216293585597, 0.46232360490762298, -0.53048074366183695, -0.11279743642704999, -0.36369130626983298, -0.40588241712651002, 0.449556572753878, 0.0320927885086668, 0.36840994462101401, 0.19720124414620599, 0.46694597622571299, -1.1292895244082699, -0.51147229244243597, -0.086073293513090302, 0.17409768877429599, 0.57461302729196495, -0.31918611045765399, -0.30165253945724901, -0.141600294612137, 0.34869999819079001, 0.054373851456593597, 0.56402759642507405, -0.384814119856207, 0.20665124043508501, 0.19457297741791299, -0.821163556092396, 0.14887003241418101, 0.87033872863406803, -0.59859575484225802, -0.31844228271025099, -0.50847484226853001, 0.56425540248662198, -0.27870017138872799, 0.375095069355801, -0.82307636156947594, -0.91205361494302495, 0.49785270578471102, 0.295814055622521, 0.13921706554681301, 0.48276354209244798, 0.033102554089321598, 0.069360195497965002, -0.43791962942865098, 0.0044111605439022998, -0.55036115307618805, -0.44541175466913302, -0.23854102730496199, 0.069570524525787603, 0.80173474625917995, 0.18917970564960501, 0.20356450249480701, 0.272187132967107, 0.60241355226128201, 0.29079805570400402, 0.032411148500829198, -0.21598122660434901, 0.29909646389082001, -0.84134202188172003, 0.0212003523072474, 0.47186419210432001, -0.11383116300844701, 0.28845558361228602, 0.086164611526444695, 0.29237296152986197, -0.24695640938351299, 0.835338553015048, 0.19795717215602199, 0.070116486536236602, -0.59571129172924397, -0.096993128244905899, -0.72321215254079496, -0.14467808943907801, -0.61491592556478603, -0.032278340356199302, 0.075867548921199499, -0.242442929345217, 0.81634874858142104, 0.68441810863512598)
ar.ols(ts(z9), aic = FALSE, order.max = 4)
#> Call:
#> ar.ols(x = ts(z9), aic = FALSE, order.max = 4)
#>
#> Coefficients:
#> 1 2 3 4
#> 0.7923 0.4780 0.0921 -0.4698
#>
#> Intercept: -0.0245 (0.03169)
#>
#> Order selected 4 sigma^2 estimated as 0.5098
As we can see, the coefficients of the models are different. They are mostly similar in magnitude, but the differences are obviously far too large to be due to floating-point errors. Also, the signs are all switched: the man page for ar.ols
says something about coefficient signs, but I admit I didn’t understand it:
For definiteness, note that the AR coefficients have the sign in
(x[t] - m) = a[0] + a[1]*(x[t-1] - m) + ... + a[p]*(x[t-p] - m) + e[t]
I’ve tried basically every combination of every other ar.*
function, adding the demean
and intercept
arguments, but couldn’t get the results to match the Matlab output.
To be specific, running the code above with intercept = FALSE
(which sets the intercept to zero, just in case Matlab does the same) barely changes the result:
ar.ols(ts(z9), aic = FALSE, order.max = 4, intercept = FALSE)
#> Call:
#> ar.ols(x = ts(z9), aic = FALSE, order.max = 4, intercept = FALSE)
#>
#> Coefficients:
#> 1 2 3 4
#> 0.7928 0.4777 0.0916 -0.4694
#>
#> Order selected 4 sigma^2 estimated as 0.5104
So it’s not merely that Matlab assumes a zero intercept.
So, how can I get my R code to get the same output as that Matlab code?
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP