I think the reason Federer lost a lot of matches to Nadal is psychological: he was scared to play someone of his level.
It's the same reason he also lost the arguably 5 closest Slam matches against Djokovic: UO10, UO11, WI14, UO15, WI19.
It seems like both Federer fans and detractors think his bad H-H record against Nadal and Djokovic are always because he is mentally weak or chokes under pressure. In fact, the most common reason cited for every loss sustained by every top player is usually ‘choking’, ‘being a mug’ and other mental issues. Most tennis matches are just won by the player who plays better on a particular day because they have more self-belief that they should win and usually because they have technique advantages on that surface and in their current form.
Federer would not have won 20 Slams and pretty much annihilated all other competition apart from Nadal and Djokovic for almost 17 years if he were mentally weak. He is supremely self-confident against most players to the point of being arrogant and not even trying to avoid their strengths - like the Delpo FH. Is it possible that Nadal is just better than Federer on slow surfaces and Djokovic is better than him on almost all surfaces except the fastest low-bouncing ones?
They both handle Federer’s strengths like his serve, his short-slice BHs, his inside-out FH etc. way better than any other player and have better movement than him. Federer‘s BH can and has been exploited by these two players from the baseline while they both don’t have any weaknesses that Federer can easily exploit. Federer used to beat them until they both developed the self-confidence that they were as good or better than him - it happened with Nadal early while it took Djokovic till late 2010 or 2011 to develop that confidence. Since then, Federer has never had the inner self-belief that he can consistently beat Djokovic and he didn‘t have it against Nadal till the AO2017 final where he played superbly and regained his mojo against Nadal. Nadal is not as quick anymore and Federer’s improved topspin BH shots and returns have nullified Nadal’s baseline advantage outside of clay. I’ve believed for a long time that Federer is unbelievably good, but was unfortunate enough to play in an era where he had two rivals that were better than him for most of their rivalry. Some would argue that it is because he is older, but I think that is a copout as Federer has no problem destroying every other rival who is more than a decade younger than him.
When a player has consistently lost close matches to another player for many years and he is out of ideas on what weakness he can exploit to change the trend, he will not suddenly be able to win close matches and it is common to ‘choke’ at crucial moments. This happens to all lower-seeded players against higher-seeded players in many matches where they cannot execute on break points, set points etc. when they are on the verge of an upset. Meanwhile, the higher seeded player expects to win and plays better under pressure. I think this is why Federer has lost many tough matches against Nadal and Djokovic because for a long time, he didn’t know what to do to beat them and his base game wasn’t good enough outside of fast courts. It is nice to see that he has turned the corner against Nadal and now, Nadal is the one without self-belief outside of clay. But, Djokovic has still proved to be a tough nut to crack on most surfaces.
If there is a psychological edge that Nadal and Djokovic had for many years, they earned it by being good enough to win many matches against Federer at his best in a way that other players could not. They had the game and so they gained the mental edge. That’s what happens when you play against a better player in rec tennis and that’s what happens on the pro tour - once a player gets the mental edge that he is on the right side of the winning trend, it’s tough to overcome.