You want to find a good balance between aggression (raiding, attacking, or just threatening an attack), defensiveness (having units or PD to defend your stuff), and greed (making t1 mexes or upgrading them to t2)
@Blodir uses the analogy of "rock-paper-scissors": if your opponent is aggressive, you want to defend (have enough units to intercept his units, win the fight, scoop the mass). if your opponent is defending, you want to eco. if your opponent is ecoing, you want to be aggressive so you can punish him with raids.
It’s best to scout your opponent so you know what they are doing so you can counter their strategy. Early in the match (like the first 2-3 minutes) you can’t do this, you have to play “blind,” meaning you have to choose how much aggression/defensiveness/greed you want without knowing anything about what your opponent is doing.
There's a thing called "defender's advantage," which basically means if he makes 10 tanks and sends them at you, you might have time to make 3 engineers before you start making your own tanks. By the time his 10 tanks arrive, you have 11 tanks to match him. So you get the benefit of 3 engineers doing engineer stuff and you can blunt his attack. If you beat his attack, you will be ahead because you have 3 engineers making mexes, and if you can scoop the mass from the battle, you will be really ahead.
You might feel like you're "losing" because you make tanks instead of engineers, so you can't build your mexes as fast as you'd like to build them. But you also have to consider whether your opponent is building his mexes. By making tanks he is also slowing himself down. You can always check the replay after to see whose strategy was more effective.
On ladder maps, it's generally better in the early game to have more units than your opponent, even if it means slightly less eco, as long as you are aggressive with your units. You should be able to do enough damage to your opponent to justify making the units. You're seeing that firsthand when you see how much damage your opponent does to you with early raiding.
The main thing about choosing to spend more on "unit spam" than your opponent is that you need to use the units, or you actually will fall behind. If you invest in 3 extra factories and 30 extra tanks while your opponent makes 2 T2 mexes or makes air factory + transport to drop an island for 4 extra mexes, you need to use those tanks, or you will fall behind and your opponent can afford to out-produce you.
If you feel like you're losing because your opponent is being more aggressive than you, you should practice being more aggressive yourself. Avoid making t2 mexes when you can make extra land factories to churn out extra units. Don't make 100% engineers out of your early factory, make more like 50% engies 50% tanks, and see how it goes. Make sure you don't carry around a full mass bar. Spend your mass (and make enough pgens so you can afford to spend your mass). If you feel like you have too many tanks and not enough engineers, you can always make further adjustments. Being aggressive isn't the only way to play, but you should definitely at least experiment with it so you are more flexible in the future about what strategies you want to use. Try going for extreme t1 spam, meaning that apart from making pgens and an air factory, you put ALL of your economy into making enough T1 factories to spend all the mass you get from mexes and reclaim. If you can get way more units than your opponent you can try to overwhelm them while they're working on getting t2 mexes or a t2 hq. Then when you think “it’s finally time to make a t2 mex,” instead make an air fac, use it to make a transport to drop arty next to his t2 mexes. You want to see how far you can push the aggression before you start losing games because you were TOO aggressive. Then you can try to find balance between aggression and defense.
You should also learn how to adjust your strategy during the match. For example if you have a lot more tanks than your opponent, but you don’t want to risk everything by sending them all into your opponent’s base, you can pause all of your land factories and upgrade 6 mexes to t2 with lots of engineer assistance and then make a t2 hq. When you have more units than your opponent, you have the freedom to adjust how you spend your mass. If you desperately need to make more tanks to avoid being overrun, you don’t have that freedom. Making a big amount of factories doesn’t actually “lock” you in to a t1 spam strategy as long as you are willing to pause them or even recycle some of them.
One thing low-rated players usually skip doing is making early air. Air is good for defense. Your air scout can show where the enemy raids are so you can stop them, and you can use bombers or jesters to clean up raids. Having an early air factory (like the 2nd factory or 3rd factory you make) helps to put your opponent on the defensive because you can make bombers to pick off their engineers or to bomb clumps of units. And you get intel from the scout planes. If you have 3 mech marines on his side of the map, an air scout can show you where to send them to find engineers and avoid tanks.