# Determining a variable's contribution to the variation in another

I have data that look like this, with a tonnage measure and density measure for each carriage in a bunch of trains.

Train Carriage_No Tonnes  Density
A     1           105.5   2.12
A     2           104.9   2.28
A     3           101.2   2.30
A     4           108.7   2.41
B     1           112.3   2.51
B     2           109.7   2.34


etc..

How do I determine density's contribution to the variation in tonnes? What is doing this called? Some R code would be most helpful!

Our goal is to fit more tonnes into each carriage, but the variation around the mean is such that we're overloading an unacceptable percentage of carriages. Therefore we need to tighten the variation to allow us to increase the mean (i.e. more total tonnes) without overloading.

There are a few things we can do to improve tonnage variability, but controlling density isn't one of them (reacting to changes in density may be possible). So I'd like to understand how much the density accounts for the variation in tonnes.

-

You should probably start by getting a general idea about the relationship between Tonnes and Density:

plot(Tonnes~Density)
lines(ksmooth(Density,Tonnes,bandwidth=0.5))


and playing with the bandwidth parameter to figure out the form of the mean function of Tonnes conditional on Density. If it is anything like linear, you should be fine with regression/ANOVA techniques - e.g. R-squared given by the summary(lm(Tonnes~Density)) is precisely the portion of the variance in Tonnes due to the variance in Density. Look up the details here: http://cran.r-project.org/doc/contrib/Faraway-PRA.pdf
If it is nonlinear, some transformation of variables or nonlinear regression modelling might be in order and it is hard to specify a general approach here so maybe you should come back with a plot.

-
We don't yet have the density data, but I'm trying to justify the cost of acquiring it. I think I'll have to make up some values just to get a better understanding of your answers! PS: I've had quick look at that book and it seems very well written. I've been trying to find a clear explanation of how to measure regression to the mean and this is the first place that I've found it. (p.14-15) –  TMOD Jul 24 '11 at 5:04

Aside from the ksmooth approach you could do a simple linear regression:

fitted_model <- lm(Tonnes ~ Density)
summary(fitted_model)


In the summary you look for the "Estimate" that tells you how much increase you expect for each increase in Density.

You should also plot the model by simply:

plot(Tonnes, Density)
abline(fitted_model)


The plot tells you visually how well the line fits but it also hints if you need some value transformation. Log() is a good transformation that is frequently used, our sense of numbers is actually logarithmic and it's when we're about 3-4 years old that we unlearn that natural instinct of logarithmic counting. This is probably due to that many things appear logarithmic in nature, when a cell divides it divides into to creating an exponential increase in cells. You should suspect logarithmic transformations if your data is grouped at one end of the plot.

@rolando2: I heard about the logarithmic counting through listening to a lovely RadioLab episode about Numbers. They report about tribes in the Amazon that don't have our numbers and that they still as adults count in a logarithmic way.

-
With this approach, is there a danger that Tonnes appears to be related to Density only because Tonnes is actually related to Carriage_No (or Train) and Carriage_No (or Train) is correlated with Density? –  mark999 Jul 23 '11 at 10:50
@Max i'd love to see a reference for your point about the change at 3-4 years old. –  rolando2 Jul 23 '11 at 13:19
If you believe that you have confounding by carriage no. or train then you should build a more complex model. Confounding is usually the hardest part of research - it is always lurking in the background - I very rarely hear anyone complaining about the statistical methods used in a study while there usually are tons of opinions on possible confounding. I guess that you want to create a variable that counts the number of carriages per train and build something like: Tonnes ~ Density + number_of_carriages –  Max Gordon Jul 23 '11 at 16:46
I'm trying to digest the responses here, but I'll just quickly add that the number of carriages per train is fixed at 140. –  TMOD Jul 24 '11 at 4:51
In that case the first two columns in your data don't add any information... unless you suspect that the first carriage is heavier loaded than the last or vice versa. There might be a situation where the crew makes an effort in the first carriage and feel tired when they reach the last - this doesn't have to be a confounding but it might be if they choose the less density stuff for the last carriage. –  Max Gordon Jul 24 '11 at 6:32