Contextual analysis is used to examine models of group, hard, and soft selection and the evolution of altruism. We extend the methodology for measuring phenotypic selection to multiple levels in structured populations by analyzing selection acting on a trait at the individual level and its mean at the group level. With contextual analysis, we partition phenotypic selection into group and individual components using partial regressions. These analyses identify the level(s) at which selection is acting and distinguish indirect from direct selection acting at other levels. Contextual analysis of group selection in the absence of individual selection indicates that indirect selection is acting on individuals. Under soft selection, though all groups have the same relative fitness, contextual analysis detects equal and opposite levels of group and individual selection resulting from frequency-dependent selection acting within groups. Under hard selection, groups vary in relative fitness, but there is no group selection. Instead, indirect selection acts on the group mean phenotype. Thus, contextual analysis reveals that group, kin, frequency-dependent, and soft selection are related phenomena. Finally, we rederive Hamilton's rule for the evolution of altruism and determine when group selection is expected to be more powerful than individual selection.