\documentclass{beamer} \usepackage{graphicx} \usepackage{Sweave} \usepackage{hyperref} \mode { \usetheme{CambridgeUS} } \usepackage[english]{babel} \usepackage[latin1]{inputenc} \usepackage{times} \usepackage[T1]{fontenc} \title{Analysis of your weight data} \author{\href{http://www.mayin.org/ajayshah}{Ajay Shah}} \begin{document} \SweaveOpts{engine=R,eps=FALSE,keep.source=TRUE,results=hide,echo=FALSE,prefix.string=pix/pic} \setkeys{Gin}{width=\textwidth} <>= library(ggplot2) library(xtable) system("mkdir pix") calories.per.g <- 2.2*3.5 # TODO: Put in an exact number d <- read.table("ans_data.text", col.names=c("date","w")) d$w <- 1000*d$w d$date <- as.Date(as.character(d$date)) d$time <- as.numeric(d$date-d$date[1]) @ \begin{frame} \titlepage \end{frame} \begin{frame} \frametitle{Scatterplot} \begin{center} <>= print(ggplot(d, aes(date,w/1000)) + geom_point() + labs(x=NULL, y="Weight (kilos)") + scale_x_date(major="1 months", minor="7 days", format="%b %y")) @ \end{center} \end{frame} \begin{frame} \frametitle{Simple OLS regression of weight (in grams) on time (in days)} \begin{center} <>= m <- lm(w ~ time, data=d) imbalance <- (coef(m)[2] + c(-1.96,1.96)*summary(m)$coefficients[2,2]) * calories.per.g imbalance.string <- sprintf("Imbalance = %.2f to %.2f kCal/day", imbalance[1], imbalance[2]) print(xtable(m),floating=FALSE) @ \end{center} \end{frame} \begin{frame} \frametitle{Scatterplot + overall OLS time trend} \begin{center} <>= print(ggplot(d, aes(date,w/1000)) + geom_point() + labs(x=NULL, y="Weight (kilos)") + geom_smooth(method="lm") + opts(title=imbalance.string) + scale_x_date(major="1 months", minor="7 days", format="%b %y")) @ \end{center} \end{frame} \begin{frame} \frametitle{OLS spline estimation} \begin{center} <>= library(splines) print(ggplot(d, aes(date,w/1000)) + geom_point() + labs(x=NULL, y="Weight (kilos)") + stat_smooth(method="lm", formula = y ~ ns(x,4)) + opts(title = "Spline smoothing") + scale_x_date(major="1 months", minor="7 days", format="%b %y")) @ \end{center} \end{frame} <>= recentwidth <- 42 # In the future, do this nicely @ \begin{frame} \frametitle{Latest \Sexpr{recentwidth} days} \begin{center} <>= small <- subset(d, d$date > (tail(d$date,1) - recentwidth)) m <- lm(w ~ time, data=small) imbalance <- (coef(m)[2] + c(-1.96,1.96)*summary(m)$coefficients[2,2]) * calories.per.g imbalance.string <- sprintf("Imbalance = %.2f to %.2f kCal/day", imbalance[1], imbalance[2]) print(xtable(m),floating=FALSE) @ \end{center} \end{frame} \begin{frame} \frametitle{Scatterplot + OLS time trend: Recent \Sexpr{recentwidth} days} \begin{center} <>= print(ggplot(small, aes(date,w/1000)) + geom_point() + labs(x=NULL, y="Weight (kilos)") + geom_smooth(method="lm") + opts(title=imbalance.string) + scale_x_date(major="1 week", format="%d %b")) @ \end{center} \end{frame} \begin{frame} \frametitle{Density of imbalance per day (latest \Sexpr{recentwidth} days)} \begin{center} <>= library(boot) sampleols <- function(dataset, indexes) { return(lm(w ~ time, data=dataset[indexes,])$coefficients[2]) } b <- boot(d, sampleols, R=2000) k <- density(b$t) tmp <- data.frame(x=k$x*calories.per.g, y=k$y) ci <- calories.per.g*boot.ci(b, type="basic")$basic[1,c(4,5)] print(ggplot(tmp, aes(x, y)) + geom_line() + labs(x="Imbalance (kcal/day)", y="Density (through bootstrap)") + geom_vline(xintercept=ci[1]) + geom_vline(xintercept=ci[2]) ) @ \end{center} \end{frame} \begin{frame} \frametitle{Finding out more} See \href{http://www.mayin.org/ajayshah/MISC/weightloss.html}{http://www.mayin.org/ajayshah/MISC/weightloss.html} \end{frame} \begin{frame} \hfill\mbox{Thank you.} \end{frame} \end{document}