Model selection by thresholding with applications to generalized linear models