toku_mo
toku_mo

Reputation: 120

How to smooth a times series on R?

My dataset consists on multiple daily observations of Facebook posts and their interactions. I am using a moving time interval of a year (YTD).

For the purposes of this study I am separating the kind of interaction as you can see in this sample of the data. The data is already in long form, I believe it is far from being tidy but it helps ggplot do the job.

from_name        created_time                              id       variable value        day
1440 Toyota Perú (Grupo Oficial) 2014-03-10 11:01:25 123538507674613_782719435089847    likes_count   140 2014-03-10
5491 Toyota Perú (Grupo Oficial) 2014-03-10 11:01:25 123538507674613_782719435089847 comments_count    10 2014-03-10
9542 Toyota Perú (Grupo Oficial) 2014-03-10 11:01:25 123538507674613_782719435089847   shares_count    17 2014-03-10
1439 Toyota Perú (Grupo Oficial) 2014-03-10 15:49:25 123538507674613_782845248410599    likes_count    61 2014-03-10
5490 Toyota Perú (Grupo Oficial) 2014-03-10 15:49:25 123538507674613_782845248410599 comments_count     1 2014-03-10
9541 Toyota Perú (Grupo Oficial) 2014-03-10 15:49:25 123538507674613_782845248410599   shares_count     0 2014-03-10

My ggplot code is:

ggplot(YTD, aes(day, value, color = variable)) + geom_line() +  
facet_wrap(~ from_name) + theme(legend.position = "bottom")

And I get this:

enter image description here

As you can see from the graph, there is a lot of variance from day to day which makes the graph look extremely messy. I also tried to log the values using scale_y_log10 of the var I am studying but the graph was just awful...

How can I draw a smooth line so I can show a tendency outside all the variance noise?

Upvotes: 3

Views: 5569

Answers (1)

cagliari2005
cagliari2005

Reputation: 271

You can use stat_smooth from the library methods. In your case it would look something like

p <- ggplot(YTD, aes(day, value, color = variable)) + geom_line() + 
facet_wrap(~ from_name) + theme(legend.position = "bottom")

# Apply a locally weighted regression
p + stat_smooth(method = "loess", formula = y ~ x, size = 1)

Another option is to smooth your data directly using rectangular or triangular smoothing techniques if you want something simple.

Upvotes: 1

Related Questions