How to predict deviation from ideal gas behaviour? Molar masses of H2, N2, CO2, and NH3 are 2g, 28g, 44g, and 17g and respectively.
The gases deviate from ideal behavior in the following order:
$$\require{mhchem} \ce{H2<N2<CO2<NH3}$$
According to my book, deviation increases with the increase of molar mass. However, according to molar mass, the order should've been the following:
$$\require{mhchem} \ce{H2<NH3<N2<CO2}$$
Why didn't the book's criteria work?
 A: This depends slightly on your definition of 'ideal' - I'm taking it to mean enthalpy is only a function of temperature, not pressure, and that the specific heat capacities are constant.
Deviation from ideal behaviour comes from two sources: intermolecular forces and internal energy states. Intermolecular forces mean that the enthalpy varies with how close the molecules are, and hence with pressure. Internal energy states become excited at different temperatures, so lead to changes in heat capacities as, when more internal energy states are excited, a lower proportion of the total heat energy is in translational kinetic energy which temperature is a measure of.
Intermolecular forces do generally get stronger with size, especially if you're mostly looking at hydrocarbons - long chains have big van der waal's forces. But they also get boosted by polar molecules, which is probably what you're seeing here - molecules made of different elements will tend to have a positive part and a negative part, which makes intermolecular forces stronger. Water is a small light molecule, but steam is very non-ideal because it's a very polar molecule and hydrogen bonds are strong.
The number of internal energy bonds also increases with molecular weight because more bonds and larger atoms means more internal energy modes that can become active at different changes and lead to variation in heat capacities. But in your examples, the lighter less-ideal molecules have fewer bonds overall so fewer degrees of freedom.
Within a chemical family (like hydrocarbons), the deviation increases with molar mass quite reliably. And within neutral diatomic gases you'd also expect deviation to get higher with molar mass. But comparing neutral diatomic gases with polar polyatomic gases, the rule gets broken - a polar polyatomic molecule made of lots of different light atoms will generally be less ideal than a pair of heavy atoms with a bond between them.
A: Theoretical Way
Deviation from the ideal gas law is defined as a compressibility factor
, $Z$ :
$$ Z = \frac {pV_m} {RT} \,,$$
For an ideal gas $Z=1$, for a real gases, however $Z \ne 1$, and is a function of gas molar volume $V_m$, intermolecular force potential $\varphi$ and temperature $T$ in such way :
$$ Z\,(V_m,\varphi,T)=1+2\pi {\frac {N_{\text{A}}}{V_{\text{m}}}}\int _{0}^{\infty }\left(1-\exp \left({\frac {\varphi }{kT}}\right)\right)r^{2}dr $$
Put your  known gas parameters, integrate, and here you go,- you have deviation from ideal gas law.
Empirical Way
If you know reduced pressure and reduced temperature of your gas, you can map that information into pre-compiled Nelson-Obert graph(s), like this one :

for finding your compressibility point. However for a polar gases, you may get a substantial error. Also these charts are compiled from pure gases data, for a gas mixture - you have to take some extra steps.
Van der Waals model
Real gas equation can be stated as per Van der Waals model :
$$ RT = (p+p_0)(V_m - V_0) $$
Where $p_0$ is pressure due to inter-molecular forces and $V_0$ is total volume of molecules in 1 mole of gas.
$p_0,~V_0$ can be estimated using critical pressure and critical temperature:
$$ p_0={\frac {27R^{2}T_{\text{c}}^{2}}{64p_{\text{c}}{V_m}^2}} $$
$$ V_0 ={\frac {RT_{\text{c}}}{8p_{\text{c}}}} $$
Now, for a deviation from an ideal gas model lets define :
$$ \Gamma = (p_0+1)(V_0+1) $$. For an ideal gas $\Gamma = 1$, and thus real gas law converges to a standard $RT = pV_m$. Now just sort your gases by a $\Gamma$ parameter in an ascending order and you'll have increasing deviation from a ideal gas law.
