# Variance in a T-test using means/medians

I have turning data for animals in different conditions with comparisons of before and after these conditions, we'll say condA condB.

Each animal has 3 repeats on both conditions, where before a treatment is compared to after a treatment (of the same condition). So condA before is compared to condA after, etc.

Is the best approach to find the mean/median (depending on normality of data) of each of the repeats and then performing a paired T-test/Mann-Whitney (depending on the normality of the means) to compare the means of each condition?

Without the variance of the original data, is the variance eliminated? Or is the variance between the 6 means/medians (3 before, 3 after) enough?

Is it also correct to use a median when the data are not parametric, mean when data are parametric?

Thank you

Edit for formatting:

Thank you for this. So would this be correct in lmer:

Response variables = turning rate under all conditions, gender, length(of animal)

Random affect = Animal ID

lmer(condABefore ~ gender + length + (1| animalID))

lmer(condAAfter ~ gender + length + (1| animalID))

lmer(condBBefore ~ gender + length + (1| animalID))

lmer(condBAfter ~ gender + length + (1| animalID))

• I think that the presence of animal multiple times in your dataset (three repeats per animal) induces a clear clustering to your data and you should consider a mixed-effect approach where animal ID is treated as random effect. – usεr11852 Dec 31 '19 at 13:25
• Is it not correct to just use a t test to compare the means of the individuals? – s33ds Jan 4 at 17:10
• Not it is not correct because animal ID induces a clustering (blocking) to the sample at hand as well as a few other factors. I try to expand on this below. Please let me know if you need further clarifications. (+1 fun question!) – usεr11852 Jan 5 at 0:28

It is incorrect to simply make a paired $$t$$-test because animal ID induces a clustering (blocking) to the measurements at hand. A random effects model for paired testing is potentially more natural in this scenario.

The immediate issues I can see are three:

1. Certain animals might have lower or higher average levels of turning under condA or condB or both. Allowing an animal-specific random intercept will help estimating more accurately the population-level estimates.
2. Repeating the experiment on particular animal might condition the animal to respond different. Using the repeat index as an explanatory variable within a mixed model would be more appropriate as it would allow to control for this scenario.
3. The mean response value in the two conditions analysed is potentially not normal. Mixed models can be directly extended distributions that are not normal (e.g. Poisson, or Gamma) or estimate quantiles (e.g. through linear quantile mixed models). (Side-note: $$t$$-test is generally fine for non-normal continuous data given they are not severely skewed.)

Notice that when we do a regression and look at the coefficients' significance we do a $$t$$-test too essentially. Therefore we do not lose the generality of a $$t$$-test. Ultimately through a $$t$$-test will be able to detect whether the average turning differs between condA and condB while the regression model will allows us to estimate how the turning rate varies by condition.

Assuming one uses R, the functions lmer (for linear mixed effects regression) and glmer (for generalised linear mixed effects regression) from the package lme4 are good places to start working with random effects models. There are some specialised packages on quantile mixed models (e.g. qrLMM and lqmm) if one wants to explore that route too.

EDIT: I think multiple lmer models are not required.

In particular something like: y ~ condX*timing + gender + length + (1|animalID) should serve as a reasonable base-line model. In this case y would be the turning property of interest, condX would take values A and B and timing would take values Before and Later. The rest of the variables would be as you defined them already.

Notice that in this case we will definecondX equals A, timing equals Before (and gender equals Female if we encode them alphabetically?) as the reference levels that will be captured in our intercept. In that way the coefficient associated with condXB will quantify the difference between condA and condB before any experiments are done and the coefficient associated with condXB:timingLater will quantify the difference between condA and condB at the later stage. Do notice that we will also have a coefficient timingLater that will encapsulate the overall changes observed in both condA and condB at the later stage. The changes in condA in later stages will simply equal that coefficient but the changes in condB in later stages will be equal to that coefficient plus the estimated coefficient condXB:timingLater.

• Thank you for this. So would this be correct in lmer: Response variables = turning rate under all conditions, gender, length(of animal) Random affect = Animal ID lmer(condABefore ~ gender + length + (1| animalID)) lmer(condAAfter ~ gender + length + (1| animalID)) lmer(condBBefore ~ gender + length + (1| animalID)) lmer(condBAfter ~ gender + length + (1| animalID)) I've added this to the main question above so that it's easier to read (won't let me insert newlines here). – s33ds Jan 5 at 13:35
• Cool, I am glad I could help. Thank you for this extra clarification, I extended my answer to give you some pointers about how to encode this using lmer. As mentioned, we want to focus on the coefficients being estimated. Their values and the associated standard errors is what we care for. Purely from an educational perspective I would urge you to try and built a small simulation of your experiment using dummy data. It will help a lot to get a feel about how informative your analysis can be. – usεr11852 Jan 5 at 14:37
• Thank you! You've helped a lot – s33ds Jan 5 at 16:09