Forecasting with R

For the past month I’ve been tracking my weight daily as part of a long term project. I am also tracking all expenses for my car I bought this year for commuting. I have vague plans on calculating statistics and running analyses as I collect more data, but for now I am just accumulating data. In the meantime I am reading the R Cookbook from O’Reilly’s Data Science Starter Kit I bought recently to learn data analysis.

One neat idea I had would be to forecast tomorrow’s weight. For a little bit of instant gratification, I looked up one method for predicting the next value in a time series using an ARIMA model. I’m not yet sure if this is the best model to use in this case, but it seems like a good start, and some diagnostics suggest it may be good. Here’s a snippet I cobbled together to read my CSV data and predict the next value (format of file is “Date,Weight”):

library(zoo)
library(forecast)
data <- read.csv2(file="weight.csv", sep=",", colClasses=c("Date",NA),
                  head=FALSE, stringsAsFactors=FALSE)
dates <- data[[1]]
weights <- as.numeric(data[[2]])
ts <- zoo(weights, dates)
m <- auto.arima(ts)
predict(m)

This is probably a very crude way of forecasting, given that I’ve only just begun teaching myself data analysis, but it’s still cool nonetheless. Tomorrow’s forecast is up from today; hopefully it is wrong!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>