They control for that stuff in these studies. It is quite literally the physiological effects of loneliness shortening your lifespan.
Consider this: your conscious mind and subconscious mind are completely separate from one another. The only link they share is your body. Think about anxiety and panic attacks. People who suffer from them cannot control them with conscious thought. It is the subconscious mind affecting the body. The only thing you can do to mitigate the symptoms is thourg conscious deep breathing to force the body to relax and as the body relaxes, the subconscious relaxes in response. This is the true "ghost" inside of us, this second, distinct, invisible mind that has access to our body and senses.
This is the mind that perceives our place within social dominance hierarchies. There is ample evidence showing that people high in status produce more serotonin (feel happier, confident, assertive, etc). Really think about that for a second - their bodies are physically reacting to something as abstract as social rank. This obviously isn't controlled by conscious thought either, as we cannot just "think" ourselves into triggering these physiological changes. That means it is the subconscious mind is able to perceive status in some way and trigger the body to respond accordingly.
For me, this raises a lot of interesting questions about how my subconscious mind works and it's relationship to my life experiences. Every time an attractive woman refuses to make eye contract, speaks in dismissive tone, makes a disgusted facial expression, all are being observed by my subconscious. Then what about the lack of positive stimulus? How different does the subconscious respond to the guy who gets smiled at by strangers everyday different from the guy who everyone tries to avoid even looking at?