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!

Geotagging with Exif Metadata

Recently I was coding a prototype using the Google Maps API to geotag objects and then search by location. Geospatial metadata would be imported automatically when users uploaded files. Alternately, items could be manually geotagged by resolving an address or place name (using the Geocoding API) and then adjusting the resulting coordinates on the map.

For my demo I wanted to use photographs with geospatial metadata baked in. Since I’m still a few months away from owning a (GPS enabled) smartphone, I had to find a way to manually set the latitude and longitude after taking some photos. First, to get the coordinates, I used a javascript bookmarklet to alert the current centred latitude and longitude in Google Maps. I aliased this as “geo“:

javascript:var c=window.gApplication.getMap().getCenter();alert(c.lat()+' '+c.lng())

A quick glance at the man page of exiv2 showed that Exif uses triplets of rational numbers for degrees, minutes, and seconds along with cardinal directions instead of simple decimal numbers. So for example, “43.54861810, -80.26079178″ becomes 43/1 32/1 55/1 N and 80/1 15/1 39/1 W (denominator is always 1). I wrote a python script “geo.py” to do the conversion and invoke exiv2:

#!/usr/bin/python
import sys, os
lat = float(sys.argv[1])
lng = float(sys.argv[2])
path = sys.argv[3]

def format(dec):
  dec = abs(dec)
  d = int(dec)
  m = int(dec*60%60)
  s = int(dec*3600%60)
  return "%02d/1 %02d/1 %02d/1" % (d, m, s)

def lat_cardinal(dec):
  return 'N' if dec >= 0 else 'S'

def lng_cardinal(dec):
  return 'E' if dec >= 0 else 'W'

os.system( "exiv2 \
  -M\"set Exif.GPSInfo.GPSLatitude %s\" \
  -M\"set Exif.GPSInfo.GPSLatitudeRef %s\" \
  -M\"set Exif.GPSInfo.GPSLongitude %s\" \
  -M\"set Exif.GPSInfo.GPSLongitudeRef %s\" \
  %s" % (format(lat), lat_cardinal(lat), format(lng), lng_cardinal(lng), path)
)

Now all I have to do is find the location in Google Maps, type ‘geo‘ to get the coordinates, and run geo.py latitude longitude file.jpg!