Barack Obama won 52.9 percent of the popular vote in 2008 and 365 electoral votes, 95 more than he needed. Many naturally concluded that prejudice was not a major factor against a black presidential candidate in modern America. My research, a comparison of Americans’ Google searches and their voting patterns, found otherwise. If my results are correct, racial animus cost Mr. Obama many more votes than we may have realized.

Doug Mills/The New York Times

Quantifying the effects of racial prejudice on voting is notoriously problematic. Few people admit bias in surveys. So I used a new tool, Google Insights, which tells researchers how often words are searched in different parts of the United States .

Can we really quantify racial prejudice in different parts of the country based solely on how often certain words are used on Google? Not perfectly, but remarkably well. Google, aggregating information from billions of searches, has an uncanny ability to reveal meaningful social patterns. “God” is Googled more often in the Bible Belt, “Lakers” in Los Angeles .

The conditions under which people use Google — online, most likely alone, not participating in an official survey — are ideal for capturing what they are really thinking and feeling. You may have typed things into Google that you would hesitate to admit in polite company. I certainly have. The majority of Americans have as well: we Google the word “porn” more often than the word “weather.”

And many Americans use Google to find racially charged material. I performed the somewhat unpleasant task of ranking states and media markets in the United States based on the proportion of their Google searches that included the word “nigger(s).” This word was included in roughly the same number of Google searches as terms like “Lakers,” “Daily Show,” “ migraine ” and “economist.”

A huge proportion of the searches I looked at were for jokes about African-Americans. (I did not include searches that included the word “nigga” because these searches were mostly for rap lyrics.) I used data from 2004 to 2007 because I wanted a measure not directly influenced by feelings toward Mr. Obama. From 2008 onward, “Obama” is a prevalent term in racially charged searches.

The state with the highest racially charged search rate in the country was West Virginia . Other areas with high percentages included western Pennsylvania , eastern Ohio , upstate New York and southern Mississippi .

Once I figured out which parts of the country had the highest racially charged search rates, I could test whether Mr. Obama underperformed in these areas. I predicted how many votes Mr. Obama should have received based on how many votes John Kerry received in 2004 plus the average gain achieved by other 2008 Democratic Congressional candidates. The results were striking: The higher the racially charged search rate in an area, the worse Mr. Obama did.

Consider two media markets, Denver and Wheeling (which is a market evenly split between Ohio and West Virginia). Mr. Kerry received roughly 50 percent of the votes in both markets. Based on the large gains for Democrats in 2008, Mr. Obama should have received about 57 percent of votes in both Denver and Wheeling. Denver and Wheeling, though, exhibit different racial attitudes. Denver had the fourth lowest racially charged search rate in the country. Mr. Obama won 57 percent of the vote there, just as predicted. Wheeling had the seventh highest racially charged search rate in the country. Mr. Obama won less than 48 percent of the Wheeling vote.

Add up the totals throughout the country, and racial animus cost Mr. Obama three to five percentage points of the popular vote. In other words, racial prejudice gave John McCain the equivalent of a home-state advantage nationally.

Yes, Mr. Obama also gained some votes because of his race. But in the general election this effect was comparatively minor. The vast majority of voters for whom Mr. Obama’s race was a positive were liberal, habitual voters who would have voted for any Democratic presidential candidate. Increased support and turnout from African-Americans added only about one percentage point to Mr. Obama’s totals.

If my findings are correct, race could very well prove decisive against Mr. Obama in 2012. Most modern presidential elections are close. Losing even two percentage points lowers the probability of a candidate’s winning the popular vote by a third. And prejudice could cost Mr. Obama crucial states like Ohio, Florida and even Pennsylvania.

There is the possibility, of course, that racial prejudice will play a smaller role in 2012 than it did in 2008, now that the country is familiar with a black president. Some recent events, though, suggest otherwise. I mentioned earlier that the rate of racially charged searches in West Virginia was No. 1 in the country and that the state showed a strong aversion to Mr. Obama in 2008. It recently held its Democratic presidential primary, in which Mr. Obama was challenged by a convicted felon. The felon, who is white, won 41 percent of the vote.

In 2008, Mr. Obama rode an unusually strong tail wind. The economy was collapsing. The Iraq war was unpopular. Republicans took most of the blame. He was able to overcome the major obstacle of continuing racial prejudice in the United States. In 2012, the tail wind is gone; the obstacle likely remains.

Seth Stephens-Davidowitz is a doctoral candidate in economics at Harvard. The most up-to-date version of the research paper on which this article draws is available here.