  The stochastic block model is a popular tool for studying community
structures in network data. We develop a goodness-of-fit test for the
stochastic block model. The test statistic is based on the largest singular
value of a residual matrix obtained by subtracting the estimated block mean
effect from the adjacency matrix. Asymptotic null distribution is obtained
using recent advances in random matrix theory. The test is proved to have full
power against alternative models with finer structures. These results naturally
lead to a consistent sequential testing estimate of the number of communities.
