我在电子表格中跟踪我的体重,但我想通过使用R来改善体验.我试图在R中找到关于时间序列分析的一些信息,但我没有成功.
我这里的数据采用以下格式:
date -> weight -> body-fat-percentage -> water-percentage
Run Code Online (Sandbox Code Playgroud)
例如
10/08/09 -> 84.30 -> 18.20 -> 55.3
Run Code Online (Sandbox Code Playgroud)
我想做的事
plot
权重和指数移动平均值与时间的关系
我怎样才能做到这一点?
使用将数据读入R中x <- read.csv(filename)
.确保日期作为字符类和重量以数字形式出现.
然后使用以下内容:
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
Run Code Online (Sandbox Code Playgroud)