将 ts 对象类强制转换为 hts 对象 Rob J Hyndman 的 hts 包

发布于 2024-11-17 16:58:11 字数 4066 浏览 2 评论 0原文

想要探索文档中示例之外的包,但陷入困境 将 ts 对象类强制转换为 hts 对象:

Error in dimnames(x) <- dn : 
  length of 'dimnames' [2] not equal to array extent

有什么想法吗?

#to replicate error run below
#install.packages("hts") # if not already installed

# Note:
# Series "AA","AB","AC","AD","AE" are children to "A"
# Series "BA","BB","BC" are children to "B"


require(hts)
Month <- c("2007-02-28","2007-03-31","2007-04-30","2007-05-31","2007-06-30","2007-07-31","2007-08-31"
          ,"2007-09-30","2007-10-31","2007-11-30","2007-12-31","2008-01-31","2008-02-29","2008-03-31"
          ,"2008-04-30","2008-05-31","2008-06-30","2008-07-31","2008-08-31","2008-09-30","2008-10-31"
          ,"2008-11-30","2008-12-31","2009-01-31","2009-02-28","2009-03-31","2009-04-30","2009-05-31"
          ,"2009-06-30","2009-07-31","2009-08-31","2009-09-30","2009-10-31","2009-11-30","2009-12-31"
          ,"2010-01-31","2010-02-28","2010-03-31","2010-04-30","2010-05-31","2010-06-30","2010-07-31"
          ,"2010-08-31","2010-09-30","2010-10-31","2010-11-30","2010-12-31","2011-01-31","2011-02-28"
          ,"2011-03-31","2011-04-30","2011-05-31","2011-06-30")
Month <- as.Date(Month, "%Y-%m-%d")
mdt <- min(Month)
AA <- c(145,51,-202,-187,-266,-187,119,-75,-210,-24,26,33,355,219,-17,-141,-281,-42,-64,-3,-21,-86,-51
      ,-115,248,193,-91,-229,-193,-220,-90,-50,-100,-121,-181,-131,37,24,-65,-213,-224,-257,-151,75,-184
      ,-480,-310,-134,97,146,-140,-310,-306)
AB <- c(3395,2463,1344,1123,-1136,-906,67,-41,-1429,-578,-14938,-7378,1739,1396,1252,298,444,-42,1439,1538
       ,306,-882,-805,-961,2727,542,-1460,-1570,1518,-85,1873,-636,-1258,601,252,-598,919,1199,-790,-813,-1283
       ,-1333,-758,794,-1865,-525,-3056,-1789,1039,998,-226,-1336,-1873)
AC <- c(4429,1139,1506,-4144,-5875,-1507,12329,4780,685,2119,-5755,-9353,3032,4001,2338,-4273,-1022,-2508
       ,4504,2289,916,-940,-4451,-1463,-293,1692,832,-6576,-524,1981,6002,367,-1328,-22,1221,-1961,3146
       ,1465,945,-6478,-3333,-1674,2307,5733,-2277,-1688,-5946,-4596,-698,1716,52,-9328,-4540)
AD <- c(1894,1265,650,-404,-1143,-883,-340,849,-563,420,100,-5107,1587,937,602,68,-1231,-1389,-156,1479
       ,-25,-444,-167,-371,1650,661,108,-2267,836,-1437,-1259,-601,-1431,-751,-1585,-126,-422,406,390,-194
       ,-633,-1366,-2301,3660,-2761,-910,-1893,-3266,-1580,-323,-257,-3622,-1584)
AE <- c(2303,2088,1419,-236,-327,562,409,1694,239,459,3601,-6524,2872,2602,1280,-63,-1241,-88,1321,-512,-402
       ,-136,-388,-699,3854,1740,187,-1600,-903,-324,-1197,-1045,-997,-108,-231,-1515,579,2784,-504,-1627,-1600
       ,-1236,-1759,5529,-1144,-1358,-4006,-1481,-328,1797,83,-1133,-1342)
BA <- c(2679,609,-6,283,-540,-442,530,-7,-17413,1002,-853,-1965,-1298,1213,-1507,-559,-2113,-1442,-228,329
       ,-2130,-3266,-2584,-1842,-5106,1640,-289,5423,-1051,15337,-818,1565,-533,-762,-630,-2154,190,188,-1569
       ,-2596,-4334,-2969,-3304,-2038,-3532,-1808,-679,-2954,-1249,1304,-106,-892,-2758)
BB <- c(-1388,785,-4372,-5128,-6652,-10177,-12592,-8006,-1489,-5107,-2952,-7470,396,3562,5779,1858,-6067,-4875
       ,-4132,-3066,-4386,-2217,-11683,-1841,4100,1508,-2061,-4579,-1840,-3147,-5087,-2529,-1369,-4674,-4578
       ,-3754,269,1645,9393,-3453,-5223,-6060,-5364,-4301,-2125,-3467,-2791,-3605,-262,4006,3058,-111,648)
BC <- c(4335,3685,-1075,-114,-94,-1672,-1551,-819,15167,-2060,-3176,-3585,3119,3775,595,931,-348,-2088,-1690
       ,169,-2936,-2615,-4086,-2397,6403,3334,-1208,-2952,-3246,-20580,-5820,-1917,-3430,-243,-3362,-3443,541
       ,1669,-1356,-2669,-3551,-3826,-3882,-2831,-2372,-2181,-2769,-4518,1278,4119,1582,-181,-1447)

tsdata <- data.frame(Month,AA,AB,AC,AD,AE,BA,BB,BC)
mts <- ts(tsdata, start =c(as.numeric(format.Date(mdt, "%Y")),as.numeric(format.Date(mdt, "%m"))), frequency=12 )
# "AA","AB","AC","AD","AE" are children to "A"
# "BA","BB","BC" are children to "B"
g.mts  <-  rbind(c("AA","AB","AC", "AD","AE","BA","BB","BC"),  c("A","A","A","A","A","B","B","B"))
hts.mts  <-  hts(y  =  mts,  g  =  g.mts)

Would like to explore package beyond examples in docs but get hung up on
coercion of ts object class to hts object:

Error in dimnames(x) <- dn : 
  length of 'dimnames' [2] not equal to array extent

Any thoughts?

#to replicate error run below
#install.packages("hts") # if not already installed

# Note:
# Series "AA","AB","AC","AD","AE" are children to "A"
# Series "BA","BB","BC" are children to "B"


require(hts)
Month <- c("2007-02-28","2007-03-31","2007-04-30","2007-05-31","2007-06-30","2007-07-31","2007-08-31"
          ,"2007-09-30","2007-10-31","2007-11-30","2007-12-31","2008-01-31","2008-02-29","2008-03-31"
          ,"2008-04-30","2008-05-31","2008-06-30","2008-07-31","2008-08-31","2008-09-30","2008-10-31"
          ,"2008-11-30","2008-12-31","2009-01-31","2009-02-28","2009-03-31","2009-04-30","2009-05-31"
          ,"2009-06-30","2009-07-31","2009-08-31","2009-09-30","2009-10-31","2009-11-30","2009-12-31"
          ,"2010-01-31","2010-02-28","2010-03-31","2010-04-30","2010-05-31","2010-06-30","2010-07-31"
          ,"2010-08-31","2010-09-30","2010-10-31","2010-11-30","2010-12-31","2011-01-31","2011-02-28"
          ,"2011-03-31","2011-04-30","2011-05-31","2011-06-30")
Month <- as.Date(Month, "%Y-%m-%d")
mdt <- min(Month)
AA <- c(145,51,-202,-187,-266,-187,119,-75,-210,-24,26,33,355,219,-17,-141,-281,-42,-64,-3,-21,-86,-51
      ,-115,248,193,-91,-229,-193,-220,-90,-50,-100,-121,-181,-131,37,24,-65,-213,-224,-257,-151,75,-184
      ,-480,-310,-134,97,146,-140,-310,-306)
AB <- c(3395,2463,1344,1123,-1136,-906,67,-41,-1429,-578,-14938,-7378,1739,1396,1252,298,444,-42,1439,1538
       ,306,-882,-805,-961,2727,542,-1460,-1570,1518,-85,1873,-636,-1258,601,252,-598,919,1199,-790,-813,-1283
       ,-1333,-758,794,-1865,-525,-3056,-1789,1039,998,-226,-1336,-1873)
AC <- c(4429,1139,1506,-4144,-5875,-1507,12329,4780,685,2119,-5755,-9353,3032,4001,2338,-4273,-1022,-2508
       ,4504,2289,916,-940,-4451,-1463,-293,1692,832,-6576,-524,1981,6002,367,-1328,-22,1221,-1961,3146
       ,1465,945,-6478,-3333,-1674,2307,5733,-2277,-1688,-5946,-4596,-698,1716,52,-9328,-4540)
AD <- c(1894,1265,650,-404,-1143,-883,-340,849,-563,420,100,-5107,1587,937,602,68,-1231,-1389,-156,1479
       ,-25,-444,-167,-371,1650,661,108,-2267,836,-1437,-1259,-601,-1431,-751,-1585,-126,-422,406,390,-194
       ,-633,-1366,-2301,3660,-2761,-910,-1893,-3266,-1580,-323,-257,-3622,-1584)
AE <- c(2303,2088,1419,-236,-327,562,409,1694,239,459,3601,-6524,2872,2602,1280,-63,-1241,-88,1321,-512,-402
       ,-136,-388,-699,3854,1740,187,-1600,-903,-324,-1197,-1045,-997,-108,-231,-1515,579,2784,-504,-1627,-1600
       ,-1236,-1759,5529,-1144,-1358,-4006,-1481,-328,1797,83,-1133,-1342)
BA <- c(2679,609,-6,283,-540,-442,530,-7,-17413,1002,-853,-1965,-1298,1213,-1507,-559,-2113,-1442,-228,329
       ,-2130,-3266,-2584,-1842,-5106,1640,-289,5423,-1051,15337,-818,1565,-533,-762,-630,-2154,190,188,-1569
       ,-2596,-4334,-2969,-3304,-2038,-3532,-1808,-679,-2954,-1249,1304,-106,-892,-2758)
BB <- c(-1388,785,-4372,-5128,-6652,-10177,-12592,-8006,-1489,-5107,-2952,-7470,396,3562,5779,1858,-6067,-4875
       ,-4132,-3066,-4386,-2217,-11683,-1841,4100,1508,-2061,-4579,-1840,-3147,-5087,-2529,-1369,-4674,-4578
       ,-3754,269,1645,9393,-3453,-5223,-6060,-5364,-4301,-2125,-3467,-2791,-3605,-262,4006,3058,-111,648)
BC <- c(4335,3685,-1075,-114,-94,-1672,-1551,-819,15167,-2060,-3176,-3585,3119,3775,595,931,-348,-2088,-1690
       ,169,-2936,-2615,-4086,-2397,6403,3334,-1208,-2952,-3246,-20580,-5820,-1917,-3430,-243,-3362,-3443,541
       ,1669,-1356,-2669,-3551,-3826,-3882,-2831,-2372,-2181,-2769,-4518,1278,4119,1582,-181,-1447)

tsdata <- data.frame(Month,AA,AB,AC,AD,AE,BA,BB,BC)
mts <- ts(tsdata, start =c(as.numeric(format.Date(mdt, "%Y")),as.numeric(format.Date(mdt, "%m"))), frequency=12 )
# "AA","AB","AC","AD","AE" are children to "A"
# "BA","BB","BC" are children to "B"
g.mts  <-  rbind(c("AA","AB","AC", "AD","AE","BA","BB","BC"),  c("A","A","A","A","A","B","B","B"))
hts.mts  <-  hts(y  =  mts,  g  =  g.mts)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星軌x 2024-11-24 16:58:11

为了适当地适应所指出的层次结构,

系列“AA”、“AB”、“AC”、“AD”、“AE”是“A”的子级
系列“BA”,“BB”,“BC”是“B”

更改的子级:

g.mts <- rbind(c(“AA”,“AB”,“AC”,“AD”,“AE”, "BA","BB","BC"), c("A","A","A","A","A","B","B","B"))

至:

g.mts <- rbind(c("A","A","A","A","A","B","B","B"),c("AA","AB","AC" 、“AD”、“AE”、“BA”、“BB”、“BC”))

To appropriately accommodate the noted hierarchy

Series "AA","AB","AC","AD","AE" are children to "A"
Series "BA","BB","BC" are children to "B"

change:

g.mts <- rbind(c("AA","AB","AC", "AD","AE","BA","BB","BC"), c("A","A","A","A","A","B","B","B"))

to:

g.mts <- rbind(c("A","A","A","A","A","B","B","B"),c("AA","AB","AC", "AD","AE","BA","BB","BC"))

随风而去 2024-11-24 16:58:11

我不太清楚您要做什么,但似乎可能存在 mts 中有 9 列但 g.mts 中有 8 列的问题。例如

hts.mts  <-  hts(y  =  mts[,-1],  g  =  g.mts)

不产生错误消息

I am not quite clear what you are trying to do, but it seems there may be an issue of having 9 columns in mts but 8 in g.mts. For example

hts.mts  <-  hts(y  =  mts[,-1],  g  =  g.mts)

does not produce the error message

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文