Question

I am using ggplot2 to get a smoothed estimation of my data

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

enter image description here

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 :

dput(yy)
structure(list(Date = structure(c(9862, 9893, 9921, 9982, 10043, 
10105, 10135, 10196, 10227, 10258, 10317, 10408, 10470, 11778, 
12753, 13118, 13483, 13848, 14518, 10013, 10166, 11747, 11869, 
11931, 12053, 12173, 12234, 12296, 12357, 12418, 12509, 12631, 
12662, 12723, 12904, 12996, 13088, 13545, 13665, 13757, 13818, 
14304, 14457, 10682, 10712, 10743, 10773, 13910, 14031, 14184, 
13970, 10286, 10347, 10378, 10439, 10500, 10531, 10561, 10592, 
10623, 10651, 10804, 10835, 10865, 10896, 11900, 12539, 12570, 
12600, 12692, 11017, 11048, 11078, 11109, 11139, 11170, 11201, 
11231, 11262, 11354, 11413, 11443, 11474, 11504, 11535, 11566, 
11627, 11688, 11839, 12022, 12204, 12843, 11382, 11808, 11992, 
12326, 12387, 13361, 13634, 14000, 14365, 11596, 11657, 11719, 
11961, 12084, 12112, 12143, 12265, 12784, 13149, 13726, 14123, 
14549, 14396), class = "Date"), 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)), row.names = c(NA, -115L
), class = c("data.table", "data.frame"), .Names = c("Date", 
"value"), .internal.selfref = <pointer: 0x0000000000230788>)
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top