Monday, April 30, 2012

Weight loss re-done with locally-weighted polynomial regression

My buddy and epic Math fiend Chris Twigg saw the previous crappy Linear Regression graph and suggested a fancier method of using R to do a locally-weighted polynomial regression instead for a better fit line. Anyhow, in a sign that I overwhelmingly need a stronger social life on a Sunday night, I just spent a bit of time refreshing myself on how to use R and drawing a plot using this cool method. Fancy!

Here are the results:
For the Math nerds in the crowd (I know there are a lot of you), here's my weight-vs-dates scatterplot redone in the statistical program "R", using locally-weighted polynomial regression


The green line is a smoothness of 0.2, the red line is a smoothness of the default which is 2/3.

If for some reason you desperately want to see a nicer regression line for your own epic weight loss, here's the code for it. Have fun :) And don't forget to put your own embarrassingly (though, previously) high weights and dates into it instead of using mine. :)
#R program for weight loss vs. date plot using locally-weighted polynomial regression
require(graphics)
require(stats)
library(lattice)

plot(weight)
dts <- c("2011-09-11", "2011-09-17", "2011-09-18", "2011-09-24", "2011-09-25", "2011-09-29", "2011-10-02", "2011-10-08", "2011-10-09", "2011-10-15", "2011-10-17", "2011-10-22", "2011-10-30", "2011-11-06", "2011-11-07", "2011-11-16", "2012-01-02", "2012-01-21", "2012-01-29", "2012-02-02", "2012-02-04", "2012-02-11", "2012-02-20", "2012-02-25", "2012-02-29", "2012-03-03", "2012-03-07", "2012-03-14", "2012-03-17", "2012-03-24", "2012-03-31", "2012-04-01", "2012-04-08", "2012-04-12", "2012-04-14", "2012-04-21", "2012-04-22", "2012-04-25", "2012-04-29")
weight <- c(210.2, 206.8, 206.8, 205.6, 205.6, 204.4, 204.4, 202.6, 202.6, 202.6, 201, 201, 199.6, 199.6, 198.4, 198.2, 200.6, 200.8, 199.2, 199.2, 198.2, 196.4, 195.2, 194.4, 193.2, 191.6, 191.6, 191.6, 189.8, 188.4, 186.4, 186.4, 186.4, 185, 185, 185, 185, 183.8, 182.8)
xyplot(weight~as.Date(dts), main = "Weight vs Dates")
lines(lowess(weight), col=2)
lines(lowess(weight, f=.2), col=3)

I guess I should probably go watch sports now or something.

Mike :)

No comments: