The effect of the number of samples in different cells on the results of ANOVA I have written a 3-way ANOVA in C++. I have 3 factors, lets say A, B and C and my aim is to check the strength of all possible interactions and main effects. The result of my code is the same as in MATLAB when I use type-I sum of squares.  
But when I change the data so that the number of replicates/samples is high in some cells and low in others (unbalanced design), I don't get the same results as in MATLAB. (To be precise, only SSt, SSe and SSa are the same as in MATLAB).
My question is, is it possible that since I have a large difference in the number of replicates, I should use type-III sum of squares? Or is there a special way that Matlab treats the data in such cases so its results differ from mine?
 A: I don't have Matlab but from what I've read in the on-line help for N-way analysis of variance it's not clear to me whether Matlab would automatically adapt the type (1--3) depending on your design. My best guess is that yes you got different results because the tests were not designed in the same way.
Generally, with an imbalanced design it is recommended to use Type III sum of squares (SS), where each term is tested after all other (the difference with Type II sum of squares is only apparent when an interaction term is present), while with an incomplete design it might be interesting to compare Type III and Type IV SS. Note that the use of type III vs. Type II in the case of unbalanced data is subject to discussion in the literature.
(The following is based on a French tutorial that I cannot found anymore on the original website. Here is a personal copy, and here is another paper that discussed the different ways to compute SS in factorial ANOVAs: Which Sums of Squares Are Best In Unbalanced Analysis of Variance?)
The difference between Type I/II and Type III (also called Yates's weighted squares of means) lies in the model that serves as a reference model when computing SS, and whether factors are treated in the order they enter the model or not. Let's say we have two factors, A and B, and their interaction A*B, and a model like y ~ A + B + A:B (Wilkinson's notation). 
With Type I SS, we first compute SS associated to A, then B, and finally A*B. Those SS are computed as the difference in residual SS (RSS) between the largest model omitting the term of interest and the smallest one including it.
For Type II and III, SS are computed in a sequantial manner, starting with those associated to A*B, then B, and finally A. For A*B, it is simply the difference between the RSS in the full model and the RSS in the model without interaction. The SS associated to B is computed as the difference between RSS for a model where B is omitted and a model where B is included (reference model); with Type III SS, the reference model is the full model (A+B+A*B), whereas for Type I and II SS, it is the additive model (A+B). This explains why Type II and III will be identical when no interaction is present in the full model. However, to obtain the first SS, we need to use dummy variables to code the levels of the factor, or more precisely difference between those dummy-coded levels (which also means that the reference level considered for a given factor matters; e.g., SAS consider the last level, whereas R consider the first one, in a lexicographic order). To compute SS for the A term, we follow the same idea: we consider the difference between the RSS for the model A+B+A*B and that for the reduced model B+A*B (A omitted), in case of Type III SS; with Type II SS, we consider A+B vs. B.
Note that in a complete balanced design, all SS will be equal. Moreover, with Type I SS, the sum of all SS will equal that of the full model, whatever the order of the terms in the model is. (This is not true for Type II and Type III SS.)
A detailed and concrete overview of the different methods is available in one of Howell's handout: Computing Type I, Type II, and Type III Sums of Squares directly using the general linear model. That might help you check your code. You can also use R with the car package, by John Fox who dicussed the use of incremental sum of squares in his textbook, Applied Regression Analysis, Linear Models, and Related Methods (Sage Publications, 1997, § 8.2.4--8.2.6). An example of use can be found on Daniel Wollschläger website.
Finally, the following paper offers a good discussion on the use of Type III SS (§ 5.1):

Venables, W.N. (2000). Exegeses on
  Linear Models. Paper presented to
  the S-PLUS User’s Conference
  Washington, DC, 8-9th October, 1998.

(See also this R-help thread, references therein, and the following post Anova – Type I/II/III SS explained.)
