Loneliness increases a person’s risk for dementia by 40 percent, according to an analysis of 10 years of data on more than 12,000 people.