General R code to analyze tub data