本文介绍了R中的时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在电子表格中跟踪我的体重,但我想通过使用 R 来改善体验.我试图在 R 中找到有关时间序列分析的一些信息,但没有成功.
I am tracking my body weight in a spread sheet but I want to improve the experience by using R. I was trying to find some information about time series analysis in R but I was not successful.
我这里的数据格式如下:
The data I have here is in the following format:
date -> weight -> body-fat-percentage -> water-percentage
例如
10/08/09 -> 84.30 -> 18.20 -> 55.3
我想做什么
plot
权重和指数移动平均随时间变化
plot
weight and exponential moving average against time
我怎样才能做到这一点?
How can I achieve that?
推荐答案
使用 x 将数据读入 R.确保日期作为字符类输入,权重作为数字输入.
然后使用以下内容:
Read the data into R using
x <- read.csv(filename)
. Make sure the dates come in as character class and weight as numeric.
Then use the following:
require(zoo)
require(forecast) # Needed for the ses function
x$date <- as.Date(x$date,"%m/%d/%Y") # Guessing you are using the US date format
x$weight <- zoo(x$weight,x$date) # Allows for irregular dates
plot(x$weight, xlab="Date", ylab="Weight") # Produce time plot
ewma <- as.vector(fitted(ses(ts(x$weight)))) # Compute ewma with parameter selected using MLE
lines(zoo(ewma,x$date),col="red") # Add ewma line to plot
这篇关于R中的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!