Juppes
Juppes

Reputation: 169

Smoothing function on data frame

I try to use various smoothing functions but did not find the right one for me. I have a data frame read in from a csv file which contains sensor data and has a date time stamp for every row:

df
#datetime value1 value2 value3 ...
#2015-10-2 05-10-12 0.1 1000 28 ...
#2015-10-2 05-10-14 0.21 2500 17 ...
#2015-10-2 05-10-19 0.52 1700 37 ...
#2015-10-2 05-11-01 0.31 1530 42 ...
#2015-10-2 05-11-03 0.25 1956 33 ...
#2015-10-2 05-11-10 0.63 2750 22 ...
#2015-10-2 05-11-30 0.23 322 32 ...
#2015-10-2 15-00-43 0.12 933 17 ...
#2015-10-3 10-43-52 0.03 3244 43 ...
#2015-10-3 12-11-45 0.73 2334 12 ...
#2015-10-4 00-32-34 0.54 2321 27 ...
...

As you see the datetime column has no constant intervals. I think thats kind of a problem. Now I want to smoothen the data frame column value1.

Ok, my question has been put on hold so I like to ask more in detail. I am sorry, I am new to R (since a few days) and also new to this forum (since yesterday) and so I must apologize for not being clear enough with my question and for not knowing every formatting rule. But I will try my best to get involved!

I picked an example from this page https://stats.stackexchange.com/questions/30975/how-to-add-non-linear-trend-line-to-a-scatter-plot-in-r:

n <- 10
x <-seq(n)
y <- rnorm(n, 50 + 30 * x^(-0.2), 1)
Data <- data.frame(x,y)

plot(y ~ x, Data)

loess_fit <- loess(y ~ x, Data)
lines(Data$x, predict(loess_fit), col = "blue")

nls_fit <- nls(y ~ a + b * x^(-c), Data, start = list(a = 80, b = 20, c = 0.2))
lines(Data$x, predict(nls_fit), col = "red")

This example shows clear how to add a smoothing trend line to the value display.

Now my problem is that I do not have subsequent numeric values as x but I have datetime values. So I modify the above example to this:

x <- strptime(c("2015-10-02 11:07:43", "2015-10-02 12:09:45", "2015-10-02 15:10:10", "2015-10-02 18:00:23",
"2015-10-02 22:31:12", "2015-10-03 02:01:53", "2015-10-03 02:05:52", "2015-10-03 04:12:37",
"2015-10-03 07:47:08", "2015-10-03 11:43:41"), format = "%Y-%m-%d %H:%M:%S")
Data <- data.frame(x,y)

plot(y ~ x, Data)

loess_fit <- loess(y ~ x, Data)
lines(Data$x, predict(loess_fit), col = "blue")

nls_fit <- nls(y ~ a + b * x^(-c), Data, start = list(a = 80, b = 20, c = 0.2))
lines(Data$x, predict(nls_fit), col = "red")

If I run this example I get an error in the line

loess_fit <- loess(y ~ x, Data)

saying:

Error in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,  :
(converted from warning) NAs introduced by coercion

Any hint for getting the smoothing trend also to these data?

Upvotes: 2

Views: 1570

Answers (1)

Roland
Roland

Reputation: 132576

help("loess") specifies that the function expects a "numeric response and one to four numeric predictors". A POSIXlt variable is not a numeric variable. However, this works:

loess_fit <- loess(y ~ as.numeric(x), Data)
lines(Data$x, predict(loess_fit), col = "blue")

PS: I generally advise to avoid POSIXlt and use as.POSIXct instead of strptime. You should also always specify the time zone explicitly.

Upvotes: 1

Related Questions