首页 > 解决方案 > 尝试分解时间序列时出错:非数字参数

问题描述

我是 R 新手,正在做一项任务,我将一些 JSON 数据导入到 (1) 创建时间序列图和 (2) 分解时间序列。这是我正在努力的分解功能。这是有效的...

# Import JSON & convert to data.frame
aor <- fromJSON.....
aor <- as.data.frame......

# Combine the year and month into a date format
aor$date <- as.yearmon(paste(aor$year, aor$month), "%Y %m")

# Ensure data is float not chr
aor$mwh <- as.numeric(aor$mwh)

# Prep the data.frame for time-series analysis by converting to xts
aor <- xts(x = aor, order.by = aor$date)

# Successfully output a time-series graph.
dygraph(aor)

这是到目前为止的 aor 示例...

> aor
         mwh         date      
Jan 2001 "  1.42000" "Jan 2001"
Feb 2001 "  1.28400" "Feb 2001"
Mar 2001 "  1.25800" "Mar 2001"
Apr 2001 "  1.53600" "Apr 2001"
May 2001 "  1.47100" "May 2001"
Jun 2001 "  1.91800" "Jun 2001"
Jul 2001 "  2.37800" "Jul 2001"

> dput(head(aor, 10))
 
structure(c("  1.42000", "  1.28400", "  1.25800", "  1.53600", 
"  1.47100", "  1.91800", "  2.37800", "  2.47000", "  1.65100", 
"  1.58100", "Jan 2001", "Feb 2001", "Mar 2001", "Apr 2001", 
"May 2001", "Jun 2001", "Jul 2001", "Aug 2001", "Sep 2001", "Oct 2001"
), .Dim = c(10L, 2L), .Dimnames = list(NULL, c("mwh", "date")))

我认为会产生分解图的代码......

ts <- as.ts(aor)

> ts
             mwh     date
Jan  1   1.42000 Jan 2001
Feb  1   1.28400 Feb 2001
Mar  1   1.25800 Mar 2001
Apr  1   1.53600 Apr 2001
May  1   1.47100 May 2001
Jun  1   1.91800 Jun 2001
Jul  1   2.37800 Jul 2001


d <- decompose(ts)
plot(d)

尝试分解 ts 时出现此错误...

`-.default`(x,趋势)中的错误:二进制运算符的非数字参数

标签: rtime-series

解决方案


推荐阅读