'recursively split a dataframe with partykit::lmtree as a stump tree
I am trying to recursively split my data using a stump tree based on the lmtree
function from the partykit
library.
The idea is the following:
- [1] for each variable of interest run and
lmtree
with a depth of 2, store the regression model's results and splitting rules. - [2] for each subset of data created from the rule-splits run [1]
- [3] ...keep on going until there is nothing to split
- [4] return all the rules generated and all the regression models at every iteration.
I have created the following data set as an example
dataCheck<-structure(list(Y = c(8.25182880801735, 4.38905581711409, 4.3723003426479,
8.95352964997245, 3.11700618762515, 3.1034226537366, 7.89646389368191,
6.85501406564123, 9.14109165528899, 1.76168520924609, -0.0186423064878894,
4.05085948895797, 9.3290668201337, -0.688712840604117, 4.15315642195899,
-0.00971512402195501, 5.42811470891677, 2.46276739480922, 8.19194673663205,
1.73449749373474, 5.11176729680431, 6.99704403247248, 8.19840241595093,
3.11060513769226, 7.90068926820612, 2.76471024799475, -1.94707392888114,
8.70720034430486, 1.87038416922914, 6.20663570735997, 4.09386155410738,
3.22531843927543, 0.830783579350356, -1.89145826468231, 4.83659003642515,
8.42672861512832, 3.64564224371417, -1.15773582661539, 9.0284462831185,
8.83097868528031, 6.45672650028379, 5.27430330162195, 1.67266323394736,
9.77833830342575, 5.52833125887379, 6.02558241863665, 9.32123924173233,
9.59814558462498, 4.99387919879268, 6.49466500856929, 7.2896720828623,
1.45305800877985, 7.9206838214584, 3.10094680249827, 6.47011468231269,
4.50777097352803, 2.80590591034666, 8.48494126341584, 5.28136515607325,
7.26296418616002, 7.58618091344727, 0.657159517213753, 4.63042456168453,
6.95119391263247, 5.81217832756039, 6.98278274815032, 1.87540163589012,
6.71792941091317, -1.29432787337523, 7.78940895663812, 7.37649159209526,
0.675702298998388, -0.00368332855764775, 6.73187085031259, 4.90496061288267,
5.9489529474397, 6.38188340303357, 1.49060261962588, 8.02748275174409,
2.68375327263568, 0.536734269942505, 10.9804396086597, 5.77181428022011,
8.02014063211585, -0.154299594612417, 11.5771087779023, 3.15668239446356,
10.9688804025395, 3.02557450708542, 2.13768854535059, 4.9987900600247,
5.77251800446134, 5.69338793536718, 7.48847879852273, 4.61918663507178,
5.31242784333302, 3.31453219290204, 5.43559388358608, 2.72114624838099,
0.283680211264713, 7.32084446410466, 9.36726403837086, 6.28712663547282,
9.68442109468396, 2.1771978188826, 2.91207121946027, 8.62268371794479,
5.41172945001701, 5.38373182957917, 7.95353655623665, 10.4130862095915,
6.25081183779213, 8.54684927325094, 8.17251249575749, 5.72201913971001,
0.981509806109492, -1.23648317875867, 3.86213151478364, -0.0584268109220522,
4.88005249800849, 5.42670918687429, 6.4038980260246, 7.14503570378985,
8.88305747736355, 1.70163134436323, 0.62926133984831, 6.332538349964,
5.85926583090084, 7.60153681164436, 1.18354110441859, 6.21376082513293,
6.72176938214484, 0.327537651877887, 7.43424865766649, 9.5345481861972,
3.36521469337704, 5.15683513556837, 6.05183146409861, 1.47921224995724,
8.14770415530154, 5.5357695364988, 6.37292822354104, 10.4408451690402,
10.6286558881235, 3.11671629644448, 4.33997804421313, 4.27598096655774,
8.81901613584242, 8.48445947041034, 10.4418692374179, 7.52723751645487,
4.9570006189033, 4.23525434076004, 4.61233757658881, 5.97883596964654,
8.92350768169135, 3.36744748717257, 7.5935668820036, 10.1530021272265,
4.44781564448189, -0.249607613297988, 7.71200039180305, 6.06313433331344,
3.51906505864441, 1.03215089097043, 6.90937324325054, 9.12875026148115,
9.21902600933707, 0.0144208736886396, 3.62417570727636, 7.02168510403701,
6.85147195840605, 3.47082453284862, -0.0848457381909239, 8.11569632204266,
7.82069734436052, 6.09482844636063, 0.951714826858227, 6.68025842180694,
0.83503748042638, 7.73552283142719, 0.166199593532002, 5.55737073827801,
9.60315749786249, 2.34268963194322, 5.06130544981114, 8.41498517401806,
6.47923805898893, 3.60160770616943, 1.93479805024185, 5.15513930951948,
6.52441526450235, 8.12845225663792, 4.79311355060987, 0.979200604605808,
1.8268590440232, 9.48019794908497, 1.08410154255385, 9.27908007601209,
8.68019897887756), Treat = c(1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L,
1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L,
1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L,
1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L,
0L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L,
0L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L,
1L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L,
0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L,
0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L,
0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 1L,
0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L,
0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L,
1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 1L
), V1 = c(-0.512224184361134, 0.236874945466597, -1.87889995137573,
0.417180920995462, -0.671381865721995, 0.287167828494884, 0.489380353826899,
-0.0882793575066059, 0.317742648720351, 1.22953756109929, -0.411584735880727,
1.08962488257142, 0.561678151413783, -1.07985068764121, 1.0466704977336,
-0.0292939926335329, -1.19627566198503, 0.129016676567211, 1.01388479182626,
-1.77853019823201, 1.70834544836442, 0.0454885896688708, 0.0522602787427178,
-1.34661181816343, -0.229192725217181, -1.29094683899917, -1.80631617970927,
0.588982048814097, -2.51335929144276, -0.33718609396271, 1.23629555181408,
2.21920877408719, 2.00276718551955, -0.266133894204283, 0.0463897977044271,
-1.28942121425525, 1.84913424106894, -1.17352670639698, -0.00604822691863582,
0.269385373823645, -1.25222659166203, 1.05795208283273, -0.841186560805508,
0.53575408754712, 0.57910047820463, 2.31402998804474, -0.0719716684180724,
-0.169879514441182, 0.898919081668701, -0.499443728528926, -0.261783645131221,
-0.997732870280807, 1.86418519053334, -0.50874295530776, -0.793740962799156,
1.5613690865751, -0.298506285466049, -0.495281299842039, 1.00812101114208,
1.11685818214117, -0.505794287012676, -1.40273953487652, 2.06123765465717,
0.368184101316567, 0.420456807959395, 0.00599354238077596, -1.1398273773676,
0.783863027697299, 1.12470121951805, -0.708307856061111, -0.0906433568083879,
-1.25738689932716, 1.26095202154429, 0.301138661248989, 0.045843974918831,
1.03024799700031, 0.949019551531677, -1.39380451586336, -0.528730424300515,
0.0369965771020032, -1.18171381017, 1.49979891866957, 0.15676325449813,
0.402736509896824, -1.03099304732686, -0.48056251405081, 1.11384829811643,
-0.218085210734798, -1.25125131426365, 0.0768043975792328, -0.0859557017662965,
-0.634842234143288, 0.605421881599738, 0.50271688606003, 0.0544965175079416,
0.254883531339007, 0.724258364968426, -0.870650774526025, 0.253029590313987,
-2.0986762438242, -0.0240824333410968, 0.174296097882529, 1.33359836459227,
-0.346308257138143, -0.563106795944062, -0.260627921956495, -0.268980353567251,
0.185828017673224, 2.01233897740523, 0.16357851917081, -0.776176022489553,
1.89540402776715, -0.6615289177093, 0.131681717006018, -1.21145287910156,
0.932690336174062, 0.585783815391921, -0.571349415819544, -1.42497017096838,
1.10888317549818, 0.283311326326235, -0.414969695561733, -0.939616281339742,
0.110492068893627, -0.746014266098047, -1.07991576386102, -0.432025117819907,
0.830391245203387, 0.164428464782244, -1.51572185585929, -0.421075043960813,
0.23777931298336, -1.17495654928808, -0.100265604687606, 0.435095241672069,
1.04859111394976, 1.21578829328542, 0.444072958180845, -0.694238471712357,
-0.739705032889642, 0.389192710410273, 0.0937806873880687, 0.462085160399263,
0.156974707854046, 0.266963975753482, -0.29707267641139, 0.354079230828466,
-0.0971026416271703, 0.673191241548993, -0.530180479720613, -0.66580050863429,
-0.26721804945571, -1.92264958452522, -0.185099340678194, 0.485163365007242,
0.638813737506359, -1.23177250938818, 0.439975847642966, 0.716863270101894,
1.02688539285126, -1.72807904491845, -0.786367064946864, -1.68678578842494,
1.22942039351412, -1.70618655825219, -1.22990651413465, -0.324950136558707,
0.624858269195195, -0.828068321649018, 0.947486788826015, -0.108630973427409,
-2.40250570840112, 1.44771355228476, -1.24199088371159, 2.0917304355165,
-1.77311409695832, 0.0954605832796433, -0.740994505927424, 1.61778488533527,
-1.40573129075368, -1.00370633649212, 1.38162617605784, 0.884164534682012,
0.267169875178434, 1.9057917321543, 0.206145940614585, -1.18383159869387,
0.481036958396535, 1.31027045373907, 0.358433947320302, -0.29237023947135,
0.282768651359503, 0.781651423368563, -0.138721715418646, -0.381835428164467,
1.70428320688559, 0.106774777090092, -0.843309921038093, -0.143242083596126,
0.698361373594494), V2 = c(0.380304375791841, -1.02059433700002,
0.250167152859078, 1.19197310535082, -0.665212884381305, 1.55101992940286,
0.0677143288757891, 0.475101191357759, -1.03947212891308, 1.33093806534135,
-1.53024562146233, 0.264721134679345, -0.850689035040517, -0.957133686252826,
1.87696618529297, -2.15775600281697, -0.338372884575403, -0.65534425003225,
0.000341190135952188, -0.325103882313815, 1.35236410406428, 0.71204447686019,
-0.832202393832718, -0.104160072249246, 0.548770775839287, -1.02555695096039,
-0.545265782667373, -0.0716200430572802, -0.416112028859363,
0.227288103381536, 0.267105299509607, 0.797786411399094, 0.588039577794279,
-1.35531243965783, -0.191960436342607, 0.802287953041775, 0.688984565302114,
-1.38282061870725, -0.145188087012673, -0.164918559770532, 0.151817539241955,
0.579936723341327, -1.82374499984194, -0.770579614791879, 0.169550022479414,
2.19606347734814, 0.680134653059498, -0.185915544520268, -2.21291386243485,
-0.902294133219532, -0.387497535879364, -1.67055198613076, 1.96212040244854,
-0.429372635727511, 0.443728048852803, 1.36782981889734, -0.860161707413864,
-0.103726927927917, -0.5584223108133, -0.381818274563981, 0.750650936355598,
-1.22126613031886, 0.753740039841989, 0.177762299660818, -1.55651654564794,
-0.558883115400796, -0.100743184349595, -0.430155955072168, 0.460282011684617,
-0.624858164490752, -0.516311949090538, -1.5464191807631, 0.238413830662162,
1.03467342924797, 1.23386662516284, -0.221492621473667, 0.762834477286496,
-1.13424883071864, 1.19791988030916, -1.96809368817677, -1.31690072853851,
-0.929006941060101, 0.45044390112418, -0.0781154774982165, -0.974521612609975,
-0.291613999662158, 0.30103016880007, 0.289110599271069, -1.05887356328042,
1.09274065862511, -0.529059084625542, -0.422764309913228, 0.203616384199572,
-0.517419458890544, -1.08577067823996, 0.785820810897911, 0.842995636719097,
-0.312364416609285, -1.70091723112381, 0.0837066866207621, -0.0770688115484564,
0.367324572148102, 0.204986234718808, -0.705779592443223, -0.473927120687011,
-0.681169822342536, -0.0839976927364287, 1.42617208418436, 1.51657444801786,
0.41486872393426, 0.265969808633188, 2.42076884070737, -0.0277907989746658,
0.523682618684171, 1.13269655038589, 0.808748497940052, 1.35012516140403,
-0.293212826343904, -0.586144534512143, 1.81611174748987, 0.380351413074197,
-0.554025114959216, 0.673033861638527, 0.307174932654839, -1.93001400490438,
-0.611789590721301, 0.00959244853917548, 2.03656896195516, 0.212605689200221,
-0.121603823807969, -0.658135321474302, 0.353764606988243, -1.04010640644265,
0.472020670742881, 0.225959603028341, 1.17400555855189, 2.06009007647237,
-0.525975289534454, -1.39925266227898, 1.5766665366283, -0.248342407094795,
-0.446498843606279, -0.624450198509769, -0.644724971787901, -1.4476249701376,
-0.901573388502368, 0.913374488495839, 0.215094167667878, 0.157170827624439,
0.680997208045578, 0.213837592170229, -0.409317630992794, -0.938757173909534,
-1.05353825972851, -1.13217103821246, 0.0862958849216095, 0.38193869141594,
-0.591354215219487, -0.368223411698507, -0.398375915714521, -1.2384244717298,
1.13224055752887, 0.735423348602136, 1.63762271072962, 0.184022782526676,
1.41794424255927, 1.35601921685383, -0.979204040600424, -1.12003486941702,
-0.0795143427554575, 0.969471073124747, -1.65077833214048, 1.13741111479043,
-1.23718181991181, 2.16939328052426, 1.58458658801677, -0.927557956775925,
-1.7506632828746, 1.31612707879696, 0.12180996377485, 0.0865113240561419,
0.45518791646425, 0.407197441111549, 0.0360385039559024, -0.0232066075820815,
-0.459695544381486, 1.17775113237394, 0.268380457018411, 1.61588776085417,
0.177635726343091, 1.17129050159447, 0.550330935173755, -1.02154802513188,
-1.3702281008687, -1.28085984249659, -0.396017227997605, 0.162060101733873,
-0.966018134129389, -0.304971980458869, -0.444854699256305),
V3 = c(-0.783029123458224, -2.86136360818513, 1.698849090337,
-2.95774523366051, 2.4909086168639, -0.627457984911746, 0.110980388892042,
-0.667847373549883, 0.495514590439687, -0.367782888805159,
-1.20225187920879, 0.362839428739434, 1.97856957054665, 0.41434488687775,
-1.64576493811649, 0.0661140240065885, 2.01134047823101,
-1.91168520346169, -1.41034016066642, -0.366939954574131,
1.00653172915348, 0.293594934133422, 1.64226196977588, 1.07900183932166,
-0.503082795576658, -0.788789148253884, 1.41312694932518,
-0.572848859920792, -0.13404183649235, 0.0069044139405835,
-1.56816266045955, -1.04010403552982, -1.06122968960302,
-2.00861746324152, 1.86252857971055, 0.564497896096412, 0.757439916697076,
0.902894839665887, 0.744838570387456, 0.322057380637828,
0.333586763094688, -1.57548558313778, -0.122309467511797,
-0.635396971662784, 0.628077927036663, 0.25844546745181,
-0.720709239291685, 1.04522293236851, 1.40954782696134, 0.701694421922767,
1.93170952412344, 0.690789230506277, 0.23071554842768, -1.29004679417293,
-1.94265879416657, 0.267161546334048, -1.04858479513342,
1.46880868203975, 0.275013030943361, 0.0966910917048534,
2.13082142480503, 0.856236705840271, 0.490901610574834, -0.0926156270728089,
-0.513605700130283, 0.91479683773873, 1.03832066326752, -0.428142251149754,
1.70288136899438, 1.65909097934217, -0.392117608602941, 1.25494215592197,
0.180615542520098, -1.56472920143675, -0.951381883242425,
0.466146905847187, -1.96152286613212, 1.72021205355769, 0.0851020317209986,
1.44883591505909, -0.103027381582208, -2.28498457332985,
0.278397909773373, -0.395165512718835, 0.694849184540884,
2.32544061976475, 0.0071225634910954, 0.623521431285071,
-0.396377229420653, 0.490069868896261, 0.799060528165712,
0.859716069573166, 0.870529746266426, 1.18543189697643, -0.169424850203802,
0.266546084203442, 0.346814297513414, 1.60326038327794, -0.141857970282539,
0.670914924191492, -0.931545980199446, -0.537316060218973,
-1.03665105210925, 0.950586460248348, -0.620522045551402,
-0.648653515651034, 0.0137635082594915, 0.0804518507120756,
-1.43822292373898, -0.439092429665076, 0.232053822277543,
-0.017980430930425, 0.841394405849294, -1.25034715693627,
0.521124729454402, 0.895707508220408, 0.215826276359757,
-1.99371074325881, -0.0175652663116899, 0.0101455125548749,
-1.06047943612771, 0.698675584642549, 0.595895126673012,
-0.980942140021254, 0.506958845680157, 1.58125228414793,
-0.529301085889923, 0.766037245403873, -1.94480369379801,
-1.52374281308753, 0.135918050922047, -0.667181951731405,
1.44318533889675, -0.328741335874945, -1.4955553201827, 0.565707746145783,
0.316263726768616, -0.994401295070257, 1.17210919346186,
-0.758550484028855, 1.76625177984024, -0.782158754650981,
0.397054130681855, 1.1672023277953, -0.626933600815539, 1.03727510699083,
-1.31780346518455, -1.92555713053862, -1.06281530463427,
-0.38968848830501, -0.235308441138616, -0.955133579576862,
-0.109096171177156, 1.80283683873315, -0.317127357450067,
-0.195896930662445, -0.230136739758576, -0.911430530226401,
-2.0650912198049, -0.0479129284084004, 2.66733363837803,
-0.234894870041216, 0.514345275702296, -1.94362698963394,
-0.17654995423815, -0.3013216838135, -1.25564920454275, -0.934948411241163,
0.97292031482541, 0.487245786628467, -0.495977457144424,
0.628817502189708, -1.17164575410945, 1.44515144111588, -0.160007391048236,
-1.1084598953987, -0.988268517824923, 1.68845209494322, 2.03448636458376,
-0.381614681170706, 2.14839995565878, -1.33328503453941,
-0.754019101136778, -0.0118992804530879, -0.48985470951793,
-1.21710580046317, -0.130039421347802, 0.639332128972829,
-1.52367289107621, 1.77820266716132, 1.61348266764632, -0.455025312061264,
0.816630355360007, 0.826673907029162, -0.435350162979888,
-0.0777859859504891, 0.050285728821641, -0.91649317704683,
0.592648726301327, -1.83943467876548), V4 = c(0.496183138622778,
0.843380288592097, 0.598852220056031, 1.16553502750529, -1.88761557895014,
1.0626794252746, -0.458430499749847, 0.54972756140618, -0.841905366037581,
0.605311075971646, -0.272630880028979, 0.181479070624168,
-1.89191480503641, -0.366157839524914, 1.14873475665488,
-1.26474522348741, -0.323908256070193, -0.25056671340487,
0.314353478689016, 0.807496754455645, -0.504654595871874,
1.03057525264001, -1.05419689039865, -0.142281911193005,
1.12486762874026, 0.457092486405227, 0.153770400655579, 0.0398154292023269,
1.89076452437969, 0.709270355860511, -0.462091162652255,
-0.850099319715336, -0.579658018507973, 0.749735038915879,
-0.375127145387184, 1.66552691269647, -0.555837765074476,
-1.4448089567226, -0.176595317847086, -0.620314106919008,
1.35636650884363, 0.259684944730687, -0.966630909841737,
-0.222690383517399, -0.892145053129533, -0.0456450942257557,
0.945216588734501, -0.434501291999419, -3.29760597174868,
-0.240674057809983, -1.34536116107364, -1.01525526084012,
0.0970254385313278, 0.671033494268336, 1.98994350716475,
0.175738027241526, -0.400083269708577, -0.871663993352983,
-1.65628220870012, -0.855532225763136, -0.0996243647479452,
-0.977583749905298, -2.25401990484524, -0.432391696967047,
-1.3158643871778, -1.1192680316909, 0.571081240547906, -0.607892593042822,
-0.281595172778716, -0.421225933815705, -0.0444109132321077,
-0.817988793696439, -0.504696559590155, 0.378022531682079,
1.83777826299318, -1.08795411181554, 0.752014030776772, -0.909188603027526,
1.05899412442052, -2.46632140331021, -0.454137339615655,
-0.906720015246957, 0.344757349737868, 0.240759494928484,
-0.548356818290179, -0.72597907334518, -0.331873922756355,
-0.65374321081531, 0.26630023619897, 0.703834537333475, -0.392869657192858,
-0.60203598801419, -0.456073505553343, -1.56598580002795,
-1.35495621459995, 0.493712154046526, -0.105499422309029,
-0.507870359416919, -1.67778621231626, 1.12536992153463,
0.393984837637675, 0.903911205052433, -0.992372351451294,
-0.269749776329061, 0.712394297240526, -0.040869348501661,
-0.00466965427743419, 0.808793054336477, 0.740536318954486,
0.533688798112223, 0.940410671125443, 0.316389437518927,
-0.27262129605243, 0.918014179899982, 1.95623086543066, -0.628831783455111,
0.924236219567331, 1.55124348624486, 0.749874295417138, 1.02328692025501,
0.270435253173243, -0.434591296205896, 1.06617656435964,
0.0417845612243438, -1.41135272426531, -0.734118465675784,
0.760275152691908, 0.524197304806363, 1.35758203983865, 1.77991978503334,
-0.35015509896571, 0.698064596060179, 0.367728355115782,
1.5491108092072, 0.575815882695618, 0.0115987689700014, 0.826517916126569,
-0.49250383359976, -1.16764045714146, 2.23711324350063, -1.11201072194231,
-0.20218119457645, -1.03095693217637, -1.12750113672542,
-1.57297500003518, -1.14049540635204, 1.47450452475808, 1.83660497267165,
-0.0646245564388892, 1.07927342296938, 0.647278397330584,
0.160456229899729, 0.294138512293373, -1.67447206458372,
-1.50089887215231, -0.282630129807303, 1.26170371199036,
-0.849109449647251, 0.625711240973577, -1.4510398204988,
-0.309264689008217, 1.27706813103201, 2.50197336690919, 1.51800029416214,
1.70025260848675, 2.40703136034766, 1.80905588964078, -0.474157610739915,
-0.501537306810103, -1.1736343999277, 1.37285094193152, 0.698322220043475,
0.771346551270717, -0.887473168463408, 0.651520294722059,
2.89432544329804, 0.311216625695187, -1.30372659600851, -0.400581827065282,
1.33964782513989, -0.843961884165941, -0.0668058114188016,
-0.190830657251782, -0.561116870171686, -1.92472393237171,
0.478199276886028, 2.32596631481254, -1.22740489308766, 0.956632976969199,
-0.735018453180533, 0.77879526123063, 0.13987244988313, -1.91402362352957,
-1.35992322311105, -0.207781871381667, -1.77869657196406,
0.141920655454794, -0.724285194394835, -0.771690640531079,
-0.731502588334428), V5 = c(-1.95720247261132, -0.624992393849862,
-0.812932158707034, 0.63698839485029, 0.749168853674764,
0.0175594195045844, -1.83950382958848, -0.335380804753572,
-0.491220035676555, -0.191134566848763, 0.49896461495255,
0.54413665076851, 0.878425880258598, 1.23549074923161, 0.123799989937528,
1.65085627858582, -1.00835439789606, -1.82599630942863, 0.90870725879643,
-0.807308916487168, 0.785331627799637, 1.66353618267387,
1.5889984722748, 0.403974936120788, 0.208999570978263, -1.29770685274578,
2.51510983185051, -1.1718804166854, -0.266130565223027, 0.0628651163617692,
-0.561401581830903, 0.693647839847029, 1.4155500148167, -1.89754279624444,
-0.106226358444514, 0.142966964132926, -1.12760138212142,
0.17472567410764, 0.541524331447821, 1.38762371090265, 0.225081646765629,
-1.15860177521253, 0.25444959262842, 0.565271472186904, -2.22942812821164,
0.579469243130158, 0.807907855559215, -0.432984605747263,
0.757096206867329, -1.33177934674227, 0.374998391824253,
0.608710926979125, -0.792398547870407, -0.228626388344189,
-1.70872106615463, 0.409579616486617, 0.246548691814628,
-1.14417008876865, 0.0627950281049514, -0.439087859672742,
0.314713430040702, -1.41839359403735, -1.21303230088663,
1.83138366932648, 0.0120792327579562, 2.16238583829647, -0.0758286379479119,
-2.51002046009773, -0.486132226522798, 1.09863681928542,
-1.30297321371365, 0.458110735611193, 0.764843489048324,
1.39769455790524, 0.61916350603702, -0.814119466631296, 0.419814569271675,
0.224977136567116, 0.0366613038208614, 1.31345858544845,
-0.0270617730206812, 0.884508188399017, -0.388035069414317,
2.23591675521183, 1.50037471568071, 0.75634359862639, 0.392384459670574,
-1.1319833967851, 1.77854023696154, 0.815354762037834, 0.355924060511744,
0.844618070998363, -0.00428159218632944, -0.213499391196822,
-0.516842668488334, 0.825001849565069, -0.424190656813675,
-1.18458492299936, -0.185577186356483, -0.667207823896983,
0.294943388825459, 0.421330379668701, 1.04231718753599, -0.0289456835010432,
0.0639093854009601, -1.11882662659817, 1.55983238137046,
0.0688071999900611, -0.956144491830392, -0.892050376142865,
-0.29288621765089, -1.24307138201574, 0.367301785174384,
1.18545406261117, 0.480255867464714, -0.273984146435023,
-0.806520230474475, -0.972207107788592, -0.572050250841124,
0.206429031606454, -0.652122820044276, 0.306241101298542,
0.350721886378023, 1.56984270102165, 0.247456999964226, -0.278090695032295,
-0.236143062858013, 0.262861521785325, 0.376000420955182,
-0.457496011755383, 0.847479641874746, -0.203950052193078,
1.03196308954371, -1.18412103424102, 1.62509863179956, 0.568042637472017,
0.480620697492834, -0.773144582811822, -0.337229819612409,
-0.581147020848617, 0.229603737912756, -0.596095812505015,
-0.555430381740764, -0.78423090297005, 0.47345493765302,
0.537378614930491, 0.0715747883730107, -0.269204731942544,
-1.16822130056704, 0.780969368903425, 0.794447786588496,
0.657806692113609, 0.519260725019535, 0.741934551336921,
-0.254958125216741, -2.99019450767595, -0.657516955680485,
-0.0170304987043617, 1.11659845335596, 0.360330874506391,
-1.64643490441176, 0.222628826632865, 2.08372365614416, 0.00145049897442939,
-0.381052475055785, 1.67399131474625, -0.495295797100106,
-0.845479464278401, 1.76699507936435, -0.222680325841757,
-1.70724299417978, 0.0120613536578708, 0.312324996040137,
1.03819089603973, 1.69773201772374, -0.592815940688807, -1.0000620909457,
0.807857475821365, 1.25701492981885, -0.0610481519298042,
0.0983016160558342, 2.1130776772487, 1.95059241205574, -0.180791432861856,
0.36745443322943, -0.311097519256647, 0.0303783361871745,
-0.828412237444424, 0.0109501198374983, -1.13104256971894,
0.101071828992581, 0.193297914069868, 0.984668999699508,
0.539394038270913, -1.54305200756413, 0.363236637675765,
0.857345602080481, -0.755365345328581, -0.790846907784197,
-0.326746686522603)), row.names = c(NA, -200L))
I can easily run the first loop in on the variables and extract all the information I need after the first iteration, but I do not understand how to make the function recursive.
library(data.table)
library(partykit)
library(jtools)
setDT(dataCheck)
first<-lapply(setdiff(names(dataCheck),c("Y","Treat")), function(x){
tr<-lmtree(as.formula(paste("Y ~ Treat | ",x )), data = dataCheck,maxdepth = 2 )
if (length(tr)>2){# if it is less than 2 there are no splits on the variable x
rules<-partykit:::.list.rules.party(tr) # get the rules
subChunks<-lapply(rules,function(x){ # parse the text of the rule and extract the subset of data
DataChunk=dataCheck[eval(parse(text = rules)),]
})
#extract SE from ech regression
allse<-nodeapply(tr, nodeids(tr,terminal = T), function(x){
seIn<-summ(info_node(x)$object)
seIn$coeftable[2,2]})
#wextract terminal node sample size
allw<-nodeapply(tr, nodeids(tr,terminal = T), function(x) info_node(x)$nobs)
#extract coef of the regression
coefz<-coef(tr)
lapply(unlist(subChunks), function(dt){
lapply(setdiff(names(dt),c("Y","Treat")), function(x){
tr<-lmtree(as.formula(paste("Y ~ Treat | ",x )), data = dt,maxdepth = 2 )
})
})
}else {
NULL
}
})
Solution 1:[1]
I'm not sure what exactly you are struggling with because you seem to be doing the right steps. I would just recommend to use more infrastructure readily provided by partykit
.
Most importantly, you should not parse the text rules but make all computations on the partysplit
objects. (See the package's vignettes for more details.) As an example:
tr <- lmtree(Y ~ Treat | V1, data = dataCheck, maxdepth = 2)
nodeapply(tr, ids = 1, split_node)[[1]]
## $varid
## [1] 3
##
## $breaks
## [1] -0.8280683
##
## $index
## NULL
##
## $right
## [1] TRUE
##
## $prob
## NULL
##
## $info
## NULL
##
## attr(,"class")
## [1] "partysplit"
Based on this object you can do various kinds of operations including the computation of sample splits. A convenience interface for this is also the data_party()
function that can extract the data associated with certain nodes in the tree:
d2 <- data_party(tr, 2)
d3 <- data_party(tr, 3)
dim(d2)
## [1] 42 4
dim(d3)
## [1] 158 4
However, this only extracts the 4 variables from the model frame of the fitted tree tr
. If you want to extract all 7 variables from the original learning data, you can predict the node ID and subset based on that:
d2 <- dataCheck[predict(tr, newdata = dataCheck, type = "node") == 2, ]
dim(d2)
## [1] 42 7
d3 <- dataCheck[predict(tr, newdata = dataCheck, type = "node") == 3, ]
dim(d3)
## [1] 158 7
And then you can use these data sets for estimating the next tree:
tr2 <- lmtree(Y ~ Treat | V1, data = d2, maxdepth = 2)
What is the best way to repeat this recursively and store the results then depends a bit on the way you want to combine the splits later on.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |