What determines the frequency of attacks on settlements?
Typically the more resources and fewer defenses you have at a given settlement, the greater chances it has of being attacked. I will post a credible source if/when I find one, but it mostly seems to be claims from players posting on various forums.
However, I believe the game does mention early on that you should try to get your defense equal to (or greater than!) the sum of food + water. (Hopefully, I'm remembering that correctly). And that makes sense that would contribute to the likelihood of attack. I definitely have Tenpines Bluff attacked much more frequently than the others (i.e. Sanctuary) since I refuse to equip it with defenses. Raiders/supermutants seem much less interested in settlements surrounded by turrets/traps/etc.
Additional information from the Fallout wiki:
It is possible for some settlers to be a Synth which may cause an attack on the settlement from The Institute.
As @DCShannon mentions in their comment, difficult is also likely a factor. A larger proportion of players seem to have settlements get attacked on survival mode, even if defense > food + water.