'Parallelization in R (tutorial)
I've got a question kinda basic. It's the first time I use parallel package in R
I'm running a function you could find here. Well, this is not exactly the function but is very similar.
So my function is running on the WTI oil price dataset (see down below) and since p_max and q_max are 12 it takes like 3 min on my PC.
So I am trying to parallelize this process:
this is my code so far:
n.cores <- round(detectCores()/2)
cl <- makeCluster(n.cores)
clusterEvalQ(cl, {library(dplyr); library(forecast)})
clusterExport(cl, c("wti_", "which_arima"))
start_time <- Sys.time()
a <- parApply(cl = cl,
wti_,
function(x)
wti_models <- which_arima(x,p_max = 12, d_max = 1, q_max = 12, expl = F))
end_time <- Sys.time()
stopCluster(cl)
end_time - start_time
this gets me an error
So this is what I know:
- I am creating a new environment only with
forecastanddplyrpackages - In this environment I am using the
wti_data frame and thewhich_arimafunction.
The desired result as you could see in the link is a dataframe, so my question is: how can I apply the function over a dataset, but using the parallelization.
Like how do I say to R, "apply this function, with this data, these libraries, and return my dataframe"
data:
structure(c(3.59131383137997, 3.58693674637687, 3.50782953019081,
3.43581773710991, 3.40722540614203, 3.42200436261882, 3.48904753923734,
3.47452426198165, 3.46675753126697, 3.46568380114955, 3.49807117713751,
3.51687930816857, 3.46335166796688, 3.41285698665032, 3.3790196588941,
3.43353497596237, 3.38675587458917, 3.41877857802065, 3.4351468995404,
3.43645947831124, 3.40956358119916, 3.38513513606487, 3.40276099733318,
3.40963956656788, 3.42328064788982, 3.44117985614171, 3.44875660306423,
3.43353497596237, 3.40231141639508, 3.3889354116122, 3.34201790329997,
3.34511517255433, 3.34481173886808, 3.32549686098232, 3.33946614154227,
3.31876553349471, 3.2719744390575, 3.34603588846443, 3.36637904646734,
3.37366507547086, 3.30585112515203, 3.28962252754128, 3.27225558783058,
3.28979015878231, 3.2868499768523, 3.33280253315514, 3.39774845550964,
3.34281846838198, 3.15856777800092, 2.77168686901602, 2.56117445699168,
2.56878239388438, 2.72383440052745, 2.58909523968371, 2.42867520384351,
2.69982721445759, 2.67193554872065, 2.67205375315245, 2.72614311872445,
2.82219484828974, 2.95059244815988, 2.91370487551786, 2.93858161230468,
2.93740122860658, 2.95149846325297, 2.98463470799191, 3.04069683959108,
2.97765699046636, 2.93763871445365, 2.95889654733423, 2.93807652956505,
2.88480844101773, 2.86942904105267, 2.85571761786751, 2.80852848405648,
2.8935585907424, 2.84269560340417, 2.79881577066578, 2.72282979080091,
2.71603502468445, 2.63791866344239, 2.59536113236419, 2.64062094528629,
2.82526704732671, 2.91780262533689, 2.91651781689448, 2.9913643118826,
3.0536937288849, 2.98445455093627, 2.98213782779332, 2.96514325822283,
2.89244417511265, 2.94275381512042, 2.97141235526606, 2.98724555337152,
3.08337497999601, 3.14759071936976, 3.13289696812015, 3.03905214266832,
2.93471666323591, 2.90128976767023, 2.8107562110377, 2.90783591296553,
3.27335143243915, 3.48472208624227, 3.55142983569527, 3.47663084589964,
3.33547898589394, 3.23423411107465, 3.06018030053868, 3.01423697354859,
3.04650806922403, 3.04258229255412, 2.99208815864649, 3.04771441224973,
3.04928664660562, 3.05142517042555, 3.1169976722344, 3.11951352194927,
3.00707722594537, 2.96128773727663, 2.98175833876288, 2.96532117334658,
3.0197098760693, 3.03074783490711, 3.09323187529891, 3.0616035244104,
3.03068145877458, 3.05371088570641, 3.04800480074745, 3.01464452755963,
2.99937500558328, 2.97449796901343, 3.03554851334902, 3.03659815370675,
3.0221759685645, 2.98195767073768, 2.93870826986599, 2.86679047972241,
2.86234614395537, 2.82942512981342, 2.86936310091189, 2.8199618739421,
2.77816257813924, 2.73548106854293, 2.72856244238227, 2.70795757728122,
2.80272019032947, 2.86822890540934, 2.93347265581204, 2.96006710519097,
2.88160396055978, 2.82656389753239, 2.84142832358034, 2.89747555394281,
2.87668303357381, 2.91724691540316, 2.95671039989371, 2.94344780195585,
3.00076613908048, 2.96379370011001, 2.90417628632361, 2.83960873830006,
2.86012144893326, 2.85926128609918, 2.82313164392855, 2.88020011734877,
2.96955328218305, 2.96606354895414, 2.98543577920877, 3.07562984880824,
3.15570736118902, 3.03455012085685, 2.99554751470324, 3.0435097809677,
3.06165227245572, 3.14193917796638, 3.18716548045293, 3.16534947422127,
3.26569653678997, 3.24350276497619, 3.12060453015829, 3.0531677335756,
2.97682814858604, 3.00688440226154, 2.92397891361979, 2.94209486278654,
2.94852384019642, 2.94200037294792, 3.02516530877763, 2.99127781689946,
2.9294612389212, 2.83139712537276, 2.79796201081782, 2.71543806099047,
2.7407142468292, 2.6875866101351, 2.60423924740296, 2.62901526614014,
2.56740818145639, 2.67394022697303, 2.63930867205784, 2.56343130659516,
2.45978922964202, 2.55235919157821, 2.52305899568833, 2.70863993827665,
2.86457105386915, 2.86261091310512, 2.87064745175996, 2.98121592968395,
3.02833128142963, 3.13942766555372, 3.09041079353602, 3.21442136821367,
3.29527067657245, 3.33321806429822, 3.41425820111181, 3.42204439492442,
3.26347876143116, 3.347641908834, 3.44778510690982, 3.38228340695309,
3.4158576241713, 3.49022619861987, 3.4685900443404, 3.53868951455743,
3.38057910897592, 3.41351461129283, 3.4240289650859, 3.32815366177433,
3.32284599134973, 3.33998170424571, 3.30277100038479, 3.25697583739984,
3.28416630900999, 3.22973411077222, 3.06968747828722, 2.97658284762748,
2.99486130729813, 3.00742083087148, 3.06857117342194, 3.21715730661809,
3.2789612975563, 3.28371073536068, 3.22541728419383, 3.27500077441177,
3.31732265679795, 3.35736068318668, 3.33283267360966, 3.27049196549064,
3.41631064824351, 3.52240767637354, 3.61534075809335, 3.52954642796643,
3.35163665031884, 3.32314665714096, 3.41099388869206, 3.40786497905298,
3.42461901851098, 3.30951378818839, 3.38255745851344, 3.43852131426105,
3.50368460070908, 3.56091764269564, 3.58492322635242, 3.62714661280319,
3.61447455478338, 3.68220458841285, 3.62480591875187, 3.69120858563579,
3.77693846464848, 3.79435311491179, 3.94561880187821, 3.88202371197633,
3.80061460739448, 3.87374132950349, 3.90774155853708, 4.01561092655659,
3.98114594176238, 3.89451764244874, 4.01868127043691, 4.05385010954482,
4.14567914885236, 4.14985582914602, 4.10367767174555, 4.06687407130807,
4.11863663720755, 4.20922906995073, 4.15822897798113, 4.16489749696743,
4.25162605091732, 4.24796372995892, 4.24812466259185, 4.29150230020906,
4.2630054327201, 4.12327520054203, 4.04569209767451, 4.08131048141254,
4.16122019539675, 4.02084951107286, 4.11908513961168, 4.12784213744363,
4.16944548583258, 4.13649191891535, 4.19841321547244, 4.28797065568862,
4.25388037613002, 4.34807914470834, 4.42392192162843, 4.55286074143255,
4.54887319905847, 4.5597691897791, 4.59523020910027, 4.68196749724852,
4.73555825781743, 4.81464312907837, 4.88370149630503, 4.87517492444396,
4.73107073687703, 4.61100598816616, 4.3094825827762, 4.04975401011471,
3.75832555503529, 3.75885483169272, 3.7047332255135, 3.8846870473905,
3.91975172914305, 4.06587924399579, 4.22947702327889, 4.14275318357282,
4.23549897665209, 4.20773532115613, 4.29842497077528, 4.35820833774186,
4.34473021442986, 4.38868400894379, 4.37347426469321, 4.42105341418424,
4.44804171845561, 4.28663516918525, 4.30859773778422, 4.3173240354623,
4.31059519857912, 4.28859054681792, 4.37618571675163, 4.43518826020675,
4.52438659257285, 4.52064662340031, 4.53192251707477, 4.65743120826524,
4.71166719869542, 4.60414256551792, 4.55339702945559, 4.55985555753629,
4.43005081750458, 4.41664247367781, 4.42975186247436, 4.57754526419444,
4.62480609037661, 4.63546694284186, 4.66438801428565, 4.68837471206333,
4.64902787588347, 4.53674738508635, 4.39749965456486, 4.45820582366945,
4.51644331685982, 4.515907622083, 4.46512546285458, 4.46376481745648,
4.51388462404826, 4.57861240050054, 4.59434364751533, 4.55517543186956,
4.53355114792002, 4.53755358906122, 4.54855862986897, 4.63304546972625,
4.6405546157846, 4.63299082099574, 4.58085459388592, 4.54432073943083,
4.61755223285532, 4.57983298789016, 4.64976302955103, 4.63437124427453,
4.63731190211323, 4.60982471864015, 4.64264901041142, 4.61613606110031,
4.54027309727139, 4.50218653982855, 4.40625994180295, 4.32979382573924,
4.11603648165941, 3.88330671224587, 3.96146854482242, 3.89012438371581,
4.00865426030002, 4.06832867298344, 4.07741121519592, 3.91185269043218,
3.72991337610464, 3.78383706950744, 3.80367233828873, 3.75576253590561,
3.65121682739862, 3.47868744897561, 3.45143296652514, 3.65503237854251,
3.72415106324801, 3.83061175389603, 3.87311075168319, 3.78173952982106,
3.77306583078007, 3.77832133572214, 3.88051312207398, 3.82081322094682,
3.98553794729897, 3.98843449509049, 4.01513010556259, 3.92710476842519,
3.94455649433009, 3.86778919512713, 3.79683870390584, 3.82466269416906,
3.84380020173594, 3.87584146117727, 3.91343871827202, 4.03845560314504,
4.0935099318552, 4.18114436146966, 4.16719192787768, 4.16283516968298,
4.20604659334379, 4.23443488412323, 4.19882940452703, 4.24241357979034,
4.19133502159757, 4.21871499991311, 4.22984507672438, 4.03880858585256,
3.92490130849051, 3.96940094923682, 4.04374304044607, 4.08654312078282,
4.16840485009025, 4.09447286457478, 3.98790356609508, 4.0341224802885,
3.97639424075332, 4.00939793877714, 3.95930614779089, 4.04566696145638,
4.12510760047058, 4.07956358085351, 3.95988657890026, 3.42070660208276,
2.81612685060959, 3.33823253465507, 3.63185545042593, 3.68944560656258,
3.71817890679241, 3.64605336714439, 3.64775241750963, 3.71757173717322,
3.88531280554538, 3.98059581620004, 4.11587323591221, 4.15628091394419,
4.13400107484973, 4.16337807059311, 4.25442327230134, 4.26502445234263,
4.18750359928602, 4.23776050752447, 4.36908452896118, 4.37328735734901,
4.30421888163429, 4.44771621423169, 4.55627784876546, 4.75090506053049,
4.7389429012291, 4.70472426847878, 4.69593575555166, 4.65481860133761,
4.60670337428884, 4.56234409901854, 4.53504073166887, 4.53416310882428,
4.53391160984426), .Dim = c(492L, 1L), .Dimnames = list(NULL,
"x"), .Tsp = c(1982, 2022.91666666667, 12), class = "ts")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
