Check the gap on a couple of spark plugs, also their tip condition.
Should be 0.054"
If larger, spark can be delayed(advanced), and you may want to change them to 0.049" so they spark sooner(retard spark)
2001-2005 4.0l SOHC uses SP498 (Platinum) (old part number is AGSF34FP) or SP412 (Nickel)
Pinging is, of course, the fuel/air mixture self-igniting just prior to the spark plug firing, the noise is the result of the two separate "explosion" wave fronts meeting in the cylinder.
Pinging causes extra heat in the cylinder(and can cause piston damage), so once a cylinder starts to ping it can be self sustaining because of the extra heat.
Self-ignition is all about heat, it can be actual hot spots in the cylinder, or from compression heat.
Octane is a heat rating, higher octane has a higher self-ignition temp, that's why pinging will often go away with higher octane gas.
4.0l has a 9.0:1 compression ratio, so can run 87 octane fuel without pre-ignition.
When you get up to 9.4:1 is when 87 octane can get pingy
If engine doesn't ping at all when engine is cold, then I would suspect some carbon build up in the cylinders creating higher compression and hot spots.
Engine makers run fuel/air mixture right at the edge of the "perfect" mixture, but slightly rich, this is to get the best MPG without melting the pistons, the "prefect" mix will melt pistons in a minute or two, lol.
This is why slight out of parameter conditions can result in pinging.
A lean mix has two things against it, it cools the cylinder less and is easier to self-ignite from hot spots.
If possible I would look at the real time OBD II data, the fuel trim data, to see if it is looking too lean.
Small air leak in PCV hose(common on Rangers) or power brake hose can lean out fuel air too much, I would expect Computer to fix that or turn on CEL but...........??
With engine warmed up and idling, unplug the IAC valve, idle should drop down to 500rpm or engine may stall, either is fine, if idle doesn't drop then you have an air leak.
The EGR system, if so equipped, is there to reduce NOx emissions, it does this by cooling the cylinders when they are under load and heating up.
As odd as it may sound the hot exhaust being mixed with the fuel/air and burned in a cylinder actually cools the cylinder more than running just the fuel/air mix.
If EGR valve or pipe starts getting carbon build up inside then exhaust gas flow is reduced, so cylinders heat up a little more than they should.