I've been wrangling data in Python to comply with the documentation for the "BGVAR" package in R.
It states that for the bgvar
class I need to input Data
as a list of dataframes where each item is named after an entity and every column is named after a variable. The W
object needs to be a square NxN
matrix where N
is the number of entities.
However, when I run this code
library("BGVAR")
library("readxl")
Data <- excel_to_list(file = pathtoxlsxwithData)
Wread <- read_excel(path = pathtoxlsxwithW)
W <- as.matrix(Wread)
W
W <- W[,-1]
rownames(W) <- names(Data)
colnames(W) <- names(Data)
storage.mode(W) <- "numeric"
mymodel <- bgvar(Data = Data, W = W)
The package throws an error:
Error in dimnames(x) <- dn :
length of 'dimnames' [1] not equal to array extent
for some inputs. When I compare the inputs, I cannot tell the difference between them that could be causing the error.
Edit: full script with dputs that reproduces the error as well as its absence:
library("BGVAR")
#this dataset works
data5<- list(CN = structure(c(4.59, 3.24, 3.24, 3.24, 3.24, 3.24, 3.24,
3.24, 3.24, 3.24, 3.24, 3.24, 2.7, 2.7, 2.7, 2.7, 2.7, 2.7, 2.7,
8.06650416308289, 8.34461339283924, 8.5574295507205, 8.8416664184757,
9.17299383383491, 9.43908106690589, 9.69503274624059, 9.97375186158891,
10.3341289752455, 10.6807472746534, 11.3639676234721, 11.7061708798632,
12.1322895358986, 12.5725745334781, 13.091436070924, 13.6849260958503,
14.3713199075819, 15.0355125778046, 15.8370499740831, 98.566272,
97.83291, 98.836061, 99.1672012, 100.1008965, 100.09692045, 100.2640063,
100.9300591, 100.66374, 101.567239, 100.7926206, 99.8662782,
99.4131021, 99.0086457, 99.2423089, 99.4068282, 100.4696215,
100.6094389, 100.8069077), dim = c(19L, 3L), dimnames = list(
NULL, c("IIR", "MBR", "CPI")), index = structure(c(915148800,
922924800, 930787200, 938736000, 946684800, 954547200, 962409600,
970358400, 978307200, 986083200, 993945600, 1001894400, 1009843200,
1017619200, 1025481600, 1033430400, 1041379200, 1049155200, 1057017600
), tzone = "UTC", tclass = "Date"), class = c("xts", "zoo")),
EU = structure(c(2.9252, 2.5632, 2.4336, 3.0418, 3.5104,
4.2945, 4.5905, 4.8268, 4.7814, 4.5395, 3.9865, 3.3389, 3.264,
3.3485, 3.3186, 3.0945, 2.749, 2.2133, 2.0209, 42.0528902876188,
42.5438693283613, 42.950682509578, 43.5053683124797, 44.6424530671513,
45.3741172927471, 45.2717683632816, 45.551092571757, 47.3904685378504,
48.6113984962501, 49.2346889214365, 50.262830868842, 51.3018546794212,
52.1071597095362, 52.6113170748269, 53.674206164702, 55.014838189854,
56.4412582156259, 56.8227600227728, 100.90444, 101.021891,
101.184049, 101.579019, 102.005636, 101.96072, 102.265031,
102.529457, 102.157986, 102.955839, 102.434531, 102.144632,
102.56009, 102.112525, 102.106886, 102.300175, 102.31819,
101.997482, 102.0718), dim = c(19L, 3L), dimnames = list(
NULL, c("IIR", "MBR", "CPI")), index = structure(c(915148800,
922924800, 930787200, 938736000, 946684800, 954547200, 962409600,
970358400, 978307200, 986083200, 993945600, 1001894400, 1009843200,
1017619200, 1025481600, 1033430400, 1041379200, 1049155200,
1057017600), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), RU = structure(c(60, 55, 55, 55, 33, 33, 28, 25,
25, 25, 25, 25, 25, 23, 21, 21, 18, 16, 16, 1.41124672182256,
1.59329907965375, 1.78179645448887, 1.97570468563864, 2.24040570450153,
2.52379975867993, 2.85068461482232, 3.21880745853885, 3.48527543523521,
3.78752721267999, 4.09204522240658, 4.47302782031843, 4.6842626885763,
4.9668169460222, 5.35451494946906, 5.78343580309854, 6.28162733303322,
6.8606313442598, 7.51108185634982, 202.6092, 216.8237, 198.22007,
147.46852, 125.43802, 119.83093, 118.73701, 119.78989, 122.19488,
124.41, 120.9723, 118.67606, 117.78062, 115.54778, 114.98511,
115.00793, 114.63042, 114.05352, 113.51224), dim = c(19L,
3L), dimnames = list(NULL, c("IIR", "MBR", "CPI")), index = structure(c(915148800,
922924800, 930787200, 938736000, 946684800, 954547200, 962409600,
970358400, 978307200, 986083200, 993945600, 1001894400, 1009843200,
1017619200, 1025481600, 1033430400, 1041379200, 1049155200,
1057017600), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), TR = structure(c(76.32, 76.91, 67.51, 69.97, 39.2,
43.05, 47.44, 183.2, 81.19, 63, 59.03, 59, 54.73, 48, 46,
44, 44, 38.23, 30.66, 2.03618691787274, 2.45224466198878,
2.85152980194772, 3.40383568191502, 4.02924500314335, 4.46950864742143,
4.88377331905386, 5.1592037849979, 5.96328891188708, 7.56154093609563,
8.71507793534991, 9.78655006771246, 9.81916170801812, 10.599320649388,
11.7940455732388, 12.1055918024629, 12.8023795093054, 12.7833858225293,
12.9848521657004, 164.42184, 163.69998, 164.87749, 166.06177,
168.82719, 161.65232, 152.65753, 142.31555, 135.63059, 152.29092,
158.59326, 167.45192, 170.32741, 147.04456, 139.45757, 131.60961,
126.53236, 125.86128, 121.06997), dim = c(19L, 3L), dimnames = list(
NULL, c("IIR", "MBR", "CPI")), index = structure(c(915148800,
922924800, 930787200, 938736000, 946684800, 954547200, 962409600,
970358400, 978307200, 986083200, 993945600, 1001894400, 1009843200,
1017619200, 1025481600, 1033430400, 1041379200, 1049155200,
1057017600), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), US = structure(c(4.81, 4.76, 5.22, 5.3, 5.85, 6.53,
6.52, 6.4, 5.31, 3.97, 3.07, 1.82, 1.73, 1.75, 1.75, 1.24,
1.25, 1.22, 1.01, 36.6954197142406, 37.2300769607558, 37.7854895351637,
38.3004984046071, 38.8976983725056, 39.5507993568616, 40.0165489147731,
40.6098745547982, 41.6816796871757, 42.7349433933024, 43.694116279649,
44.6978439365385, 45.4754768882506, 45.8371730689935, 46.7041922994966,
47.6625349727275, 48.4476398424809, 49.4090266304694, 50.4113705988327,
101.667696, 102.109359, 102.345981, 102.622484, 103.240178,
103.329322, 103.508073, 103.4271, 103.393488, 103.377329,
102.695937, 101.857882, 101.252134, 101.295531, 101.59385,
102.200075, 102.866779, 102.131603, 102.196383, 264.333333333333,
253.75, 268.083333333333, 245.75, 255.166666666667, 258.833333333333,
254, 263.416666666667, 264.333333333333, 262.166666666667,
275.75, 287.916666666667, 279.416666666667, 283.666666666667,
364.25, 366.75, 306.583333333333, 301.833333333333, 358.75,
14.3103989361702, 18.295156626506, 22.3649880668258, 23.826140776699,
28.2539671361502, 29.3859846547315, 30.5744572748268, 31.2360814249364,
27.4194019138756, 27.6128148148148, 25.7981572481572, 20.134243902439,
22.5551535087719, 26.4484615384615, 28.7856198347107, 28.4003557312253,
33.7766602316602, 28.5684267241379, 30.4364462809917), dim = c(19L,
5L), dimnames = list(NULL, c("IIR", "MBR", "CPI", "WHT",
"WTI")), index = structure(c(915148800, 922924800, 930787200,
938736000, 946684800, 954547200, 962409600, 970358400, 978307200,
986083200, 993945600, 1001894400, 1009843200, 1017619200,
1025481600, 1033430400, 1041379200, 1049155200, 1057017600
), tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"
)))
W5 <- structure(c(0, 0.1062173, 0.1883998, 0.1033952, 0.445663, 0.4197663,
0, 0.6621655, 0.6400166, 0.5123887, 0.07414336, 0.08450978, 0,
0.14496766, 0.02597733, 0.01588464, 0.07693148, 0.06575524, 0,
0.01597095, 0.4902057, 0.7323414, 0.0836794, 0.1116206, 0), dim = c(5L,
5L), dimnames = list(c("CN", "EU", "RU", "TR", "US"), c("CN",
"EU", "RU", "TR", "US")))
mod5 <- bgvar(Data = data5, W = W5, draws = 10)
#this does not work
data15 <- list(AU = structure(c(82.26542, 76.90665, 81.3056, 13.3278529605016,
13.2675314934202, 14.08287406905, 1.57492049199085, 1.53241145743146,
1.53703787878788, 687779.1, 690014.4, 698007.9, 15.4186513303132,
15.7887207126198, 16.3593905194588, 5.31813882533333, 5.813293651,
6.27242424233333, 21.6562684866715, 22.2824426507816, 22.7961576160219
), dim = c(3L, 7L), dimnames = list(NULL, c("CPI", "EXP", "FXR",
"GDP", "IMP", "LTR", "MBR")), index = structure(c(917740800,
925430400, 933379200), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), BR = structure(c(122.0008, 118.3568, 121.3928, 11.4663652061975,
12.0036792773981, 11.5188000894012, 1.76999944444444, 1.71353391812866,
1.85892121212121, 1888531.9, 1896246, 1898302.3, 12.0720366715372,
12.7649682308787, 12.2797300986907, 12.84, 13.48, 14.05, 8.37557145311969,
8.80096626575696, 9.2219031051324), dim = c(3L, 7L), dimnames = list(
NULL, c("CPI", "EXP", "FXR", "GDP", "IMP", "LTR", "MBR")), index = structure(c(917740800,
925430400, 933379200), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), CA = structure(c(95.6446, 96.81783, 98.2313, 56.5147700547161,
58.2006505998734, 60.9462095084936, 1.51118672302738, 1.47301513708514,
1.48561146461905, 1113890.4, 1123208.6, 1140783.2, 51.3699660110776,
53.0084007132572, 54.0430206460317, 5.09697826086957, 5.32331096681097,
5.65603174603175, 31.0106333689699, 31.5177272859839, 31.961391384168
), dim = c(3L, 7L), dimnames = list(NULL, c("CPI", "EXP", "FXR",
"GDP", "IMP", "LTR", "MBR")), index = structure(c(917740800,
925430400, 933379200), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), CH = structure(c(94.26778, 97.00242, 95.68923, 19.05200132798,
18.2208427948728, 18.9420780347153, 1.42636358695652, 1.51443228715729,
1.52747196775758, 18.9725459040406, 17.9508938635398, 18.9808202320066,
2.56966666666667, 2.73033333333333, 3.21333333333333, 49.7735085441036,
50.0841532380439, 50.2953552933618), dim = c(3L, 6L), dimnames = list(
NULL, c("CPI", "EXP", "FXR", "IMP", "LTR", "MBR")), index = structure(c(917740800,
925430400, 933379200), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), CN = structure(c(58.0546, 57.23155, 58.45868, 43.5283183955028,
45.7667027202633, 51.369022356663, 8.2787, 8.27856333333333,
8.27743, 37.6837351837686, 41.3163600353252, 41.9422477435453,
4.47663333333333, 4.47663333333333, 4.47663333333333, 8.06650416308289,
8.34461339283924, 8.5574295507205), dim = c(3L, 6L), dimnames = list(
NULL, c("CPI", "EXP", "FXR", "IMP", "LTR", "MBR")), index = structure(c(917740800,
925430400, 933379200), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), EA = structure(c(117.6876, 118.4051, 119.1685, 209.838005174791,
206.344334864423, 214.310007179154, 0.890865035412319, 0.946297424241702,
0.953590795030303, 11401409.4, 11461237.1, 11598799, 204.666656461184,
201.047372041002, 210.300905832237, 3.9924, 4.25743333333333,
5.0507, 42.0528902876188, 42.5438693283613, 42.950682509578), dim = c(3L,
7L), dimnames = list(NULL, c("CPI", "EXP", "FXR", "GDP", "IMP",
"LTR", "MBR")), index = structure(c(917740800, 925430400, 933379200
), tzone = "UTC", tclass = "Date"), class = c("xts", "zoo")),
IL = structure(c(111.1162, 111.1162, 111.1162, 5.09757871712443,
5.4592682123013, 5.96892630789915, 4.06122011732229, 4.08691197691198,
4.18112121212121, 163284.8, 168668.4, 169290, 7.10997117053671,
7.406586771916, 7.9197374186323, 5.21, 5.09333333333333,
5.21666666666667, 22.8002029860775, 23.805787468422, 24.8224859629471
), dim = c(3L, 7L), dimnames = list(NULL, c("CPI", "EXP",
"FXR", "GDP", "IMP", "LTR", "MBR")), index = structure(c(917740800,
925430400, 933379200), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), IN = structure(c(70.86469, 69.57063, 70.92097, 8.39952864029184,
8.44018422538691, 9.52468309974898, 42.4695966666667, 42.8780733333333,
43.4284866666667, 2268598.5, 2327179, 2356160, 10.5690361503223,
10.7008370773947, 12.9287236978616, 8.26716666666667, 8.26716666666667,
8.26716666666667, 8.8035303879122, 9.13320603099139, 9.49252426345693
), dim = c(3L, 7L), dimnames = list(NULL, c("CPI", "EXP",
"FXR", "GDP", "IMP", "LTR", "MBR")), index = structure(c(917740800,
925430400, 933379200), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), JP = structure(c(185.5802, 205.3497, 183.9057, 98.9686430444595,
97.2352592101695, 107.217234836789, 116.523421052631, 120.902406078327,
113.598863636364, 4516739.7, 4533668.7, 4558343.4, 72.7857900351236,
72.0284287089008, 79.4308397025317, 1.94766666666667, 1.50966666666667,
1.78, 76.8403418311359, 77.4865630799908, 77.9800907004243
), dim = c(3L, 7L), dimnames = list(NULL, c("CPI", "EXP",
"FXR", "GDP", "IMP", "LTR", "MBR")), index = structure(c(917740800,
925430400, 933379200), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), KR = structure(c(102.7181, 100.7295, 104.8241, 32.2524721948081,
34.9171509637589, 36.2037447955411, 1192.05405797101, 1187.39287157287,
1195.16121212121, 915697.3, 955833.8, 982417.2, 25.8937480394644,
28.6686974235292, 30.4342735103449, 7.76333333333333, 7.76333333333333,
7.76333333333333, 27.2030286045815, 27.8539035746815, 28.2525780239985
), dim = c(3L, 7L), dimnames = list(NULL, c("CPI", "EXP",
"FXR", "GDP", "IMP", "LTR", "MBR")), index = structure(c(917740800,
925430400, 933379200), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), RU = structure(c(61.36739, 64.91503, 84.93548, 15.4860492644414,
15.9242307983476, 17.0560174296649, 22.8895, 24.4942333333333,
24.8239, 1742001.7, 1803740.1, 1839252.1, 9.09241637213071,
9.69047185843195, 9.0749258239904, 98.16, 90.6766666666667,
75.9933333333334, 1.41124672182256, 1.59329907965375, 1.78179645448887
), dim = c(3L, 7L), dimnames = list(NULL, c("CPI", "EXP",
"FXR", "GDP", "IMP", "LTR", "MBR")), index = structure(c(917740800,
925430400, 933379200), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), SA = structure(c(10.1824, 11.588, 13.3738666666667,
3.75, 3.75, 3.75, 1148504.3, 1148504.3, 1148504.3, 7.0808,
6.9392, 6.92773333333333), dim = 3:4, dimnames = list(NULL,
c("EXP", "FXR", "GDP", "IMP")), index = structure(c(917740800,
925430400, 933379200), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), TR = structure(c(67.63757, 70.3793, 72.74276, 6.890286218,
6.243196527, 6.530444188, 0.34168861884058, 0.395968412337662,
0.439352015151515, 910591.9, 905458.8, 890187.1, 9.391672566,
10.05122254, 10.22050927, 2.03618691787274, 2.45224466198878,
2.85152980194772), dim = c(3L, 6L), dimnames = list(NULL,
c("CPI", "EXP", "FXR", "GDP", "IMP", "MBR")), index = structure(c(917740800,
925430400, 933379200), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), UK = structure(c(90.00383, 87.55027, 87.51717, 64.5369466592794,
63.9992857837613, 68.4235909623759, 0.612205222913043, 0.622407689325758,
0.624305662727272, 2064137.7, 2072479.5, 2107310.2, 78.0951697878842,
75.4532585256608, 80.7567020375659, 4.45803333333333, 4.89013333333333,
5.49806666666667, 35.0503710971087, 35.1602570318163, 35.3874976284418
), dim = c(3L, 7L), dimnames = list(NULL, c("CPI", "EXP",
"FXR", "GDP", "IMP", "LTR", "MBR")), index = structure(c(917740800,
925430400, 933379200), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo")), US = structure(c(91.55647, 86.47107, 88.24854, 167.543,
169.468, 176.035, 12992931.6, 13101422.5, 13274757.9, 237.192,
249.869, 263.535, 4.98333333333333, 5.54, 5.88333333333333,
36.6954197142406, 37.2300769607558, 37.7854895351637, 264.333333333333,
253.75, 268.083333333333, 14.3103989361702, 18.295156626506,
22.3649880668258), dim = c(3L, 8L), dimnames = list(NULL,
c("CPI", "EXP", "GDP", "IMP", "LTR", "MBR", "WHT", "WTI"
)), index = structure(c(917740800, 925430400, 933379200
), tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"
)))
W15 <- structure(c(0, 0.007258397, 0.004384407, 0.007124473, 0.046937439,
0.016027547, 0.007413191, 0.039915283, 0.059307258, 0.043306643,
0.00172888, 0.007626772, 0.005573096, 0.013747509, 0.014358853,
0.005843338, 0, 0.006594832, 0.008140664, 0.030819836, 0.023463688,
0.011279637, 0.018533225, 0.012689312, 0.015664346, 0.012533901,
0.015931198, 0.009508997, 0.007760516, 0.025719979, 0.0140926,
0.021594124, 0, 0.011126088, 0.023385409, 0.022573649, 0.011143019,
0.016084155, 0.023795867, 0.016364039, 0.003944071, 0.016867148,
0.008167946, 0.023457056, 0.258992347, 0.010657658, 0.016580201,
0.005491079, 0, 0.011315142, 0.104580939, 0.056779456, 0.057895386,
0.012069034, 0.005796411, 0.028872631, 0.008545085, 0.030086389,
0.037530141, 0.020293891, 0.29541545, 0.21235005, 0.07231354,
0.05097169, 0, 0.03739433, 0.0713611, 0.16024703, 0.28559193,
0.2961008, 0.15012195, 0.19670219, 0.07971904, 0.07721311, 0.1913896,
0.14341162, 0.28888082, 0.06898971, 0.63380519, 0.20780404, 0,
0.34392078, 0.23658324, 0.14555799, 0.13237178, 0.52763098, 0.16813418,
0.49346108, 0.59132537, 0.22004493, 0.003238443, 0.005956027,
0.001707756, 0.004608563, 0.004796915, 0.01827949, 0, 0.0159985,
0.003137514, 0.003756785, 0.007008386, 1e-13, 0.01999201, 0.00527625,
0.01447442, 0.03948706, 0.02485663, 0.00614089, 0.02541879, 0.02859312,
0.01582243, 0.03790926, 0, 0.01375526, 0.02361541, 0.02002204,
0.11864119, 0.02024388, 0.01622535, 0.0231229, 0.19705161, 0.051129,
0.03127153, 0.03016873, 0.18225716, 0.09395666, 0.02823832, 0.04925955,
0, 0.15915297, 0.04544574, 0.16467115, 0.02033042, 0.02784839,
0.0996567, 0.083743087, 0.03629164, 0.013005784, 0.008077664,
0.121588139, 0.023785858, 0.02106277, 0.044982132, 0.091134769,
0, 0.038475866, 0.11478393, 0.024775274, 0.01294248, 0.045654965,
0.002817707, 0.023071514, 0.0024476, 0.011303583, 0.036704451,
0.02975208, 0.016255829, 0.023112611, 0.020704182, 0.025561642,
0, 0.002831056, 0.111771946, 0.016490506, 0.011155943, 0.01022532,
0.01913693, 0.003576951, 0.006503726, 0.02313183, 0.01198395,
1e-12, 0.072994, 0.0415516, 0.04920629, 0.001863715, 0, 0.02051937,
0.007694036, 0.0182116, 0.003622021, 0.00723971, 0.00228576,
0.012022732, 0.007863642, 0.027084105, 0.037849027, 0.012945634,
0.003264646, 0.008246546, 0.05239551, 0.016648586, 0, 0.017044619,
0.006858711, 0.04911127, 0.02892588, 0.02747133, 0.07405134,
0.032128, 0.31747091, 0.06570103, 0.05529758, 0.02602394, 0.02111569,
0.04327837, 0.02200737, 0.06978965, 0, 0.05006517, 0.14128282,
0.25672909, 0.75431882, 0.11667677, 0.24267487, 0.25782437, 0.29108658,
0.19615167, 0.26141669, 0.19974064, 0.06667796, 0.14661014, 0.08606091,
0.14544467, 0), dim = c(15L, 15L), dimnames = list(c("AU", "BR",
"CA", "CH", "CN", "EA", "IL", "IN", "JP", "KR", "RU", "SA", "TR",
"UK", "US"), c("AU", "BR", "CA", "CH", "CN", "EA", "IL", "IN",
"JP", "KR", "RU", "SA", "TR", "UK", "US")))
mod15 <- bgvar(Data = data15, W = W15, draws = 10)
How are they different in structure? If there is no difference at all, what else can I do to try to debug besides comparison of dputs? Can it have anything to do with pandas functions that write into Excel spreadsheets?
There's a bug in the BGVAR
package. It tries to extract the names of variables from strings like
[1] "AU.CPI" "AU.EXP" "AU.FXR" "AU.GDP" "AU.IMP" "AU.LTR" "AU.MBR"
[8] "BR.CPI" "BR.EXP" "BR.FXR" "BR.GDP" "BR.IMP" "BR.LTR" "BR.MBR"
...
The problem is, it uses a bad method to do it. So the country name BR
matches the variable name MBR
, and then things get really messed up. (There's also a problem with TR
matching LTR
.)
This happens in this line of code:
endnames <- unlist(lapply(strsplit(colnames(xglobal)[grepl(cnames[[cc]],colnames(xglobal))],".",fixed=TRUE),
function(l)l[2]))
from around here: https://github.com/mboeck11/BGVAR/blob/7498d4d3a5b14ca24b391a2637245970dd245036/R/utils.R#L80
It's probably not hard to fix (just require that the country name matches the first part of the string), but there are likely other locations where the same sort of thing happens.