Want to live longer? Consider joining a church.

People who are religious live an average of four years longer than those who have no ties to religion, a study published Wednesday in the journal Social Psychological and Personality Science. The researchers analyzed the obituaries of more than 1,000 people around the country, and adjusted for other factors that can affect lifespan, including the gender and marital status of the deceased.

“Religious affiliation had nearly as strong an effect on longevity as gender does, which is a matter of years of life,” said Laura Wallace, lead author of the study and a doctoral student in psychology at The Ohio State University.

This isn’t the first study that has tied religious belief to living longer. People who go to church at least once a week are at a 33% lower risk for death, a 2016 study of more than 75,000 people found.

Researchers believe the health benefits of religion have to do with its ties to volunteering and being part of a community. Strong social ties can boost survival rates by 50%, a survey of more than 100 years of research released in 2010 found.

Don’t miss: Harvard scientists say these 5 things can prolong your life by a decade

But that isn’t the only reason for the boost in longevity, according to Wallace. “We found that volunteerism and involvement in social organizations only accounted for a little less than one year of the longevity boost that religious affiliation provided,” she said. “There’s still a lot of the benefit of religious affiliation that this can’t explain.”

Meet the tech founders building the anti-smartphone

Those benefits could come from meditation and other stress-reducing practices, including prayer, the researchers said. Some preliminary studies have shown that meditation leads to fewer deaths from heart disease and cancer, though their sample sizes were relatively small.

People who abandon their religious practice put themselves at risk for an earlier death, said Howard Friedman, a health psychologist and professor at the University of California, Riverside. He conducted similar research, reported in his book on “The Longevity Project,” including a study of 1,528 men and women followed from their childhood until their deaths.

”It is partly the good health habits often fostered by religious practice, but especially the social engagement that is so much a part of religious community, that are the likely explanations for the health of many religious folks,” he said.

Also see:5 ways lack of sleep could alter the course of your life

Peer pressure to live a healthier life may also help. Other factors related to religion that could boost longevity include abstaining from unhealthy habits like drinking and drugs, according to the researchers. Not drinking or smoking are two of five healthy habits pinpointed by a study released in June that can make people live 10 years longer.

The other three healthy habits are eating a healthy diet, exercising 30 minutes a day, and maintaining a body mass index between 18.5 and 24.9. Men and women who make these lifestyle changes are 82% less likely to die from cardiovascular disease and 65% less likely to die from cancer, the study found.

Taking a daily constitutional also helps. Walking at an average pace was linked to a 20% reduction in the risk of mortality compared with walking at a slow pace, while walking at a brisk or fast pace was associated with a risk reduction of 24%, according to a recent study by scientists at five universities.