4f occupancy and magnetism of rare-earth atoms adsorbed on metal substrates