# What to do with ANOVA unequal balance in r

I have a dataset with several continuous variables on which I would like to know the effect of 2 other factorial variables (and their interaction if present). The big problems are that the design is unbalanced, sample size is small and not all combinations are even present.

Factors: x1= 1 to 6, and x2= 1 to 4. x1 and x2 are independent variables.

The overall sample size = 31, the size per group (combination of x1 and x2) varies between 0 and 3.

How can I correctly analyse the effect of the factors x1 and x2 on each of the other variables?

Can I use ANOVA?

I am using R and was trying the following codes, but I am wondering which approach is the most correct:

aov(a~x1*x2, data=mydata)
Anova(lm(a~x1*x2, data=mydata),type=c("III"))


but when I do the latter, I get:

Error in Anova.III.lm(mod, error, singular.ok = singular.ok, ...) :
there are aliased coefficients in the model


This is probably caused by the fact that not all the interacting combinations are present. Seeing that from the first model the interaction does not seem to be significant, I then dropped it, and used:

Anova(lm(a~x1+x2, data=mydata),type=c("III"))


My dataset (here I only included one variable to model, there are actually 10):

║ X1 ║ X2 ║      a      ║
╠════╬════╬═════════════╣
║  1 ║  0 ║ 6.110615522 ║
║  1 ║  0 ║ 8.683245006 ║
║  1 ║  1 ║ 5.597826087 ║
║  1 ║  2 ║ 6.068779501 ║
║  1 ║  3 ║ 6.099436187 ║
║  2 ║  0 ║ 12.28545619 ║
║  2 ║  0 ║ 11.42178363 ║
║  2 ║  2 ║ 12.70053476 ║
║  3 ║  0 ║ 6.33733517  ║
║  3 ║  1 ║ 5.988267883 ║
║  3 ║  1 ║ 9.542958023 ║
║  3 ║  1 ║ 4.218181818 ║
║  3 ║  2 ║ 6.310226919 ║
║  3 ║  3 ║ 6.031021898 ║
║  4 ║  0 ║ 7.668276058 ║
║  4 ║  0 ║ 10.61430277 ║
║  4 ║  0 ║ 10.62778052 ║
║  4 ║  1 ║ 6.703470032 ║
║  4 ║  2 ║ 7.662107396 ║
║  4 ║  2 ║ 10.28938907 ║
║  4 ║  3 ║ 8.391157011 ║
║  5 ║  0 ║ 5.037664783 ║
║  5 ║  0 ║ 7.875457875 ║
║  5 ║  2 ║ 6.092588151 ║
║  5 ║  3 ║ 0.975952032 ║
║  6 ║  0 ║ 5.377966799 ║
║  6 ║  0 ║ 5.333951763 ║
║  6 ║  0 ║ 7.720119098 ║
║  6 ║  1 ║ 4.530504851 ║
║  6 ║  2 ║ 5.492851768 ║
║  6 ║  3 ║ 4.605137964 ║


The QQ plot of the residuals of the model with the Tukey's test of additivity for this variable:

After Box-Cox transformation, the QQ plot of residuals seems similar:

You could see if there is an interaction effect without actually doing x1*x2 by using Tukey's test of additivity. Do a quick Google search for that and you can find some info. This is typically used when there aren't enough df to include the interaction but should work here as well.

Edit: Try something like this

options(contrasts =c("contr.sum","contr.poly"))
m1 <- aov(a ~ x1 + x2)
ypred = m1$fitted.values mu <- model.tables(m5,type="mean")$tables\$'Grand mean'
onedf <- ypred*ypred/(2*mu)
m1.onedf <- aov(y~x1 + x2+onedf)
anova(m1.onedf)


Then, if onedf is significant in the anova table then there is an interaction. The option is there to deal with unbalanced data. Also, don't forget to check the residuals on the anovas to see if any transformations need to be done on the response variable.

• Thank you! Actually my question had two sides: one, is it legitimate to use some form of ANOVA, and two, how should I handle this in R? With regards to your reply, I don't know if you could briefly explain what you did there and how it relates to the different statistical approaches handling with this unbalanced ANOVA design (just to get a better on the rationale behind and to describe it)? Finally, I am not really sure what you mean with "the option is there to deal with unbalanced data". – veronique Jan 4 '16 at 11:23
• I did a qqnorm on the residuals, but I am doubting on the normality. Can I use a shapiro wilk on the residuals? – veronique Jan 4 '16 at 11:31
• @veronique try a boxcox to see if a transformation on the response will fix it. Also, you just can type plot(model name) for all residual plots to show automatically. myowelt.blogspot.com/2008/05/… this site has an explanation of the contrasts. – Kristofersen Jan 4 '16 at 11:43
• @veronique I'm not sure how good of an explanation I can give on tukeys 1df, but I know that will work for testing interactions. I pulled it from an old hw assignment I did for a design of experiments class. I'm not at a computer but I can try to do a little reading and post more on it later. – Kristofersen Jan 4 '16 at 11:48
• @veronique it doesn't look like the transform did anything and looks fine. With small sample size like this it won't look perfect. The 1df term just says if interactions are significant so if term isn't significant don't need to include the 1df or the interaction in your model. – Kristofersen Jan 4 '16 at 15:33