如何平滑 R 上的时间序列?
How to smooth a times series on R?
我的数据集包含对 Facebook 帖子及其互动的多次日常观察。我使用的是一年 (YTD) 的移动时间间隔。
出于本研究的目的,我将您在该数据样本中看到的交互类型分开。数据已经很长了,我相信它远非整洁,但它有助于 ggplot 完成工作。
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
我的 ggplot 代码是:
ggplot(YTD, aes(day, value, color = variable)) + geom_line() +
facet_wrap(~ from_name) + theme(legend.position = "bottom")
我明白了:
从图中可以看出,每天都有很大的差异,这使得图表看起来非常混乱。我还尝试使用我正在研究的 var 的 scale_y_log10
来记录这些值,但是图表太糟糕了......
如何绘制一条平滑的线,以便在所有方差噪音之外显示趋势?
您可以使用库 methods
中的 stat_smooth
。在您的情况下,它看起来像
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)
如果您想要一些简单的东西,另一种选择是直接使用矩形或三角形平滑技术来平滑数据。
我的数据集包含对 Facebook 帖子及其互动的多次日常观察。我使用的是一年 (YTD) 的移动时间间隔。
出于本研究的目的,我将您在该数据样本中看到的交互类型分开。数据已经很长了,我相信它远非整洁,但它有助于 ggplot 完成工作。
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
我的 ggplot 代码是:
ggplot(YTD, aes(day, value, color = variable)) + geom_line() +
facet_wrap(~ from_name) + theme(legend.position = "bottom")
我明白了:
从图中可以看出,每天都有很大的差异,这使得图表看起来非常混乱。我还尝试使用我正在研究的 var 的 scale_y_log10
来记录这些值,但是图表太糟糕了......
如何绘制一条平滑的线,以便在所有方差噪音之外显示趋势?
您可以使用库 methods
中的 stat_smooth
。在您的情况下,它看起来像
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)
如果您想要一些简单的东西,另一种选择是直接使用矩形或三角形平滑技术来平滑数据。