当 x 轴为日期时,无法使用 `loess` 重现 `stat_smooth`。

14

我正在使用ggplot2对我的数据进行平滑估计

ggplot(yy)+geom_smooth(aes(x=Date,y=value),method='loess')

enter image description here

它运行良好。现在,当我尝试直接使用loess函数进行复制时,我会收到一个错误:

loess(value~Date,yy)
Error in simpleLoess(y, x, w, span, degree, parametric, drop.square, normalize,  : 
  NA/NaN/Inf in foreign function call (arg 2)
In addition: Warning message:
In simpleLoess(y, x, w, span, degree, parametric, drop.square, normalize,  :
  NAs introduced by coercion

我认为ggplot2版本可行是因为它设置了一些默认参数。

这里是我的数据 yy:

yy <- data.table::data.table(
  Date = as.Date(c(
    "1997-01-01", "1997-02-01", "1997-03-01", "1997-05-01", "1997-07-01",
    "1997-09-01", "1997-10-01", "1997-12-01", "1998-01-01", "1998-02-01",
    "1998-04-01", "1998-07-01", "1998-09-01", "2002-04-01", "2004-12-01",
    "2005-12-01", "2006-12-01", "2007-12-01", "2009-10-01", "1997-06-01",
    "1997-11-01", "2002-03-01", "2002-07-01", "2002-09-01", "2003-01-01",
    "2003-05-01", "2003-07-01", "2003-09-01", "2003-11-01", "2004-01-01",
    "2004-04-01", "2004-08-01", "2004-09-01", "2004-11-01", "2005-05-01",
    "2005-08-01", "2005-11-01", "2007-02-01", "2007-06-01", "2007-09-01",
    "2007-11-01", "2009-03-01", "2009-08-01", "1999-04-01", "1999-05-01",
    "1999-06-01", "1999-07-01", "2008-02-01", "2008-06-01", "2008-11-01",
    "2008-04-01", "1998-03-01", "1998-05-01", "1998-06-01", "1998-08-01",
    "1998-10-01", "1998-11-01", "1998-12-01", "1999-01-01", "1999-02-01",
    "1999-03-01", "1999-08-01", "1999-09-01", "1999-10-01", "1999-11-01",
    "2002-08-01", "2004-05-01", "2004-06-01", "2004-07-01", "2004-10-01",
    "2000-03-01", "2000-04-01", "2000-05-01", "2000-06-01", "2000-07-01",
    "2000-08-01", "2000-09-01", "2000-10-01", "2000-11-01", "2001-02-01",
    "2001-04-01", "2001-05-01", "2001-06-01", "2001-07-01", "2001-08-01",
    "2001-09-01", "2001-11-01", "2002-01-01", "2002-06-01", "2002-12-01",
    "2003-06-01", "2005-03-01", "2001-03-01", "2002-05-01", "2002-11-01",
    "2003-10-01", "2003-12-01", "2006-08-01", "2007-05-01", "2008-05-01",
    "2009-05-01", "2001-10-01", "2001-12-01", "2002-02-01", "2002-10-01",
    "2003-02-01", "2003-03-01", "2003-04-01", "2003-08-01", "2005-01-01",
    "2006-01-01", "2007-08-01", "2008-09-01", "2009-11-01", "2009-06-01"
  )),
  value = c(
    31.8333333333333, 38.2, 28.8333333333333, 29.1666666666667, 50.6,
    28.8333333333333, 34.6, 34.4, 34.4, 35.6, 79.1666666666667, 96.5714285714286,
    124, 29.2, 8, 10, 66.6, 10, 20.25, 23.75, 49, 93, 49.7142857142857, 40.5,
    73.8, 55, 71.2, 32.6, 27.25, 27.5, 24.75, 30.2, 21.4, 16.2, 27.5, 26.75,
    18.75, 25.3333333333333, 39.8, 43.25, 22.6666666666667, 54.6666666666667, 67,
    112, 91.5, 93, 106.666666666667, 91, 48.6666666666667, 50, 68, 300, 280, 290,
    200, 301, 303, 300, 200, 150, 200, 200, 200, 100, 100, 63.3333333333333, 2, 3,
    2, 2, 173, 169, 62, 130, 108, 154, 65, 94, 68, 220.5, 223, 210, 211, 214, 202,
    212.333333333333, 193, 198.666666666667, 112.5, 133.666666666667, 106, 216.5,
    206, 121.5, 39, 87, 93, 118, 115, 206, 95, 213, 90, 84, 85, 54, 34, 48, 52,
    218, 187, 114, 96, 114, 42
  )
)
1个回答

19
您需要将"Date"类对象Date转换为数字向量:
R> loess(value ~ as.numeric(Date), yy)
Call:
loess(formula = value ~ as.numeric(Date), data = yy)

Number of Observations: 115 
Equivalent Number of Parameters: 4.53 
Residual Standard Error: 66.7

强制行为将在ggplot()的代码中进行 - 毕竟,那些日期需要在某个时候被分配给笛卡尔坐标才能绘制它们。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接