выполнять расчеты над списками объектов XTS и сохранять результаты - PullRequest
2 голосов
/ 29 февраля 2020

У меня есть несколько списков, которые я получаю по линейной регрессии. Список coefficients для одного из наблюдений выглядит следующим образом:

             (Intercept)              x1
2019-09-03            NA              NA
2019-09-04            NA              NA
2019-09-05            NA              NA
2019-09-06            NA              NA
...
2019-11-25   3.022396191  0.000056017622
2019-11-26   3.886149366  0.000008555377
2019-11-27   4.431671529 -0.000021411006

Я бы хотел lapply просмотреть эти списки и сделать следующее:

  • Взять exp из столбца x1 в списке coefficients.
  • В общем случае с помощью списка r.squared, используя столбец R-squared
  • , сохраните результаты в объекте xts с имена столбцов, соответствующие XLE и XLY, откуда были получены расчеты.

Я пытался сохранить точки пересечения и склоны как отдельные объекты.

Intercept <- lapply(lm_model$coefficients, "[", , '(Intercept)')
Slope <- lapply(lm_model$coefficients, "[", , 'x1')

Затем попытался взять exp столбцов.

slope_calc <-  lapply(Slope, exp(x1))

, которые не работали.

myLinearModel <- list(coefficients = list(XLE.Adjusted = structure(c(NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 3.02239619131764, 3.88614936592188, 
4.4316715293472, 4.95125063034751, 5.36643891833301, 5.67378020013074, 
5.38101705998938, 5.14977297787001, 4.62914286958868, 3.94472317952619, 
2.53231959481246, 1.4218784566451, 0.00010364095638149, -1.2234858565559, 
-2.77269771315001, -4.28043173064425, -5.53653697954025, -6.81984822457144, 
-8.02330596014672, -9.42051172322219, -10.561115807903, -11.1632142519063, 
-11.1039543989832, -11.1207586602127, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 0.0000560176219152604, 0.0000085553769637845, 
-0.000021411005961506, -0.0000499668709796664, -0.0000727836042042275, 
-0.0000896998824466513, -0.0000736222695348367, -0.0000609339321522088, 
-0.0000323191308555377, 0.00000528448318397251, 0.0000828592780329832, 
0.000143840622023329, 0.000221939933158979, 0.000289134001740165, 
0.000374217013151444, 0.000457009137455344, 0.000525984936663271, 
0.000596442443883195, 0.000662524255478158, 0.000739243653003764, 
0.000801868288271764, 0.000834936039355274, 0.000831707447825559, 
0.000832640867678377), .Dim = c(83L, 2L), .Dimnames = list(NULL, 
    c("(Intercept)", "x1")), index = structure(c(1567468800, 
1567555200, 1567641600, 1567728000, 1567987200, 1568073600, 1568160000, 
1568246400, 1568332800, 1568592000, 1568678400, 1568764800, 1568851200, 
1568937600, 1569196800, 1569283200, 1569369600, 1569456000, 1569542400, 
1569801600, 1569888000, 1569974400, 1570060800, 1570147200, 1570406400, 
1570492800, 1570579200, 1570665600, 1570752000, 1571011200, 1571097600, 
1571184000, 1571270400, 1571356800, 1571616000, 1571702400, 1571788800, 
1571875200, 1571961600, 1572220800, 1572307200, 1572393600, 1572480000, 
1572566400, 1572825600, 1572912000, 1572998400, 1573084800, 1573171200, 
1573430400, 1573516800, 1573603200, 1573689600, 1573776000, 1574035200, 
1574121600, 1574208000, 1574294400, 1574380800, 1574640000, 1574726400, 
1574812800, 1574985600, 1575244800, 1575331200, 1575417600, 1575504000, 
1575590400, 1575849600, 1575936000, 1576022400, 1576108800, 1576195200, 
1576454400, 1576540800, 1576627200, 1576713600, 1576800000, 1577059200, 
1577145600, 1577318400, 1577404800, 1577664000), tzone = "UTC", tclass = "Date"), class = c("xts", 
"zoo")), XLY.Adjusted = structure(c(NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 4.98889484319804, 5.16802090923807, 4.97616463385546, 
4.52078796121232, 4.18775798814248, 4.00275488080957, 3.76282789322115, 
3.49092596497609, 2.92825584329686, 2.37104079768632, 2.14362125726613, 
1.73140742339278, 1.14034245413532, 0.589522376427091, 0.201098541050889, 
-0.320138223638116, -0.614721732377921, -1.1417788011257, -1.58782764427245, 
-1.88366441033893, -2.36352774771231, -2.90012382672107, -3.01162142504143, 
-3.03517989881806, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
-0.0000108362013472044, -0.0000206592926441128, -0.0000100823930980587, 
0.0000149589499384135, 0.0000332599175195558, 0.000043407489433079, 
0.0000565814419118819, 0.0000715082778937003, 0.000102417074840518, 
0.000133023995469188, 0.000145513039608199, 0.000168151284311777, 
0.000200621471807216, 0.000230878798793901, 0.000252224001462865, 
0.000280862901016464, 0.000297058432868864, 0.00032601432628427, 
0.000350522134622209, 0.00036677760002836, 0.000393130452175933, 
0.000422612610194717, 0.000428767791934619, 0.000430083219860299
), .Dim = c(83L, 2L), .Dimnames = list(NULL, c("(Intercept)", 
"x1")), index = structure(c(1567468800, 1567555200, 1567641600, 
1567728000, 1567987200, 1568073600, 1568160000, 1568246400, 1568332800, 
1568592000, 1568678400, 1568764800, 1568851200, 1568937600, 1569196800, 
1569283200, 1569369600, 1569456000, 1569542400, 1569801600, 1569888000, 
1569974400, 1570060800, 1570147200, 1570406400, 1570492800, 1570579200, 
1570665600, 1570752000, 1571011200, 1571097600, 1571184000, 1571270400, 
1571356800, 1571616000, 1571702400, 1571788800, 1571875200, 1571961600, 
1572220800, 1572307200, 1572393600, 1572480000, 1572566400, 1572825600, 
1572912000, 1572998400, 1573084800, 1573171200, 1573430400, 1573516800, 
1573603200, 1573689600, 1573776000, 1574035200, 1574121600, 1574208000, 
1574294400, 1574380800, 1574640000, 1574726400, 1574812800, 1574985600, 
1575244800, 1575331200, 1575417600, 1575504000, 1575590400, 1575849600, 
1575936000, 1576022400, 1576108800, 1576195200, 1576454400, 1576540800, 
1576627200, 1576713600, 1576800000, 1577059200, 1577145600, 1577318400, 
1577404800, 1577664000), tzone = "UTC", tclass = "Date"), class = c("xts", 
"zoo"))), r.squared = list(XLE.Adjusted = structure(c(NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.00272802883705676, 0.0000667929781573667, 
0.000427941454839732, 0.00234937217080687, 0.0050179536778247, 
0.00758794386584372, 0.00517821082891964, 0.00358569755492882, 
0.00101224527230829, 0.000027471548542707, 0.00741031098523336, 
0.023642572507366, 0.0575891261641195, 0.100475513095238, 0.169705020767937, 
0.255692043936871, 0.328065092311103, 0.409656351798543, 0.472820538079838, 
0.539367590711293, 0.58843079395797, 0.605688129219686, 0.605623542956064, 
0.607278786511739), .Dim = c(83L, 1L), .Dimnames = list(NULL, 
    "R-squared"), index = structure(c(1567468800, 1567555200, 
1567641600, 1567728000, 1567987200, 1568073600, 1568160000, 1568246400, 
1568332800, 1568592000, 1568678400, 1568764800, 1568851200, 1568937600, 
1569196800, 1569283200, 1569369600, 1569456000, 1569542400, 1569801600, 
1569888000, 1569974400, 1570060800, 1570147200, 1570406400, 1570492800, 
1570579200, 1570665600, 1570752000, 1571011200, 1571097600, 1571184000, 
1571270400, 1571356800, 1571616000, 1571702400, 1571788800, 1571875200, 
1571961600, 1572220800, 1572307200, 1572393600, 1572480000, 1572566400, 
1572825600, 1572912000, 1572998400, 1573084800, 1573171200, 1573430400, 
1573516800, 1573603200, 1573689600, 1573776000, 1574035200, 1574121600, 
1574208000, 1574294400, 1574380800, 1574640000, 1574726400, 1574812800, 
1574985600, 1575244800, 1575331200, 1575417600, 1575504000, 1575590400, 
1575849600, 1575936000, 1576022400, 1576108800, 1576195200, 1576454400, 
1576540800, 1576627200, 1576713600, 1576800000, 1577059200, 1577145600, 
1577318400, 1577404800, 1577664000), tzone = "UTC", tclass = "Date"), class = c("xts", 
"zoo")), XLY.Adjusted = structure(c(NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 0.000566837207432245, 0.00213707470294336, 0.000491767172532374, 
0.00107584137287431, 0.00542923262797556, 0.00957746540985965, 
0.0166843622448482, 0.0275011422426094, 0.0592842230618785, 0.104129625728413, 
0.124797074201594, 0.168008295124026, 0.231320333150441, 0.29267024767244, 
0.325895371357572, 0.369402206566775, 0.386558799981241, 0.42298738973539, 
0.447766206165064, 0.464764300002698, 0.498106404749475, 0.514512350787861, 
0.515308094301249, 0.517604217744102), .Dim = c(83L, 1L), .Dimnames = list(
    NULL, "R-squared"), index = structure(c(1567468800, 1567555200, 
1567641600, 1567728000, 1567987200, 1568073600, 1568160000, 1568246400, 
1568332800, 1568592000, 1568678400, 1568764800, 1568851200, 1568937600, 
1569196800, 1569283200, 1569369600, 1569456000, 1569542400, 1569801600, 
1569888000, 1569974400, 1570060800, 1570147200, 1570406400, 1570492800, 
1570579200, 1570665600, 1570752000, 1571011200, 1571097600, 1571184000, 
1571270400, 1571356800, 1571616000, 1571702400, 1571788800, 1571875200, 
1571961600, 1572220800, 1572307200, 1572393600, 1572480000, 1572566400, 
1572825600, 1572912000, 1572998400, 1573084800, 1573171200, 1573430400, 
1573516800, 1573603200, 1573689600, 1573776000, 1574035200, 1574121600, 
1574208000, 1574294400, 1574380800, 1574640000, 1574726400, 1574812800, 
1574985600, 1575244800, 1575331200, 1575417600, 1575504000, 1575590400, 
1575849600, 1575936000, 1576022400, 1576108800, 1576195200, 1576454400, 
1576540800, 1576627200, 1576713600, 1576800000, 1577059200, 1577145600, 
1577318400, 1577404800, 1577664000), tzone = "UTC", tclass = "Date"), class = c("xts", 
"zoo"))), std.error = list(XLE.Adjusted = structure(c(NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 2.55715195420927, 2.49943714299577, 
2.47097504528166, 2.45892751674777, 2.44772556424091, 2.45016047528293, 
2.43749930108922, 2.42649104176685, 2.42558798408019, 2.40884996595051, 
2.29137614711048, 2.20882340477333, 2.14556436854187, 2.06759534995848, 
1.97841313379212, 1.8638072854433, 1.79948857210704, 1.71173736359576, 
1.67260209393366, 1.63349115933776, 1.60363279017081, 1.61105709887396, 
1.60517066858881, 1.60153829837792, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 0.000140634864607623, 0.000137450158428207, 0.000135874497208889, 
0.000135201492932277, 0.000134574835999221, 0.000134698214269024, 
0.00013399172695222, 0.00013337620518148, 0.000133316185840543, 
0.00013238567300781, 0.000125919737812665, 0.000121373704682028, 
0.000117888476159589, 0.000113595610277398, 0.000108687197740801, 
0.000102383178912518, 0.0000988423132899811, 0.0000940149988619425, 
0.0000918583966548947, 0.0000897033019373937, 0.000088056776308246, 
0.0000884574886696485, 0.0000881273496996249, 0.00008792084462616
), .Dim = c(83L, 2L), .Dimnames = list(NULL, c("(Intercept)", 
"x1")), index = structure(c(1567468800, 1567555200, 1567641600, 
1567728000, 1567987200, 1568073600, 1568160000, 1568246400, 1568332800, 
1568592000, 1568678400, 1568764800, 1568851200, 1568937600, 1569196800, 
1569283200, 1569369600, 1569456000, 1569542400, 1569801600, 1569888000, 
1569974400, 1570060800, 1570147200, 1570406400, 1570492800, 1570579200, 
1570665600, 1570752000, 1571011200, 1571097600, 1571184000, 1571270400, 
1571356800, 1571616000, 1571702400, 1571788800, 1571875200, 1571961600, 
1572220800, 1572307200, 1572393600, 1572480000, 1572566400, 1572825600, 
1572912000, 1572998400, 1573084800, 1573171200, 1573430400, 1573516800, 
1573603200, 1573689600, 1573776000, 1574035200, 1574121600, 1574208000, 
1574294400, 1574380800, 1574640000, 1574726400, 1574812800, 1574985600, 
1575244800, 1575331200, 1575417600, 1575504000, 1575590400, 1575849600, 
1575936000, 1576022400, 1576108800, 1576195200, 1576454400, 1576540800, 
1576627200, 1576713600, 1576800000, 1577059200, 1577145600, 1577318400, 
1577404800, 1577664000), tzone = "UTC", tclass = "Date"), class = c("xts", 
"zoo")), XLY.Adjusted = structure(c(NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 1.08636135698409, 1.06591854264299, 1.08540910209538, 
1.08853773596743, 1.07511434220683, 1.05430999436528, 1.03757188910514, 
1.01580849822287, 0.974657818398504, 0.932223804439982, 0.920752001636802, 
0.8941630412764, 0.873975788554756, 0.857819137038633, 0.867032881208871, 
0.877160883640559, 0.894566785078665, 0.91031482546048, 0.930702883787585, 
0.941138028762768, 0.943647076077152, 0.981738840003784, 0.994530405011063, 
0.993085924415702, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
0.0000597462665849495, 0.0000586174663237333, 0.0000596847047463205, 
0.0000598520802315354, 0.0000591092965635484, 0.0000579609682547785, 
0.0000570363442550111, 0.0000558356409943353, 0.0000535695524967021, 
0.0000512331932204797, 0.0000505987857047386, 0.0000491337970590741, 
0.0000480207797182594, 0.0000471293806989319, 0.0000476317977262412, 
0.0000481844450261784, 0.0000491367668570541, 0.0000499978846637408, 
0.0000511136958257362, 0.0000516826725851987, 0.0000518164258060635, 
0.0000539038326927621, 0.0000546018753671679, 0.0000545181800206789
), .Dim = c(83L, 2L), .Dimnames = list(NULL, c("(Intercept)", 
"x1")), index = structure(c(1567468800, 1567555200, 1567641600, 
1567728000, 1567987200, 1568073600, 1568160000, 1568246400, 1568332800, 
1568592000, 1568678400, 1568764800, 1568851200, 1568937600, 1569196800, 
1569283200, 1569369600, 1569456000, 1569542400, 1569801600, 1569888000, 
1569974400, 1570060800, 1570147200, 1570406400, 1570492800, 1570579200, 
1570665600, 1570752000, 1571011200, 1571097600, 1571184000, 1571270400, 
1571356800, 1571616000, 1571702400, 1571788800, 1571875200, 1571961600, 
1572220800, 1572307200, 1572393600, 1572480000, 1572566400, 1572825600, 
1572912000, 1572998400, 1573084800, 1573171200, 1573430400, 1573516800, 
1573603200, 1573689600, 1573776000, 1574035200, 1574121600, 1574208000, 
1574294400, 1574380800, 1574640000, 1574726400, 1574812800, 1574985600, 
1575244800, 1575331200, 1575417600, 1575504000, 1575590400, 1575849600, 
1575936000, 1576022400, 1576108800, 1576195200, 1576454400, 1576540800, 
1576627200, 1576713600, 1576800000, 1577059200, 1577145600, 1577318400, 
1577404800, 1577664000), tzone = "UTC", tclass = "Date"), class = c("xts", 
"zoo"))))

РЕДАКТИРОВАТЬ:

Я думаю, что это получает мой exp функция:

 out <- map(lm_model$coefficients, ~exp(.x$x1))

bind_cols(out) # but doesn`t give the column / list names

РЕДАКТИРОВАТЬ:

> map(lm_model$coefficients, ~length(.x))
$XLE.Adjusted
[1] 166

$XLY.Adjusted
[1] 166

> map(lm_model$r.squared, ~length(.x))
$XLE.Adjusted
[1] 83

$XLY.Adjusted
[1] 83

> map(lm_model$out, ~length(.x))
$XLE.Adjusted
[1] 83

$XLY.Adjusted
[1] 83

Ответы [ 2 ]

3 голосов
/ 29 февраля 2020

Объект xts также является matrix.

library(purrr)
map_lgl(lm_model$coefficients, is.matrix)
#XLE.Adjusted XLY.Adjusted 
#        TRUE         TRUE 

Итак, мы не можем использовать $ вместо использования [ для поднабора

map_dfc(lm_model$coefficients, ~exp(.x[, "x1"]))

Если нам нужно умножить два компонента

map2(lm_model$r.squared, map(lm_model$coefficients, ~  exp(.x[, "x1"])), `*`)
1 голос
/ 29 февраля 2020

В вашем slope_calc' attempt, the consideration of variable names is superfluous and causes the error. The second argument of lapply` это просто имя функции (с кавычками или без).

slope_calc <- lapply(Slope, "sum")

Так что, если я ничего не пропустил, вы можете просто продолжить с Map (что с большой буквой "М"),

exp_calc <- Map(`*`, slope_calc, myLinearModel$r.squared)

и преобразовать as.xts .

library(xts)
res <- lapply(exp_calc, as.xts)

Результат

lapply(res, tail)
# $XLE.Adjusted
#                   x1
# 2019-12-20 0.4731339
# 2019-12-23 0.5397665
# 2019-12-24 0.5889028
# 2019-12-26 0.6061941
# 2019-12-27 0.6061275
# 2019-12-30 0.6077846
# 
# $XLY.Adjusted
#                   x1
# 2019-12-20 0.4479232
# 2019-12-23 0.4649348
# 2019-12-24 0.4983023
# 2019-12-26 0.5147298
# 2019-12-27 0.5155291
# 2019-12-30 0.5178269
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...