You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WaveletComp包小波分析日历日期报错问题求助

问题描述

使用WaveletComp包做小波分析时,希望在X轴显示年份,但始终报错:

"Please check your calendar dates, format and time zone: dates may not be in an unambiguous format or chronological. The default numerical axis was used instead."

尝试调整日期格式后问题依旧,以下是原代码:

library('WaveletComp')
firecount <- data.frame( YEAR = c("1986-01-01","1987-01-01","1988-01-01","1989-01-01","1990-01-01"
                                  ,"1991-01-01","1992-01-01","1993-01-01","1994-01-01","1995-01-01"
                                  ,"1996-01-01","1997-01-01","1998-01-01","1999-01-01","2000-01-01"
                                  ,"2001-01-01","2002-01-01","2003-01-01","2004-01-01","2005-01-01"
                                  ,"2006-01-01","2007-01-01","2008-01-01","2009-01-01","2010-01-01"
                                  ,"2011-01-01","2012-01-01","2013-01-01","2014-01-01","2015-01-01"
                                  ,"2016-01-01","2017-01-01","2018-01-01","2019-01-01","2020-01-01"
),
                          COUNT = c(3,5,4,0,0,0,13,0,2,3,0,1,0,3,15,13,
                                   59,18,42,16,20,46,44,8,68,18,7,3,9
                                   ,48,7,48,23,84,54)
)
flycount$YEAR <- as.Date(as.character(firecount$YEAR),"%Y")
my.w <- analyze.wavelet(flycount, my.series = "COUNT",
                        loess.span = 0.5,
                        dt = 1, dj = 1/35,
                        lowerPeriod = 2, upperPeriod = 12,
                        make.pval = TRUE, n.sim = 10,
)

wt.image(my.w, color.key = "interval", n.levels = 15,
         legend.params = list(lab = "fire occurrence wavelet", label.digits = 2),
         periodlab = "periods (years)",
         # Concerning item 1 above --- plot the square root of power:
         exponent = 0.5,
         # Concerning item 2 above --- time axis:
         show.date = TRUE, 
         date.format = "%F", 
         timelab = "",
         spec.time.axis = list(at = c(paste(1986:2020, "-01-01", sep = "")),
                               labels = c(1986:2020)),
         timetcl = -0.5)
问题分析与解决

代码里有三个关键错误导致日期解析失败:

  • 变量名笔误:定义的数据框是firecount,但后续操作错误使用了flycount,导致日期列未正确赋值。
  • 日期转换格式错误:原YEAR列已经是YYYY-MM-DD格式,用%Y作为格式符转换会丢失月日信息,导致日期不明确;且无需额外转成字符型,直接用as.Date()即可解析。
  • 自定义时间轴类型错误:spec.time.axis$at传入的是字符串,需要转为Date类型才能被WaveletComp识别。

修正后的代码如下:

library('WaveletComp')
firecount <- data.frame( YEAR = c("1986-01-01","1987-01-01","1988-01-01","1989-01-01","1990-01-01"
                                  ,"1991-01-01","1992-01-01","1993-01-01","1994-01-01","1995-01-01"
                                  ,"1996-01-01","1997-01-01","1998-01-01","1999-01-01","2000-01-01"
                                  ,"2001-01-01","2002-01-01","2003-01-01","2004-01-01","2005-01-01"
                                  ,"2006-01-01","2007-01-01","2008-01-01","2009-01-01","2010-01-01"
                                  ,"2011-01-01","2012-01-01","2013-01-01","2014-01-01","2015-01-01"
                                  ,"2016-01-01","2017-01-01","2018-01-01","2019-01-01","2020-01-01"
),
                          COUNT = c(3,5,4,0,0,0,13,0,2,3,0,1,0,3,15,13,
                                   59,18,42,16,20,46,44,8,68,18,7,3,9
                                   ,48,7,48,23,84,54)
)
# 修正日期转换:直接解析YYYY-MM-DD格式的字符串
firecount$YEAR <- as.Date(firecount$YEAR)
# 使用正确的变量名firecount
my.w <- analyze.wavelet(firecount, my.series = "COUNT",
                        loess.span = 0.5,
                        dt = 1, dj = 1/35,
                        lowerPeriod = 2, upperPeriod = 12,
                        make.pval = TRUE, n.sim = 10
)

wt.image(my.w, color.key = "interval", n.levels = 15,
         legend.params = list(lab = "fire occurrence wavelet", label.digits = 2),
         periodlab = "periods (years)",
         exponent = 0.5,
         show.date = TRUE, 
         date.format = "%F", 
         timelab = "",
         # 将at的字符串转为Date类型
         spec.time.axis = list(at = as.Date(paste(1986:2020, "-01-01", sep = "")),
                               labels = 1986:2020),
         timetcl = -0.5)

内容的提问来源于stack exchange,提问作者Qiyue Zhang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 16:06:27