#r #parallel.foreach
#r #parallel.foreach
Вопрос:
У меня возникли некоторые проблемы при распараллеливании MSGARCH с использованием пакета foreach. Когда я использую %do%
, все в порядке, потому что он обрабатывается последовательно. Однако, когда я использую %dopar%
, возникает ошибка. Вот код:
library(parallel); library(foreach); library(doParallel)
numCores <- detectCores();numCores
registerDoParallel(numCores)
residuosp_confirmed = c(-0.0196875366159436,0.00325443127173577,0.0182816284009416,0.0192446651938748,0.00727550837699058,-0.00999897450094205,-0.0212720835440839,-0.0192817949364019,-0.00535239570149668,0.0120597269453738,0.0217378727204427,0.017737009818541,0.00279267525250477,-0.0141336585344371,-0.022159863506126,-0.0161684683146264,-6.29614361339562e-05,0.0161183762143486,0.0221921171437408,0.0143172587608738,-0.0024993188612204,-0.0178564942071159,-0.0219507373320812,-0.0123484948718229,0.00502294737631941,0.0192785513571538,0.0214850694443236,0.0102484661431422,-0.00742198690135835,-0.0205036291642626,-0.0206629774349384,-0.00807466097992956,0.00958801203908789,0.0213286302464695,0.0198641012505057,0.00612996178140447,-0.01126495676855,-0.0217711790235895,-0.0183296265942021,-0.00397122365318793,0.0135744821158487,0.0217166706792698,0.0164431859793376,0.00318083068541324,-0.0153337496395438,-0.0168318358577133,-0.0124564763090568,-0.00560412839127421,0.00798634418141394,0.0241547777864991,0.0283247142244195,0.0326634428870256,0.0338032418120409,-0.0309089829915761,-0.0118948170606764,-0.0149851739107048,-0.0301663401729663,0.00572367889175109,0.0749695697436577,-0.0385487421898849,-0.0311637967397238,0.0550533740233793,-0.0558319261056033,-0.179722926116892,0.0645793594939214,0.195615688800576,0.201602293259364,-0.0857029522415413,-0.304812541019657,-0.343785934964325,-0.0193536740092431,0.154892832064571,0.227080031906639,0.247042094989573,0.00468354741126695,-0.0972809286263443,-0.237583268178664,-0.309865066683055,0.0940247515243544,0.401155571026064,0.274566110649214,0.0413989102283142,-0.150455877021988,-0.107660184622748,-0.334135048043844,-0.126277820932189,0.335186226996402,0.277640177792035,-0.00204721794142648,0.0236693324340407,-0.331548217518189,-0.407888322282594,-0.0221193962320836,0.284434742332657,0.450910290618038,0.293599374360697,-0.0902823610810239,-0.366837405564773,-0.453603535222909,-0.275745020887442,0.429494139040331,0.430138861008622,0.157034239142103,-0.103282137356553,-0.261606735967327,-0.334059714292657,-0.0513886375995055,0.257052930337833,0.460801822346426,0.107235405958507,-0.206004284348478,-0.182409505034553,-0.393224112580217,-0.000699808392974115,0.2107427060963,0.37051985059476,-0.0178820975270486,-0.0530665738933614,-0.232390867400116,-0.250988437739553,0.0476025555059126,0.207364084830832,0.195665759660895,0.138343841812137,-0.0248232472674759,-0.103145294816385,-0.307409184908437,-0.186774880511828,0.12037419781335,0.35191978344821,0.162994478551899,-0.161765463824254,-0.109245636987554,-0.215234362709374,-0.15379580920973,0.268985900209686,0.326052062820931,0.112949640642254,-0.10916274612397,-0.273147130427355, -0.225904368331137,-0.0535022858502821,0.213962400864431,0.458223952226378,0.0792445872222229,-0.262342104863832,-0.310869593544637,-0.14596745781693,0.0147767210029759,0.291042783662462,0.462046848530148,-0.18524228143278,-0.323491015339061,-0.13840542493231,-0.207961132162425,-0.0315800080669762,0.478666379493214,0.358646718806687,-0.00223810139391538,-0.458926857687976,-0.542469522679019,-0.198630288150908,0.53019461438016,0.695356743440516,0.230989228884446,0.105255080892084,-0.889964499522834,-0.424026685775305,-0.179025286557036,0.115074098844135,0.755834975364839,0.493243649024643,-0.0557647061305075,-0.676483611348495,-0.527852568459921,-0.411719363785436,0.403884323298847,0.889996670591729,0.4830529567927,-0.102768061596523,-0.517427632806724,-0.674850884272866,-0.225711216708831,0.0667212242576542,0.834913547293299,0.822765382108459,-0.141373498257285,-0.945823081900105,-0.829508223814126,-0.133408837966964,0.306494619450977,1.06706072390013,0.882624968503112,-0.0979854436910752,-0.947462466487382,-0.496693360005315,-0.421860718154182,0.110255891184119,0.643500230177106,0.720063055571549,0.0556716534883973,-0.395779616552298,-0.776873879924312,-0.370869581562704,-0.343580635620593,1.07050858919907,0.893667752892966,0.144149903535234,-0.689331347864336,-0.610373303072764,-0.204106922367902,-0.0557060791181194,0.585109579687194,0.710859262384192,0.0174141036808852,-0.466871091388327,-0.626763671898102,-0.297753001433115,0.040555895564039,0.564626235801996,0.705799286675642,0.0750613044187593,-0.478737190261705,-0.487080519650291,-0.570108362865829,-0.316410770808425,0.641599306385284,1.09437775518271,0.677757969589283,-0.279199281909371,-0.768187524460682,-0.688781445913068,-0.516372624117536,0.53660985088371,0.794345050014078,0.289416759410566,-0.264440031616523,-0.360220781758186,-0.519053840344975,-0.138096076672349,0.505076752726044,0.361358239881256,-0.254043907425136,0.341922351004651,-0.16155210041353,-0.619706033381399,-0.205795751346826,0.414125857617523,0.741315042749241,0.388243292958371,-0.418276395198082,-0.347489603765833,-0.66652960594701,-0.421345746166661,0.652733059555744,1.08021633725686,0.205045469739519,-0.47552565817178,-0.78068196906867,-0.653369814013868,0.105357406204062,0.617608779663909,0.96466240155043,0.405586776519645,-0.637162154413196,-0.873103137539985,-0.562469972687096,0.0913514879956615,1.02644707066352,0.70368667296402,-0.430787129231878,-0.0295745418453463,-0.385761921205086,-0.845222508703945,-0.307182015834936,0.575387880592629,1.43246491181298,0.281966789957515,-0.48486854698586,-0.565262198371098,-0.677067016626925,-0.0954371440992165,0.769978099273788,0.245450746918551,0.59326599100154, -1.37114452366068,0.126781856495443,-0.799294739286162,0.288349195211991,0.984132688177628,1.31532130545469,0.111325864567524,-1.12000149053335, -1.1350497405753,-1.22403679433685,-0.233873883070884,1.74883652789099,2.2254363701363,-0.437009359474075,-0.849335452553339,-1.4017061496311, -1.36768245828704,0.0486908476891424,1.84907060854246,1.83084740199672,-1.02442891967816,-0.93191211228321,-0.182620164153958,1.81844836597859, -2.4140455718039,2.33046604256119,1.87907927313668,-0.638446963737747,-2.13013114762951,-2.00554982895445,-1.4032225125371,0.383941458863185, 2.55278816928774,2.77674692359084,-0.338563216543842,-2.26182765765696,-1.49794650378289,-0.942117640211966,0.441963423519597,2.5573100364143,2.57951630324916,-0.132936888542228,-2.38582375995109,-3.24965441702695,-0.838541004341834,0.864937380194083,3.68448023241881,1.19279425696347,-1.21276844975653,-1.85115982588624,-1.6774725265934,2.37965146711994,3.89626653010402,-3.16392254206833,1.71439013844662,-0.567052451289917,-1.96110564410559,-1.93004934430742,0.44863743623737,2.48411534832981,-3.42289418528128,4.00185283424478,2.40125067255723,-2.1944539939102,-2.97263707252501,-2.57558947366834,-0.516666512035414,2.87868805978347,3.76876793799052,0.687075554312873,-1.66950377764177,-2.21200902073682,-1.75700315862579,-0.148858200169474,2.73767019853833,2.74046400926481,1.40897706375589,-2.40852701509342,-2.47917851016626,-1.67476773465148,0.315773403061838,
2.21276273384996,2.52554280945548,-0.415776470298961,-1.07441236085753,-1.75736446513317,
-1.60656964115673,0.232522579977605,2.02611987453222,1.90125649136352,-0.511868541729463,
-0.158687377880881,-0.932134741274215,-0.490616203292175,0.326189896965346)
#Sorry for this big vector, but it's for keeping the reproducibility of my issue
modelo = c("sARCH","sGARCH","tGARCH","eGARCH","gjrGARCH")
distribuicao = c("norm","std","ged","snorm","sstd","sged")
#Here using %do%. Works fine, but it's not working in parallel
system.time({
abdi1<-foreach(m=1:5) %:%
foreach(d=1:6) %do% {try(FitML(spec = CreateSpec(variance.spec=list(model = modelo[m]),
distribution.spec=list(distribution = distribuicao[d]),
switch.spec = list(do.mix = FALSE, K = 2)),
data = residuosp_confirmed))}
})
abdi1 #Here it will show all the results.
#Here using %dopar%.
system.time({
abdi2<-foreach(m=1:5) %:%
foreach(d=1:6,.packages = c("MSGARCH")) %dopar% {
MSgarch_settings <- CreateSpec(variance.spec=list(model = modelo[m]),
distribution.spec=list(distribution = distribuicao[d]),
switch.spec = list(do.mix = FALSE, K = 2))
try(FitML(spec = MSgarch_settings,data = residuosp_confirmed))
}
})
abdi2 #An error occurs
[[1]]
[[1]][[1]]
Error in .External(list(name = "CppMethod__invoke_notvoid", address = <pointer: (nil)>, :
NULL value passed as symbol address
Кто-нибудь может мне помочь, как это исправить? Спасибо!!! (PS: я пробовал предыдущие предложения, опубликованные здесь на Stack overflow, но они не сработали).
Доктор Абдинар Оливейра.
Ответ №1:
Через 7 месяцев я, наконец, смог решить эту проблему. Вот код:
library(purrr); library(furrr); library(MSGARCH); library(tictoc); library(tidyr)
residuosp_confirmed = c(-0.0196875366159436,0.00325443127173577,
0.0182816284009416,0.0192446651938748,0.00727550837699058,-0.00999897450094205,
-0.0212720835440839,-0.0192817949364019,-0.00535239570149668,0.0120597269453738,0.0217378727204427,
0.017737009818541,0.00279267525250477,-0.0141336585344371,-0.022159863506126,-0.0161684683146264,
-6.29614361339562e-05,0.0161183762143486,0.0221921171437408,0.0143172587608738,-0.0024993188612204,
-0.0178564942071159,-0.0219507373320812,-0.0123484948718229,0.00502294737631941,0.0192785513571538,
0.0214850694443236,0.0102484661431422,-0.00742198690135835,-0.0205036291642626,-0.0206629774349384,
-0.00807466097992956,0.00958801203908789,0.0213286302464695,0.0198641012505057,0.00612996178140447,
-0.01126495676855,-0.0217711790235895,-0.0183296265942021,-0.00397122365318793,0.0135744821158487,
0.0217166706792698,0.0164431859793376,0.00318083068541324,-0.0153337496395438,-0.0168318358577133,
-0.0124564763090568,-0.00560412839127421,0.00798634418141394,0.0241547777864991,0.0283247142244195,
0.0326634428870256,0.0338032418120409,-0.0309089829915761,-0.0118948170606764,-0.0149851739107048,
-0.0301663401729663,0.00572367889175109,0.0749695697436577,-0.0385487421898849,-0.0311637967397238,
0.0550533740233793,-0.0558319261056033,-0.179722926116892,0.0645793594939214,0.195615688800576,
0.201602293259364,-0.0857029522415413,-0.304812541019657,-0.343785934964325,-0.0193536740092431,
0.154892832064571,0.227080031906639,0.247042094989573,0.00468354741126695,-0.0972809286263443,
-0.237583268178664,-0.309865066683055,0.0940247515243544,0.401155571026064,0.274566110649214,
0.0413989102283142,-0.150455877021988,-0.107660184622748,-0.334135048043844,-0.126277820932189,
0.335186226996402,0.277640177792035,-0.00204721794142648,0.0236693324340407,-0.331548217518189,
-0.407888322282594,-0.0221193962320836,0.284434742332657,0.450910290618038,0.293599374360697,
-0.0902823610810239,-0.366837405564773,-0.453603535222909,-0.275745020887442,0.429494139040331,
0.430138861008622,0.157034239142103,-0.103282137356553,-0.261606735967327,-0.334059714292657,
-0.0513886375995055,0.257052930337833,0.460801822346426,0.107235405958507,-0.206004284348478,
-0.182409505034553,-0.393224112580217,-0.000699808392974115,0.2107427060963,0.37051985059476,
-0.0178820975270486,-0.0530665738933614,-0.232390867400116,-0.250988437739553,0.0476025555059126,
0.207364084830832,0.195665759660895,0.138343841812137,-0.0248232472674759,-0.103145294816385,
-0.307409184908437,-0.186774880511828,0.12037419781335,0.35191978344821,0.162994478551899,
-0.161765463824254,-0.109245636987554,-0.215234362709374,-0.15379580920973,0.268985900209686,
0.326052062820931,0.112949640642254,-0.10916274612397,-0.273147130427355, -0.225904368331137,
-0.0535022858502821,0.213962400864431,0.458223952226378,0.0792445872222229,-0.262342104863832,
-0.310869593544637,-0.14596745781693,0.0147767210029759,0.291042783662462,0.462046848530148,
-0.18524228143278,-0.323491015339061,-0.13840542493231,-0.207961132162425,-0.0315800080669762,
0.478666379493214,0.358646718806687,-0.00223810139391538,-0.458926857687976,-0.542469522679019,
-0.198630288150908,0.53019461438016,0.695356743440516,0.230989228884446,0.105255080892084,
-0.889964499522834,-0.424026685775305,-0.179025286557036,0.115074098844135,0.755834975364839,
0.493243649024643,-0.0557647061305075,-0.676483611348495,-0.527852568459921,-0.411719363785436,
0.403884323298847,0.889996670591729,0.4830529567927,-0.102768061596523,-0.517427632806724,
-0.674850884272866,-0.225711216708831,0.0667212242576542,0.834913547293299,0.822765382108459,
-0.141373498257285,-0.945823081900105,-0.829508223814126,-0.133408837966964,0.306494619450977,
1.06706072390013,0.882624968503112,-0.0979854436910752,-0.947462466487382,-0.496693360005315,
-0.421860718154182,0.110255891184119,0.643500230177106,0.720063055571549,0.0556716534883973,
-0.395779616552298,-0.776873879924312,-0.370869581562704,-0.343580635620593,1.07050858919907,
0.893667752892966,0.144149903535234,-0.689331347864336,-0.610373303072764,-0.204106922367902,
-0.0557060791181194,0.585109579687194,0.710859262384192,0.0174141036808852,-0.466871091388327,
-0.626763671898102,-0.297753001433115,0.040555895564039,0.564626235801996,0.705799286675642,
0.0750613044187593,-0.478737190261705,-0.487080519650291,-0.570108362865829,-0.316410770808425,
0.641599306385284,1.09437775518271,0.677757969589283,-0.279199281909371,-0.768187524460682,
-0.688781445913068,-0.516372624117536,0.53660985088371,0.794345050014078,0.289416759410566,
-0.264440031616523,-0.360220781758186,-0.519053840344975,-0.138096076672349,0.505076752726044,
0.361358239881256,-0.254043907425136,0.341922351004651,-0.16155210041353,-0.619706033381399,
-0.205795751346826,0.414125857617523,0.741315042749241,0.388243292958371,-0.418276395198082,
-0.347489603765833,-0.66652960594701,-0.421345746166661,0.652733059555744,1.08021633725686,
0.205045469739519,-0.47552565817178,-0.78068196906867,-0.653369814013868,0.105357406204062,
0.617608779663909,0.96466240155043,0.405586776519645,-0.637162154413196,-0.873103137539985,
-0.562469972687096,0.0913514879956615,1.02644707066352,0.70368667296402,-0.430787129231878,
-0.0295745418453463,-0.385761921205086,-0.845222508703945,-0.307182015834936,0.575387880592629,
1.43246491181298,0.281966789957515,-0.48486854698586,-0.565262198371098,-0.677067016626925,
-0.0954371440992165,0.769978099273788,0.245450746918551,0.59326599100154, -1.37114452366068,
0.126781856495443,-0.799294739286162,0.288349195211991,0.984132688177628,1.31532130545469,
0.111325864567524,-1.12000149053335, -1.1350497405753,-1.22403679433685,-0.233873883070884,
1.74883652789099,2.2254363701363,-0.437009359474075,-0.849335452553339,-1.4017061496311,
-1.36768245828704,0.0486908476891424,1.84907060854246,1.83084740199672,-1.02442891967816,
-0.93191211228321,-0.182620164153958,1.81844836597859, -2.4140455718039,2.33046604256119,
1.87907927313668,-0.638446963737747,-2.13013114762951,-2.00554982895445,-1.4032225125371,
0.383941458863185, 2.55278816928774,2.77674692359084,-0.338563216543842,-2.26182765765696,
-1.49794650378289,-0.942117640211966,0.441963423519597,2.5573100364143,2.57951630324916,
-0.132936888542228,-2.38582375995109,-3.24965441702695,-0.838541004341834,0.864937380194083,
3.68448023241881,1.19279425696347,-1.21276844975653,-1.85115982588624,-1.6774725265934,
2.37965146711994,3.89626653010402,-3.16392254206833,1.71439013844662,-0.567052451289917,
-1.96110564410559,-1.93004934430742,0.44863743623737,2.48411534832981,-3.42289418528128,
4.00185283424478,2.40125067255723,-2.1944539939102,-2.97263707252501,-2.57558947366834,
-0.516666512035414,2.87868805978347,3.76876793799052,0.687075554312873,-1.66950377764177,
-2.21200902073682,-1.75700315862579,-0.148858200169474,2.73767019853833,2.74046400926481,
1.40897706375589,-2.40852701509342,-2.47917851016626,-1.67476773465148,0.315773403061838,
2.21276273384996,2.52554280945548,-0.415776470298961,-1.07441236085753,-1.75736446513317,
-1.60656964115673,0.232522579977605,2.02611987453222,1.90125649136352,-0.511868541729463,
-0.158687377880881,-0.932134741274215,-0.490616203292175,0.326189896965346)
#Sorry for this big vector, but it's for keeping the reproducibility of my issue
combinacoes = crossing(modelo = c("sARCH","sGARCH","tGARCH","eGARCH","gjrGARCH"),
distribuicao = c("norm","std","ged","snorm","sstd","sged"))
plan(multisession, workers = 4)
tic()
modelos <- future_map2(combinacoes$modelo,combinacoes$distribuicao, function(x,y){
MSgarch_settings <- CreateSpec(variance.spec=list(model = x),
distribution.spec=list(distribution = y),
switch.spec = list(do.mix = FALSE, K = 2))
try(FitML(spec = MSgarch_settings,data = residuosp_confirmed))
},.options = furrr_options(seed = T)
)
toc()
map(1:length(modelos),function(x) modelos[[x]][["par"]])
Наслаждайтесь!!!