Using R to help simulate the NHL Draft Lottery

Upon discussing the NHL game results file, I mentioned to a few of you that I have used R to generate an NHL draft lottery simulator. It’s quite simple, although you do have to install the XML package, which allows us to use R to ‘scrape’ websites. We use this functionality in order to create the lottery simulator dynamically, depending on the previous evening’s (afternoon’s) game results.

Here’s the code: (remember to un-comment the install.packages(“XML”) command the first time you run the simulator). Copy and paste this code into your R console, or save it as an R script file and run it as source.

# R code to simulate the NHL Draft Lottery
# The current draft order of teams obviously changes on a
# game-to-game basis. We have to create a vector of teams in order
# from 31st to 17th place that can be updated on a game-by-game
# (or dynamic) basis.

# To do this, we can use R's ability to interrogate, scrape,
# and parse web pages.

#install.packages("XML") # NOTE: Uncomment and install this
#                                package before running this
#                                script the first time.

require(XML) # We need this for parsing of the html code

url <- ("http://nhllotterysimulator.com/") #retrieve the web page we are using as the data source
doc <- htmlParse(url) #parse the page to extract info we'll need.

# From investigation of the web page's source code, we see that the
# team names can be found in the element [td class="text-left"]
# and the odds of each team winning the lottery are in the
# element [td class="text-right"]. Without this
# information, we wouldn't know where to tell R to find the elements
# of data that we'd like to extract from the web page.
# Now we can use xml to extract the data values we need.

result.teams <- unlist(xpathApply(doc, "//td[contains(@class,'text-left')]",xmlValue)) #unlist used to create vector
result.odds <- unlist(xpathApply(doc, "//td[contains(@class,'text-right')]",xmlValue))

# The teams elements are returned as strings (character), which is
# appropriate. Also only non-playoff teams are included, which makes
# it easier for us. The odds elements are returned as strings as
# well (and percentages), which is problematic.
# First, we have 31 elements (the values of 16 of which--the playoff
# teams --are returned as missing). We only want 15 (the non-playoff
# teams).
# Second, in these remaining # 15 elements we have to remove the
# "%" character from each.
# Third, we have to convert the character format to numeric.
# The code below does the clean-up. 

result.odds <- result.odds[1:15]
result.odds <- as.numeric(gsub("%"," ",result.odds)) #remove % symbol
teamodds.df <- data.frame("teams"=result.teams[1:15],"odds"=result.odds, stringsAsFactors=FALSE) #Create data frame for easier display 

# Let's print a nice table of the teams, with up-to-date
# corresponding odds. 

print(teamodds.df) # odds are out of 100 

#Now, let's finally 'run' the lottery, and print the winner's name.

cat("The winner of the 2018 NHL Draft Lottery is the:", sample(teamodds.df$team,1,prob=teamodds.df$odds),sep="") 

 

Advertisements

Polity IV Democracy Scores, Participation, and the Suffragettes

We noted today in lecture that Polity IV gives countries like the United States very high scores on the ”democraticness” variable, even during periods when a majority of the adult population–African-Americans, and women–were legally not allowed to vote. While Switzerland (1971) was the last European democracy to grant universal suffrage for women, Portugal was the last European country to do so (1976)–Portugal was run by a military dictatorship during in the early years of the 1970s.

In this era of social media abuse and bullying, it’s interesting to learn about some of the abuse hurled at the Suffragettes:

dvwcclyxcaewjpd

The Economist Intelligence Unit’s Global “Livability” Survey Omits Cost-of-Living

Before we can say anything definitive about the concepts and ideas that we’re studying, it is imperative that we have some understanding about whether the data that we observe and collect are actually “tapping into” the concept of interest.

For example, if my desire were to collect data that are meant to represent how democratic a country is, it would probably not be beneficial to that enterprise to collect measures of annual rainfall. [Though, in some predominantly agricultural countries, that might be an instrument for economic growth.] Presumably, I would want to collect data like whether elections were regularly held, free, and fair, whether the judiciary was independent of elected leaders, etc. That seems quite obvious to most.

The Economist’s Intelligence Unit puts out an annual  “Global Livability Report” , which claims to comparatively assess “livability” in about 140 cities worldwide. The EIU uses many different indicators (across five broad categories) to arrive at a single index value that allegedly reflects the level of livability of each city in the survey.  Have a look at the indicators below. Do you notice that the cost-of-living is not include? Why might that be?

livability_top_bottom_teneconomist_livability_1economist_livability_2

 

You may not be registered to vote even though you think you are.

I read a somewhat troubling story this morning about Canadian citizens who have previously not only been registered to vote, but who have voted, and are no longer registered with Elections Canada. Here is an excerpt:

Delaney Ryan is a 23-year-old anthropology student at Simon Fraser University. Unlike many of her contemporaries, she voted in both the last provincial and federal elections. She meets all the criteria for voter registration, having her driver’s licence and maintaining the same address since voting in those elections.

She can’t understand why, then, this time, she wasn’t registered on the voters list.

“I had heard rumours about people not being registered, and a friend’s Facebook page had postings on it of other people finding out they were suddenly no longer registered. A lot of these people seemed to be in the same demographic as me. So I went online (to the Elections Canada website) and checked, and I wasn’t on it, either.

“So I had to reapply for registration.”

So, go to the Elections Canada website and verify that you are registered to vote this October 19.

Political Psychology–How the gut, head, and heart decide our political views

David Moscrop of Maclean’s magazine, has been writing a series of articles on the psychology of politics. Why do we have the political opinions that we do? The answer, as you now know, is a combination of environmental, demographic, and personal characteristics. More and more, political scientists and psychologists have been researching the importance of psychology as a factor that influences political beliefs and behaviours.

moscrop-graphic

At the beginning of his article, Moscrop reveals the inspiration behind the series:

But the truth is that your gut is as much a source of your political decisions as your rational brain, and much of the time your gut—emotions, feelings, intuition—does its work outside of your awareness.

Yep. The faces of the candidates, the pitch of their voices, their gender and ethnicity and height; whether or not you believe in God, whether or not you’re hungry, whether you’re a lawyer or dock worker or school teacher; the effects of political advertising, the effects of issue framing or priming, the effects of your peer group; your partisanship, your family, your fears.

Here we see an image taken from the article, that demonstrates the various parts of our bodies that are involved in the making of political decisions.

Political Ideology and Political Parties

The Conservative Party of Canada website in the weeks leading up to the 2015 federal election.

The Conservative Party of Canada website in the weeks leading up to the 2015 federal election.

Today, we continue to look at Canada’s glorious Prime Minister, Stephen Harper. Besides being the current PM of Canada, Stephen Harper is also an honourable MP (member of parliament–we Canadians are forever grateful to the glorious denizens of Calgary Southwest*) and the leader of the federal Conservative Party.

On the short quiz that I gave on Tuesday, you were asked to write three words or phrases that are usually associated with conservatism, as a political ideology. Did any of you use the word “protect?”

Below you’ll find some screenshots of the scrolling images on the home page of the Conservative Party website. You’ll notice that the political campaign operatives have decided to portray PM Harper is our daddy, whose job it is to protect us in a dangerous world. Protect our jobs (presumably from being snatched away by foreigners); protect our children; protecting our economy, and protecting us from terrorists and others who would do us harm. Will it work?

“Fear makes man unwise in the three great departments of human conduct: his dealings with nature, his dealings with other men, and his dealings with himself. Until you have admitted your own fears to yourself, and have guarded yourself by a difficult effort of will against their myth-making power, you cannot hope to think truly about many matters of great importance … .”

—Bertrand Russell (“Outline of Intellectual Rubbish” in Unpopular Essays 1950)

*Calgary Southwest is no longer a federal constituency. It has been dissolved into Calgary Heritage and Calgary Midnapore.

Should Canada have Mandatory Voting?

When political scientists engage in studies of political phenomena there are many approaches that they may take. One oft-used approach is the so-called most similar systems design. This approach tries to “control for concomitant variation.” What does that mean? In social research it is difficult to clear cause-and-effect relationships because phenomena are complex and multi-faceted. Thus, if we wanted to determine why, for example, Canada is a relatively enduring and stable democracy, and Azerbaijan is not, one potential reason could be the relatively different histories of the two countries–Canada is a former British colony, while Azerbaijan was a former republic in the Soviet Union (which was a communist state).

Figure_3_3

Could this be the reason? Possibly. But, there are so many other differences between Canada and Azerbaijan that could also be the cause of the divergent outcomes regarding present political regime. Which one of these myriad differences, then, is the true cause of the difference between Canada and Azerbaijan regarding the level of democracy in each? (Indeed, the answer may not be mono-causal, but more complex and multi-causal.)

This is why many comparativists use the most similar systems design. By selecting units (countries) that are as similar as possible, they can control for many other potential causes for the alleged divergence in outcomes across the political phenomenon of interest.

So, let’s look at Canada and Australia–two countries that are quite similar in many respects: former British colonies, large land masses with relatively small populations, multi-cultural, constitutional monarchies, parliamentary democracies, economies reliant on natural resources, neither of which has won FIFA’s World Cup (men or women), etc. The two countries, differ, however, in levels of voting participation. Whereas barely 60% of eligible Canadians vote in federal elections, the corresponding figure for Australia is well over 90%. Do Aussies simply value political participation more than Canadians? Hardly! Australia has a mandatory voting law, which penalizes (monetarily) those who do not vote.

Should Canada enact a mandatory voting law? What do you think? Is it anti-democratic to force citizens to participate in the democratic process?