Selecting the dimension of a subspace in principal component analysis and canonical correlation analysis