Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

extract item from list in R using tidyverse

I have a list in R from a Structural VAR Model I've run. Here is the dataset I've used

structure(c(-2.46773175636719, -7.72826473957142, 11.7280053716074, 
-1.90017613519231, 2.46070753200911, -10.9055849254775, 12.5325444143789, 
1.3401777880631, 1.11706357603545, -11.9683333308867, 13.8014223673123, 
3.00567366940339, 1.93467425287253, -12.4811258520841, 12.5337510851622, 
6.17970387794458, -0.311524449552714, -4.43067381564184, 6.00580266302142, 
0.733215859597713, -0.0920826461465296, -10.4968848655989, 15.1592141225619, 
-0.0142294828419764, 2.81612947228318, -7.95484787976672, 11.3258100085652, 
-4.26536944046738, 1.71777230300645, 3.05071906367438, 6.86888493397788, 
-2.04121390648186, -5.45632423113958, 4.32701496742318, -1.77039007913358, 
-3.38387552001187, -3.6948032720689, 9.71137617961375, 7.70239243534423, 
-7.34908250395865, 1.66529724744393, 4.12699586248877, 1.46757481960265, 
0.371903496562886, -3.1265641490501, 10.1504446414976, -6.97113134346488, 
10.6335568680931, -7.74296970668011, -0.31563625249369, 4.86848083529221, 
0.0394209612387897, 4.1060141554107, 4.40828962229318, -3.65631265465627, 
-0.804371886780864, 5.300399767623, 3.33547294572138, 2.19966779280814, 
-2.96620054964727, 2.01147630916658, 3.00808531323007, -1.78858517036105, 
-0.262917554632125, -2.63540419004151, 3.42060573075127, -0.379197105963414, 
-2.24861964821645, -1.33132752566709, 1.67672890048411, 2.42834777679839, 
-1.28230298674303, 2.33228006495114, -1.79196678955762, -3.25533945043563, 
-2.49506277883942, 3.87275419034108, -0.237088301198796, 3.15158720918571, 
-8.16796902280537, -5.40342750457601, -0.684253285161596, 2.76776077124627, 
3.03762540403962, 1.59655060303945, 2.81108799665279, 2.12619249528281, 
1.25976266808472, 1.75072385830202, 3.35523567403357, 3.66056933213099, 
2.81157744760496, 2.8694753969682, 1.22389693906415, 1.23949528512668, 
0.655297291930701, -0.0811557166513488, 2.49469766016768, 1.04750905161959, 
0.553154355370644, 0.0394037933455493, 0.495095459931427, 0.263970407073533, 
1.46876270708987, 1.02690822562028, 1.21249150827216, 0.744350196120713, 
2.28082505446663, 1.40375659950536, 1.59077468759525, 1.74048892256584, 
1.81791859189397, 0.640927792895951, 2.05320969806211, 3.31708856718933, 
3.94172305193239, 1.29907384513261, 2.18844272980503, 2.02564945461834, 
2.89662683480758, 0.531971587307556, 1.69103059033393, 0.195326999327161, 
1.20287931837835, -0.0417495586606087, 1.89055607207678, 1.8793164132429, 
1.19212718508228, 1.43097285231573, 2.4079363839828, 1.02287700043888, 
1.41700117422383, 1.9324642470476, 1.42192313279805, 1.00916357593048, 
1.26198292983659, 0.929701659038162, 1.77329986790085, 1.88793046797189, 
0.685737107435092, 0.400744609924519, 0.766302538581343, 1.09780031685389, 
1.00049427220021, 0.535284014354875, 3.18688973234602, 1.75770144393539, 
1.19538307882463, 0.904511107610428, 3.52708500618766, 0.526934426385495, 
0.361429093322574, 0.624469028588326, 1.95600784906818, 0.781933189336748, 
1.09121935535335, 1.30962848646536, 1.24173607356797, 0.453213216209214, 
0.468243562258674, 0.489076348335971, 0.848706378173603, 0.125371629239357, 
0.817153292286932), .Dim = c(82L, 2L), .Dimnames = list(NULL, 
    c("GDP_NAM", "CPI_NAM")), index = structure(c(962323200, 
970272000, 978220800, 985996800, 993859200, 1001808000, 1009756800, 
1017532800, 1025395200, 1033344000, 1041292800, 1049068800, 1056931200, 
1064880000, 1072828800, 1080691200, 1088553600, 1096502400, 1104451200, 
1112227200, 1120089600, 1128038400, 1135987200, 1143763200, 1151625600, 
1159574400, 1167523200, 1175299200, 1183161600, 1191110400, 1199059200, 
1206921600, 1214784000, 1222732800, 1230681600, 1238457600, 1246320000, 
1254268800, 1262217600, 1269993600, 1277856000, 1285804800, 1293753600, 
1301529600, 1309392000, 1317340800, 1325289600, 1333152000, 1341014400, 
1348963200, 1356912000, 1364688000, 1372550400, 1380499200, 1388448000, 
1396224000, 1404086400, 1412035200, 1419984000, 1427760000, 1435622400, 
1443571200, 1451520000, 1459382400, 1467244800, 1475193600, 1483142400, 
1490918400, 1498780800, 1506729600, 1514678400, 1522454400, 1530316800, 
1538265600, 1546214400, 1553990400, 1561852800, 1569801600, 1577750400, 
1585612800, 1593475200, 1601424000), tzone = "UTC", tclass = "Date"), class = c("xts", 
"zoo"))

and the code I've used to create the model

library(tidyverse)
library(vars)

var.namibia <- namibia %>% VAR(.,p=1,type = 'both',season=NULL)
SVAR.namibia <- BQ(var.namibia)

from this, I want to extract residuals from the variables in my model. I can do it by indexing (see below), but I'm interested in finding a dplyr solution to achieve the same. Any suggestions?

res <- SVAR.namibia$var$varresult$CPI_NAM$residuals


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use purrr's pluck function.

SVAR.namibia %>%
  purrr::pluck('var', 'varresult', 'CPI_NAM', 'residuals')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...