Time Series in R

I track my body weight in an extended sheet, but I want to improve the experience using R. I tried to find some information about time series analysis in R, but I failed.

The data that I have is in the following format:

date -> weight -> body-fat-percentage -> water-percentage 

eg.

 10/08/09 -> 84.30 -> 18.20 -> 55.3 

What i want to do

plot weight and exponential moving average of time

How can i achieve this?

+4
source share
3 answers

Read the data in R using x <- read.csv(filename) . Make sure dates enter 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 
+6
source

It looks like you need to handle irregularly spaced time series, so ts is not an option. Use one of the other time series libraries. The zoo is the most widely used, but some other options are timeSeries, xts, fts and it. Take a look at the CRAN view: http://cran.r-project.org/web/views/TimeSeries.html .

One call that I am seeing right now is the date format. I suggest either reformatting the date first in your data, or using the format () function in R, but you will need to convert them to a Date or POSIX object in R to use it with the time series package.

You can use the read.zoo () function to read the time series in your file. Also look at the vignette. For EWMA, I think there are several options. Rmetrics and TTR have versions.

I will send an example when I get to the computer. By the way, there are many resources on this subject. Take a look at this book: http://www.rmetrics.org/ebooks/TimeSeriesFAQ.pdf .

+2
source

There's a really good book about Time Series in R that came out this summer

http://www.amazon.com/Introductory-Time-R-Use/dp/0387886974

if you want to delve into the subject.

-k

0
source

All Articles