Organize Results Kalman Filter in a Matrix

Asked

Viewed 94 times

1

simple question:

With the code below I estimate Kalman Filter with an AR component(1). The goal is to exit the vector k_fk. When I have only one vector it is simple. But I need to run the same code for several vectors. These vectors I have gathered, as columns, into a flame matrix Matrix_FK.

I want to run the Kalman Filter below for all columns of this Matrix and then put each k_fk referring to each column in another empty Matrix that I create. I can do but separately, that is, I repeat all the code below 5 times! It gets very big. I would like some suggestion to make the code more elegant.

I’m not getting to work using for and the functions optim.

You could help me?

My Matrix: Matrix_FK

                     a             b            c            d            e
Jan 1932   1.850842046  -1.569796034  -3.86703562  -3.70885804  -3.52838460
Feb 1932   0.695430163  -1.263834133   2.04679783  -0.57774389  -1.81938085
Mar 1932  -1.155459612  -1.375228333  -2.56902935  -4.18820808  -1.87963414
Apr 1932   0.876312739   1.095184325   3.72558782   1.38908077  -1.73895471
May 1932   1.778220516  -0.228588115  -3.40387447  -0.16437899  -1.57999233
Jun 1932  -1.532919489  -0.401615700  -4.04059931  -3.06292113   3.24023693
Jul 1932  -0.853453175  -0.647391750   1.52062149   1.31220367  -1.53168757
Aug 1932  -0.271756880   0.227539602   1.79810027   3.37019494   1.68859250
Sep 1932   3.865040601  -2.072427260   3.54705484   1.18479762  -1.16082455
Oct 1932   0.599674245  -1.176546510  -2.40650477  -3.21858089  -4.50304382
Nov 1932  -2.231206701  -0.720217144  -2.67286548  -2.85785781  -0.67487973
Dec 1932  -0.478563913  -2.234382028  -1.53654814  -2.24945799  -2.64905648
Jan 1933   0.842738461   2.624749795   0.19604646   1.19005790  -1.96990357
Feb 1933   1.846665775  -0.006328424  -4.21347614   0.05377252  -1.26049459
Mar 1933  -0.172363344  -0.659327632   0.82200066   2.30309262  -4.95140006
Apr 1933  -2.304450878  -1.314443000   0.64447634  -1.07627386   1.53871842
May 1933   2.436129902  -1.133146914  -2.87587532  -1.91479118   2.54500858
Jun 1933  -1.602963131  -2.553387393   2.03615676   0.26499586  -2.94983895
Jul 1933   2.230501876  -3.699483272   2.31281068  -2.22591862  -4.37350797
Aug 1933   2.402754968   3.404874741   4.91719243   3.74235401  -1.38626991
Sep 1933  -1.697403507  -2.820929307   2.29945531   0.24403812  -4.18449366
Oct 1933  -1.280780318   1.139607917  -3.06407384  -3.80907509   2.84230473
Nov 1933   0.607107992   0.169127732   2.17921243   0.47200233   8.61801081
Dec 1933  -1.592986278   2.609462937  -3.85479317   3.72595201  -1.09970343
Jan 1934  -2.440549544   0.088069852  -2.36859426  -5.20434411  -0.59305754
Feb 1934   0.821528217  -0.207777845  -2.47410902  -0.28985175  -3.88084999
Mar 1934  -0.183719895   3.787154996  -2.86968421  -1.29114339   0.26474367
Apr 1934  -0.466043977  -0.631774120  -0.78434871  -2.61279780  -5.12932944
May 1934  -1.611272207  -0.573902305  -2.34886869   0.68221244  -1.21287941
Jun 1934   2.491069752  -1.690086428   3.32391309   2.93634244   3.36063422
Jul 1934  -0.744049851   0.528023150  -1.82971938  -0.89697608   3.79939741
Aug 1934   3.643293001   1.380693211   1.82255441   1.50904917   0.23195852
Sep 1934  -0.179018730  -0.546052710  -1.34589399  -0.73097960  -1.01117490
Oct 1934  -2.984234513  -0.208621509  -4.66427450  -3.32608587  -1.36399967
Nov 1934   0.596162874   1.549294986  -0.88791861  -0.57522023   0.53990345
Dec 1934   0.554429979   0.096578374  -3.21640301  -3.09745990  -2.27168715
Jan 1935  -2.548662238  -0.721410530  -1.67786737  -0.54327758   0.46687319
Feb 1935   0.350352303  -0.106131517  -0.39048897   0.58163853   1.09495528
Mar 1935  -1.873287765  -1.672132957  -3.52854267  -3.62054936   3.36251750
Apr 1935  -4.598704756  -1.147643299   1.19402303  -0.87757740   3.71228073
May 1935   1.036169454   1.447926643   3.38466130   2.34878083   5.31374095
Jun 1935  -1.547048883  -0.433702626  -2.33058507  -4.11691596  -0.59688167
Jul 1935  -3.375154845   0.100309069  -4.56191956  -3.60744993  -0.48958503
Aug 1935   1.521572320  -1.853439024   0.04222668  -2.42350457   0.89827002
Sep 1935  -1.852501902  -1.565919020  -4.24024560  -4.17008320   1.62955000
Oct 1935  -2.516442868  -0.656061105   3.40100452  -0.04138856   1.26235471
Nov 1935   4.801948906   2.528351978   7.84968810   6.65055456   5.49610167
Dec 1935  -2.277275657  -0.034638033  -1.72464082  -1.22932791   2.79776278
Jan 1936  -1.239367454  -2.071877717  -1.83170754  -2.83434183   0.34644925
Feb 1936  -1.388652724  -3.059801853  -3.16664679  -4.68989356   1.64072945
Mar 1936   0.681593969   2.436079308  -0.65752616   0.38238549   3.82411611
Apr 1936  -1.048191701  -0.333673796  -4.24549231  -5.63987057   6.62219416
May 1936  -1.081718373   1.663083762  -1.14702614  -2.74669575  -2.30351865
Jun 1936   1.437611425  -0.654453027   3.92229397   2.43746849  -1.40095460
Jul 1936   1.932128855  -1.237873346   3.34293295   1.47159618  -3.56510132
Aug 1936   1.495210450  -2.131824003   3.16676944  -2.20813585   5.97926494
Sep 1936  -0.886674375  -0.368129179  -1.04098032   0.41232064   0.85656603
Oct 1936  -1.929477721   0.464656534  -1.79213521  -0.72790582   2.61930721
Nov 1936   2.452576103  -1.012437344  -0.20257812   1.96470938   5.18367895
Dec 1936  -2.258335692   0.430464449  -1.96556669  -0.43458959  -0.77571007
Jan 1937   0.240160599  -0.804725037   0.23089961   2.10022996   0.09810923
Feb 1937   0.702404534   0.887985043   1.58213787   1.48426708  -1.01528731
Mar 1937   2.488453139   1.649692602   3.70734802   0.99173758  -1.52073518
Apr 1937   0.611212289   2.567540111   3.49883152   5.22864451   2.31277207
May 1937   4.163381051   0.876546842   4.76216424   5.80954162  -0.28754208
Jun 1937   2.639358981  -4.012398028   0.36844114  -3.18456601  -0.57230636
Jul 1937  -1.395726507   0.869923121  -1.82979293   0.22090332   0.98920909
Aug 1937   1.823218765  -1.570712921   1.75080673   1.43776755   3.13003118
Sep 1937   0.952922009   0.817473579   1.75859551   1.96722908   3.26736898
Oct 1937   0.096426024   0.036377909   0.29079963   1.92175994   4.39292536
Nov 1937   3.620319177  -0.722951354   6.96782517   4.19160901   2.27513122
Dec 1937   2.666392152   3.188376036   1.74255290   1.91642945  -4.48008175
Jan 1938  -4.268563042   2.755352680   0.68642924   0.41598632   1.97406823
Feb 1938   0.318783213   1.716713825   0.72693813   0.34238020  -1.53094616
Mar 1938   2.109669450   2.390003219   1.13706318   4.78728395  -0.47231242
Apr 1938   3.159099464   5.365015945   4.78958447   6.49986278  -4.53271349
May 1938   0.906752603   1.230303122  -4.01285852  -2.84086510  -3.07463109
Jun 1938  -2.432497402  -3.779386012  -1.55388967  -2.11669665   0.54148149
Jul 1938  -2.009909173   0.960659397  -2.89980370  -3.46037268   1.12388902
Aug 1938   5.116868121   2.901654298   5.80217058   7.92814808  -1.25325414
Sep 1938   2.412583380   2.938826512  -0.05018670  -0.71271279  -0.43212167
Oct 1938   1.334252866   0.873929911   2.48671166   2.44236934  -2.76478953
Nov 1938   4.230672267   0.738608181   4.34412417   5.03752602   0.10728160
Dec 1938   2.450702032  -1.575026593  -1.32016437  -2.19368097   2.73451497
Jan 1939   2.164399905  -0.728565248  -0.82938161  -0.31752893  -1.51000208
Feb 1939  -0.713794703   2.177377614   0.17744834  -1.16652451   1.85005067
Mar 1939  -2.686956813  -4.934148863  -1.41439800  -0.41121418   3.49909770
Apr 1939   3.415392470   0.638438365   3.06402338   2.62707088   1.73303970
May 1939  -0.819422980   1.845992534   0.08460170  -0.95141825  -0.99977702
Jun 1939   0.861855778   1.399491596  -1.44074292  -1.82462217   1.55118434
Jul 1939  -0.801940504   0.909632902   0.79256087   2.17196054   0.07165707
Aug 1939   1.138941934   4.057068880   2.30759680   3.94098279  -1.47776511
Sep 1939   2.045773196   4.141767687   4.68283077   3.24652916   0.58160046
Oct 1939   4.458057259   2.599942248   5.32537583   5.55264736   3.99011040
Nov 1939   1.586624398  -1.262368366   0.16695228   0.83990349   0.10148472
Dec 1939  -1.641111231  -2.834060439  -4.89622446  -5.50787083  -1.62047103
Jan 1940   2.410580457   1.105967011   2.58040887   2.47823246   1.80260538
Feb 1940  -1.170826672   2.367907293  -2.06411363  -2.41506054   0.36672421
Mar 1940   1.580296643  -3.131515467   0.19385625  -1.20004744   2.37358250
Apr 1940  -4.526064172   0.019134636  -4.67885543  -3.94201239  -1.60880289
May 1940  -1.597200970   2.447752199  -3.58225437  -6.05919675   0.44334617
Jun 1940  -2.349612449  -0.019578571  -0.55243155   0.72024806   1.24066344
Jul 1940   2.221542864   3.052830702   1.83689319   1.97278746  -0.48786917
Aug 1940  -0.781448228   2.698065010  -1.57763052  -1.11466267   4.07423509
Sep 1940   0.566643764  -0.769345854   0.19963021  -2.60893986   1.63727551
Oct 1940  -2.617450257   0.621061722  -3.22020064  -2.73422429   1.69702806
Nov 1940  -0.006867189   0.005136195  -0.44257281   1.34455031  -1.09668275
Dec 1940   3.480935789   2.058608523   2.32846755   4.18103833   0.63022783
Jan 1941  -0.973162338   0.588798785  -2.18198322  -3.72567036   2.71511246
Feb 1941  -0.307486757  -2.182919508   0.70835488   1.96222240   4.38818475
Mar 1941   3.999571170   3.426410549   3.98158202   4.58328964  -1.10053789
Apr 1941   2.864690363   1.682483014   1.98349031   2.05584490  -0.30817625
May 1941  -1.978117266  -1.281442306  -1.63782929  -0.51650139   1.51206490
Jun 1941   2.885523323  -1.533485228   1.02771344   1.70773670  -0.79734121
Jul 1941   1.874954821   1.698492034  -0.47315623  -0.15551758  -5.35095556
Aug 1941  -2.053250973  -0.541056828  -2.34965102  -1.84900233  -4.54316682
Sep 1941   2.040752308  -0.517199335   0.50281384   1.23900371 -13.03391858
Oct 1941   3.317807774  -1.762374185   3.86892599   5.59387662  -1.17426172
Nov 1941   0.375066369  -2.368641622  -1.26047067   0.73570586   9.15119376
Dec 1941  -0.007829934  -1.516008275  -2.37696135  -2.01103007  -9.74268814
Jan 1942  -0.086266058   0.718067286   2.30135256  -0.75879867  -0.85201337
Feb 1942  -0.210086173   0.920639643  -0.24850642  -0.76258158   4.46099200
Mar 1942   1.948730869   4.207389120   0.90498355   4.31797932   0.65509915
Apr 1942  -1.191947173   4.089756925  -2.01952132  -3.40414889   6.48180331
May 1942   1.604174116   0.960821698  -0.29252928   0.81218603  -0.88617716
Jun 1942   1.312195831  -0.448675379   1.59270015   1.60506124   0.84846170
Jul 1942  -0.311111362   0.879984973  -0.79434079  -2.63657232   2.02150487
Aug 1942   1.146550663   6.151808921   2.75405541   6.11328862   1.67849488
Sep 1942   2.009260113   4.488196656   0.54722524   1.58242058   1.44285738
Oct 1942  -0.430657675  -4.723674949   2.61119658  -0.40420319   1.74722426
Nov 1942  -3.463216438   0.612135490  -1.62870195  -1.69739174  -4.29021395
Dec 1942  -0.157174864  -3.243876364   3.81295765   0.41932183  -2.84357969
Jan 1943   0.218574979   1.773038675   4.51063464   3.25712562  -1.82617004
Feb 1943   0.085426791  -5.181407763   6.22071823   4.06161706  -0.84928797
Mar 1943  -0.214128599   1.142294121  -4.27019709  -0.45030622  -0.94316376
Apr 1943   0.463333472   2.141541639  -1.22595756  -0.01032535  -7.64477734
May 1943   0.929059900  -2.030228392   2.51283143  -0.01080627  -0.70064451
Jun 1943  -0.307512468  -1.441770783  -2.75614662  -1.04598547   6.72804009
Jul 1943  -7.938326246  -3.475271736  -4.61910047  -5.54572596  -2.47310597
Aug 1943  -2.083840228  -0.272191022  -2.09628988  -8.03122455   7.33656095
Sep 1943 -11.289123758 -16.109030430  -6.83143148 -14.56369241   2.19130357
Oct 1943  -5.627243685  -1.495169860  -6.16748582  -4.84996185  -6.51600802
Nov 1943  -6.522792142   0.546901923  13.00066085   2.41362099   2.48427888
Dec 1943   0.007208194  -1.179653135  -9.83586746  -6.81175278   4.16228912
Jan 1944  -1.100707647  -2.273350519  -0.40166396  -8.04760545   1.40035068
Feb 1944   0.571699643   0.850011449   2.37429790   8.46410093   3.21180406
Mar 1944   4.332430678   5.522629572   1.04895914   3.44856921   5.17783112
Apr 1944   8.815428261   9.271842876   6.28257701   5.95646438  -3.06548599
May 1944   2.115084305  -5.829921814  -1.99203300  -2.17663112   1.65071975
Jun 1944   0.185507300   6.792919125   0.71843513   5.92023767  -0.09776437
Jul 1944  -1.168856186  -1.446329890   3.06555264   4.21246974   0.26170121
Aug 1944  -1.957458453   0.923699857   1.67243959   0.85889627  -7.26393674
Sep 1944   3.391803497   1.017825192   1.45579062  -0.30885808   2.19622297
Oct 1944  -0.422652813   2.152618494   1.95145941   0.89465821  -5.13882378
Nov 1944  -2.073838998   1.190953079  -2.78870686  -2.52054119  -2.72659232
Dec 1944   0.104102446  -1.372111034  -1.27457152  -2.29031054   0.81612885
Jan 1945  -5.100007614   0.810369570  -1.77460187   3.16909170   2.15964250
Feb 1945  -0.337319694   4.098954246   1.88418464  -1.19145207  -0.46183293
Mar 1945   1.333444317   0.625967813  -1.09408955   0.23012433  -0.62667828
Apr 1945  -4.994448070  -4.400959522  -6.52779644  -7.66104042  -6.82009768
May 1945   3.412095709  -1.035788578   7.00525716   0.78309898   2.58931574
Jun 1945   3.933936809   2.019377481   3.52149063   7.73029340  -3.02314588
Jul 1945  -1.867763405  -3.437216846   3.09748536  -1.90879864   2.28666125
Aug 1945   1.831533011   2.808557821   4.69972687   9.33206568   2.01229409
Sep 1945   1.214081559   1.493314786  -0.81572267   0.61172456   0.69086465
Oct 1945  -2.604802835  -0.862436776  -1.35457301  -4.68517234   0.34276845
Nov 1945   0.831031873   3.082868584   7.16742500   4.59738318   1.32539003
Dec 1945   4.012467303   0.848179817   2.29558978   6.33826689   2.88851445
Jan 1946   2.643580288   2.482900145   1.31680701   2.80585273  -3.77403280
Feb 1946  -0.239125141   0.749688582   1.53486214   2.14039385  -1.81670043
Mar 1946   3.811091385   2.396103616   6.04965965   5.08610910   2.61546364
Apr 1946  -1.285488240  -2.016373909   1.79410357  -2.37459520  -1.73212605
May 1946  -2.447509836   1.286886550   1.15013017  -1.16106334   0.28167022
Jun 1946   2.446838298   1.766004449   5.88049754   1.17199123   2.11789933
Jul 1946  -0.778061397  -2.387533659  -2.34230363  -0.09776997  -0.70927914
Aug 1946  -4.347507282  -6.016889342 -10.49080021  -7.43532798   2.61462408
Sep 1946   2.934534206   3.876561532   2.51070600   4.59176024   0.42326957
Oct 1946  -2.579338966  -2.328347557  -5.42613473  -5.78053077   0.14421967
Nov 1946  -0.423610663   1.124029784  -1.66059455   0.67282952   1.19848852
Dec 1946   1.559522424   1.591692480   1.62226355   0.84058441  -2.15510600
Jan 1947   1.244618556   1.867821982   2.02519929   3.63185988   2.38827913
Feb 1947   0.039935784  -1.390380566  -0.36577296  -0.78099629  -0.20971310
Mar 1947   1.639565806   1.216318900  -0.45384604  -1.19521249   0.60685418
Apr 1947  -5.336031808  -4.663955312  -6.86033704  -7.70014777  -1.59277145
May 1947   1.901593754   1.660568223   2.57717461   5.32039910   0.34306758
Jun 1947  -0.097189354   1.764251373  -3.08677728   1.76916439  -2.31320417
Jul 1947   1.369279344   1.622276562   2.29551619   2.63162296  -1.57719831
Aug 1947   1.663862618   0.356090687   1.31080101   1.10843617  -4.19880206
Sep 1947  -0.222573557  -1.502984308   0.86608400  -1.10412292  -0.84298436
Oct 1947  -0.514164733   0.679220257   0.46631177  -0.25984743  -0.51870253
Nov 1947   1.418692441  -0.173898228   1.15353309   2.41801529  -2.99574732
Dec 1947  -2.486504398  -0.250601575   0.46787684   2.28698581  -7.01445551
Jan 1948  -4.333737500  -2.821395529  -2.48782813  -1.22648061  -0.62785016
Feb 1948   0.050096018   1.252033766  -1.49375603  -0.80405438  -4.38090346
Mar 1948   2.484026951   1.063077084   1.88005729   0.51126967   4.20108620
Apr 1948  -2.603373800  -2.641122852  -3.27307722  -2.58217288  -2.22157457
May 1948   0.060649252  -1.978783997   1.37714597  -1.45675120   1.57746449
Jun 1948  -0.021226916   2.655919463   1.78983808   3.28663205  -0.88446557
Jul 1948   2.038027682  -2.499458253  -0.37177560  -1.48059347   1.36769677
Aug 1948   4.603260259   2.665775041   3.04200583   3.36116185  -0.42395459
Sep 1948  -0.767683796  -1.368092281  -0.16688867  -0.66702679  -1.09069777
Oct 1948   1.961053883  -1.430797445   0.18242632  -0.92532766  -4.52835823
Nov 1948   1.128150372  -0.280864644   1.56413560   1.81661317   2.70278034
Dec 1948  -0.757198204  -4.742287654  -1.83982145  -1.85309118  -0.46854241
Jan 1949   1.973000423   0.785559890   2.75904280   2.21640183   0.34709452
Feb 1949  -0.498045620   0.362648543  -0.30952932  -1.15696283   4.66640028
Mar 1949   1.298479886   0.642154461   0.30392747  -0.46602922   0.41831979
Apr 1949  -0.642807672   1.137312407  -1.54438872  -2.41112809  -2.93143622
May 1949   1.940526197   1.997473222   0.77506706  -0.02812515           NA
Jun 1949  -1.243539341  -2.099692858  -2.53375472  -3.20684831           NA
Jul 1949  -1.620661623   0.434179376  -0.73754971  -0.92765324           NA
Aug 1949  -2.390342906  -2.909807890  -4.26442539  -3.33663999           NA
Sep 1949  -1.296241390  -0.803252934  -0.74880937  -2.61336017           NA
Oct 1949  -2.111238764  -1.055318739  -0.13085881  -0.56772739           NA
Nov 1949  -0.417191364  -1.492629937  -3.12589333  -5.06840140           NA
Dec 1949  -3.722576827  -8.982660260   7.64436246  -5.52964853           NA
Jan 1950   2.872964193   1.575636559  -3.13948750  -0.84581737           NA
Feb 1950  -3.997928056  -1.372982895  -2.56531273  -3.37338311           NA
Mar 1950   3.480936422   4.496744598   3.47607548   3.17955515           NA
Apr 1950  -0.684436024  -2.966872216  -0.79287365  -2.35141395           NA
May 1950   3.038984910   0.032835331   0.83116255   2.87001801           NA
Jun 1950  -0.758148995  -4.075954211  -2.91910621  -3.35738836           NA
Jul 1950  -1.431295539  -2.225252551  -0.71398483   1.00457161           NA
Aug 1950  -1.506358274  -0.755438192  -1.12906002   1.09638155           NA
Sep 1950   1.953004870   2.457422896  -0.98069985  -1.29859254           NA
Oct 1950  -2.547529778  -1.746091137  -4.28324614  -2.73959802           NA
Nov 1950  -2.125695417  -4.219973390   2.38784609   3.25200318           NA
Dec 1950  -3.840342378  -1.305338159  -2.46811453  -1.78862262           NA
Jan 1951  -1.774362554   3.813834360   2.72263771  -0.24876940           NA
Feb 1951   3.075467227   4.619314071   3.76370606   5.65227293           NA
Mar 1951   1.893652459   3.205301892  -0.22246950   0.93917790           NA
Apr 1951  -0.652733347  -4.335431160  -3.78738033  -4.14856291           NA

My code:

funcao_otimazarei<- function(par, yt) {
  ans <- fkf(a0 = c(0), 
             P0 = 10^7*diag(1), 
             dt = matrix(par[4], ncol = 1),
             ct = matrix(0, ncol = 1), 
             Tt = par[1]*diag(1),
             Zt = matrix(1),
             HHt =  matrix(par[3]), 
             GGt = matrix(par[2]),
             yt = matrix(Matrix_FK[,1], nrow = 1))
  return(-ans$logLik)
}

# ordem vetor par: phi (Tt), sigma2_ep (GG_t) , sigma2_eta (HHt), alpha (dt)

otimizar_agora<- optim(par = c(0.5,0.5,0.5,0.5), otimizar, yt = Matrix_FK[,1],lower = c(-Inf,0,0,-Inf), method = "L-BFGS-B")

otimizados$par

FK_ação<- fkf(a0 = c(0),
          P0 = 10^7*diag(1),
          dt = matrix(otimizados$par[4], ncol = 1),
          ct = matrix(0, ncol = 1),
          Tt = otimizados$par[1]*diag(1),
          Zt = matrix(1), 
          HHt = matrix(otimizados$par[3]),
          GGt = matrix(otimizados$par[2]),
          yt = matrix(Matrix_FK[,1], nrow = 1))

k_fk <- ts(t(FK_ação$att), start = 1) 

plot(k_fk)
  • 1

    What is fkf? this on some package?

  • Hi Robert, package FKF.

2 answers

4


If you have a function that takes an array as input and returns another vector as output you can always use the apply. This is for any function.

For example, starting very simple. Consider the function defined below:

funcao <- function(x) {
  x[1:10] <- x[1:10] + 1
  return(x)
}

She picks up a vector x and sum 1 in each of its first 10 elements. If you own the matrix:

x <- matrix(1:200, ncol = 10)

And if you want to apply this function to each of your columns you can use the `apply as follows:

apply(x, 2, funcao)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    2   22   42   62   82  102  122  142  162   182
 [2,]    3   23   43   63   83  103  123  143  163   183
 [3,]    4   24   44   64   84  104  124  144  164   184
 [4,]    5   25   45   65   85  105  125  145  165   185
 [5,]    6   26   46   66   86  106  126  146  166   186
 [6,]    7   27   47   67   87  107  127  147  167   187
 [7,]    8   28   48   68   88  108  128  148  168   188
 [8,]    9   29   49   69   89  109  129  149  169   189
 [9,]   10   30   50   70   90  110  130  150  170   190
[10,]   11   31   51   71   91  111  131  151  171   191
[11,]   11   31   51   71   91  111  131  151  171   191
[12,]   12   32   52   72   92  112  132  152  172   192
[13,]   13   33   53   73   93  113  133  153  173   193
[14,]   14   34   54   74   94  114  134  154  174   194
[15,]   15   35   55   75   95  115  135  155  175   195
[16,]   16   36   56   76   96  116  136  156  176   196
[17,]   17   37   57   77   97  117  137  157  177   197
[18,]   18   38   58   78   98  118  138  158  178   198
[19,]   19   39   59   79   99  119  139  159  179   199
[20,]   20   40   60   80  100  120  140  160  180   200

Now, you can complicate its function in any way you want. As long as it receives a vector as input and returns vector as well. Imagine you want to find the parameter a minimising the sum of (x - a)^2 for the whole vector. a can be interpreted by the average of x. Next you want to split each element of x for the found value of a.

You could set a function:

funcao <- function(x) {
  funcao_obj <- function(par, x){
    sum((par[1] - x)^2)
  }
  otm <- optim(par = c(1), funcao_obj, x = x, method = "BFGS")$par
  x <- round(x/otm[1],2)
  return(x)
}

And use the apply, likewise:

apply(x, 2, funcao)

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,] 0.10 0.69 0.81 0.87 0.90 0.91 0.93 0.94 0.94  0.95
 [2,] 0.19 0.72 0.83 0.88 0.91 0.92 0.93 0.94 0.95  0.96
 [3,] 0.29 0.75 0.85 0.89 0.92 0.93 0.94 0.95 0.96  0.96
 [4,] 0.38 0.79 0.87 0.91 0.93 0.94 0.95 0.96 0.96  0.97
 [5,] 0.48 0.82 0.89 0.92 0.94 0.95 0.96 0.96 0.97  0.97
 [6,] 0.57 0.85 0.91 0.94 0.95 0.96 0.97 0.97 0.97  0.98
 [7,] 0.67 0.89 0.93 0.95 0.96 0.97 0.97 0.98 0.98  0.98
 [8,] 0.76 0.92 0.95 0.96 0.97 0.98 0.98 0.98 0.99  0.99
 [9,] 0.86 0.95 0.97 0.98 0.98 0.99 0.99 0.99 0.99  0.99
[10,] 0.95 0.98 0.99 0.99 0.99 1.00 1.00 1.00 1.00  1.00
[11,] 1.05 1.02 1.01 1.01 1.01 1.00 1.00 1.00 1.00  1.00
[12,] 1.14 1.05 1.03 1.02 1.02 1.01 1.01 1.01 1.01  1.01
[13,] 1.24 1.08 1.05 1.04 1.03 1.02 1.02 1.02 1.01  1.01
[14,] 1.33 1.11 1.07 1.05 1.04 1.03 1.03 1.02 1.02  1.02
[15,] 1.43 1.15 1.09 1.06 1.05 1.04 1.03 1.03 1.03  1.02
[16,] 1.52 1.18 1.11 1.08 1.06 1.05 1.04 1.04 1.03  1.03
[17,] 1.62 1.21 1.13 1.09 1.07 1.06 1.05 1.04 1.04  1.03
[18,] 1.71 1.25 1.15 1.11 1.08 1.07 1.06 1.05 1.04  1.04
[19,] 1.81 1.28 1.17 1.12 1.09 1.08 1.07 1.06 1.05  1.04
[20,] 1.90 1.31 1.19 1.13 1.10 1.09 1.07 1.06 1.06  1.05

3

If your function otimizar inside optim is funcao_otimazarei, can solve using lapply and sapply (where a function, which usually returns several data structures, is applied to each column).

otimizados<- lapply(1:ncol(Matrix_FK),function(ii)optim(par = c(0.5,0.5,0.5,0.5), otimizar, 
                       yt = Matrix_FK[,ii],lower = c(-Inf,0,0,-Inf),
                       method = "L-BFGS-B"))


FK_ação<-sapply(1:length(otimizados),function(ii) fkf(a0 = c(0),
              P0 = 10^7*diag(1),
              dt = matrix(otimizados[[ii]]$par[4], ncol = 1),
              ct = matrix(0, ncol = 1),
              Tt = otimizados[[ii]]$par[1]*diag(1),
              Zt = matrix(1), 
              HHt = matrix(otimizados[[ii]]$par[3]),
              GGt = matrix(otimizados[[ii]]$par[2]),
              yt = matrix(Matrix_FK[,ii], nrow = 1))$att
)

k_fk <- ts(FK_ação, start = 1) 

plot(k_fk)

inserir a descrição da imagem aqui

  • Thanks a lot, guys!

  • Robert, a detail: I realized that when I call otimizados all parameters otimizados[[i]]$par are the same. I think he’s not pulling from every column of the Matrix Matrix_FK.

  • Change your Optimize function by setting yt = matrix(yt, nrow = 1) and not yt = matrix(Matrix_FK[,1], nrow = 1).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.