I am using ggplot2
to get a smoothed estimation of my data
ggplot(yy)+geom_smooth(aes(x=Date,y=value),method='loess')
It works fine. Now, when try to reproduce this using loess
function directly, I get an error:
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
I think that ggplot2
version works because it sets some default parameters.
Here my data 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
)
)
You need to convert the "Date"
class object Date
to a numeric vector:
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
That coercion will be going on internally with ggplot()
's code - after all, those dates need to be assigned to cartesian coordinates at some point in order to plot them.