Category: Non-linear models

The 2016 Rio Olympic games being officially over, we can obsess as much as we like with the final medal table, without the distraction of having to actually watch any sports. One of the basic questions to ponder about the medal table is to what extent Olympic glory is determined by the wealth, economic power and population size of the countries.

Many news outlets quickly calculated the ratios of the 2016 medal count with economic power and population size per country and presented the rankings of ‘medals won per billion of GDP’ and ‘medals won per million of population’ (for example here and here). But while these rankings are fun, they give us little idea about the relationships between economic power and population size, on the one hand, and Olympic success, on the other. Obviously, there are no deterministic links, but there could still be systematic relationships. So let’s see.

Data

I pulled from the Internet the total number of medals won at the 2016 Olympic games and assigned each country a score in the following way: each country got 5 points for a gold medal, 3 points for silver, and 1 point for bronze. (Different transformations of medals into points are of course possible.) To measure wealth and economic power, I got the GDP (at purchasing power parity) estimates for 2015 provided by the International Monetary Fund, complemented by data from the CIA Factbook (both sets of numbers available here). For population size, I used the Wikipedia list available here.

Olympic medals and economic power

The plot below shows how the total medal points (Y-axis) vary with GDP (X-axis). Each country is represented by a dot (ok, by a snowflake), and some countries are labeled. Clearly, and not very surprisingly, countries with higher GDP have won more medals in Rio. What is surprising however, is that the relationship is not too far from linear: the red line added to the plot is the OLS regression line, and it turns out that this line summarizes the relationship as well (or as badly) as other, more flexible alternatives (like the loess line shown on the plot in grey). The estimated linear positive relationship implies that, on average, each 1,000 billion of GDP bring about 16 more medal points (so ~315 billion earns you another gold medal).

The other thing to note from the plot is that the relationship is between medal points and total GDP, thus not GDP per capita. In fact, GDP per capita, which measures the relative wealth of a country, has a much weaker relationship with Olympic success with a number of very wealthy, and mostly very small, countries getting zero medals. The correlation of Olympic medal points with GDP is 0.80, while with GDP per capita is 0.21. So it is absolute and not relative wealth that matters more for Olympic glory. This would seem to make sense as it is not money but people who compete at the games, and you need a large pool of contenders to have a chance. But let’s examine more closely whether and how does population size matter.

Olympic medals and population size

The following plot shows how the number of 2016 Rio medal points earned by each country varies with population size. Overall, the relationship is positive, but it is not quite linear, and it is not very consistent (the correlation is 0.40). Some very populous countries, like India, Indonesia, and Pakistan have won very few medals, and some very small ones have won at least one. The implied effect of population size is also small in substantive terms: each 10 million people are associated with 1 more medal point (so, a bronze); for reference three quarters of the countries in the dataset have less than 25 million inhabitants.

Putting everything together

Now, we can put both GDP and population size in the same statistical model with the aim of summarizing the observed distribution of medal points as best as we can. In addition to these two predictors, we can add an interaction between the two, as well as different non-linear transformations of the individual predictors. In fact, the possibilities for modeling are quite a few even with only two predictors, so we have to pick a standard for selecting the best model. As the goal is to describe the distribution of medal points, it makes sense to use the sum of the errors (the absolute values of the differences between the actual and predicted medal score for each country) that the models make as a benchmark.

I find that two models describe the data almost equally well. Both use simple OLS linear regression. The first one features population size, GDP, and GDP squared. In this multivariate model, population size turns out to have a negative relationship with Olympic success, net of economic power. GDP has a positive relationship, but the quadratic term implies that the effect is not truly linear but declines in magnitude with higher values of GDP. The substantive interpretation of this model is something along these lines: Olympic success increases at a slightly declining rate with the economic power of a country, but given a certain level of economic power, less populous countries do better. The sum of errors of Model 1 is 1691 medal points.

The second model is similar, but instead of the squared term for GDP it features an interaction between GDP and population size. The interaction turns out to be negative. This implies that economically powerful but populous countries do less well than their level of GDP alone would suggest. This interpretation is a bit strange as population size is positively associated with GDP and seems to suggest that it is relative wealth (GDP per capita) that matters, but this turns out not to be the case, as any model that features GDP per capita has a bigger sum of errors than either Model 1 or Model 2.

Model 1

Model 2

Population size

– 0.20

– 0.09

GDP

+ 0.04

+ 0.03

GDP squared

– 0.00000008

/

GDP*Population

/

-0.0000008

Sum of errors

1691

1678

Adjusted R-squared

0.83

0.81

Both models presented so far are linear which is not entirely appropriate given that the outcome variable – medal points – is constrained to be non-negative and is not normally distributed. The models actually predict that some countries, like Kenya, should get a negative number of medal points, which is clearly impossible. To remedy that, we can use statistical models specifically developed for non-negative (count) data: Poisson, negative binomial, or even hurdle or zero-inflated models that can account for the excess number of countries with no medal points at all. I spend a good deal of time experimenting with these models, but I didn’t find any that improved at all on the simple linear models described above (it is actually quite hard even evaluating the performance of these non-linear models). Let me know if you find a different model that does better than the ones reported here. (But please no geographical dummies or past Olympic performance measures; also, the Olympic delegation size would be a mediator so not a proper predictor).

The one model I can find that outperforms the simple OLS regressions is a generalized additive model (GAM) with a flexible form for the interaction. This model has a sum of errors of 1485, and the interaction surface looks like this:

In conclusion, do the population size, economic power and wealth of countries account for their success at the 2016 Olympic games? Yes, to a large extent. It is economic power and not relative wealth that matters more, and population size actually has a negative effect once economic power is taken into account. So the relationships are rather complex and, to remind, far from deterministic.

Here is the data (text file): olypm. Let me know if you interested in the R script for the analysis, and I will post it.
Finally, here is a ranking of the countries by the size of the model error (based on Model 2; negative predictions have been replaced with zero). This can be interpreted in the following way: the best way to summarize the distribution of medal points won at the 2016 Rio Olympic games as a function of population size and GDP is the model described above. This model implies a prediction for each country. The ones that outperform their model predictions have achieved more than their level of GDP and economic size imply. The ones with negative errors underperform in the sense that they have achieved less than their level of GDP and economic size imply.

It’s Oscars season again so why not explore how predictable (my) movie tastes are. This has literally been a million dollar problem and obviously I am not gonna solve it here, but it’s fun and slightly educational to do some number crunching, so why not. Below, I will proceed from a simple linear regression to a generalized additive model to an ordered logistic regression analysis. And I will illustrate the results with nice plots along the way. Of course, all done in R (you can get the script here).

DataThe data for this little project comes from the IMDb website and, in particular, from my personal ratings of 442 titles recorded there. IMDb keeps the movies you have rated in a nice little table which includes information on the movie title, director, duration, year of release, genre, IMDb rating, and a few other less interesting variables. Conveniently, you can export the data directly as a csv file.

Outcome variableThe outcome variable that I want to predict is my personal movie rating. IMDb lets you score movies with one to ten stars. Half-points and other fractions are not allowed. It is a tricky variable to work with. It is obviously not a continuous one; at the same time ten ordered categories are a bit too many to treat as a regular categorical variable. Figure 1 plots the frequency distribution (black bars) and density (red area) of my ratings and the density of the IMDb scores (in blue) for the 442 observations in the data.

The mean of my ratings is a good 0.9 points lower than the IMDb scores, which are also less dispersed and have a higher peak (can you say ‘kurtosis’).

Data-generating processSome reflection on how the data is generated can highlight its potential shortcomings. First, life is short and I try not to waste my time watching bad movies. Second, even if I get fooled to start watching a bad movie, usually I would not bother rating it on IMDb.There are occasional two- and three-star scores, but these are usually movies that were terrible and annoyed me for some reason or another (like, for example, getting a Cannes award or featuring Bill Murray). The data-generating process leads to a selection bias with two important implications. First, the effective range of variation of both the outcome and the main predictor variables is restricted, giving the models less information to work with. Second, because movies with a decent IMDb ratings which I disliked have a lower chance of being recorded in the dataset, the relationship we find in the sample will overestimate the real link between my ratings and the IMDb ones.

Take one: linear regressionEnough preliminaries, let’s get to business. An ordinary linear regression model is a common starting point for analysis and its results can serve as a baseline. Here are the estimates that lm provides for regressing my ratings on IMDb scores:

The intercept indicates that on average my ratings are more than half a point lower. The positive coefficient of IMDb score is positive and very close to one which implies that one point higher (lower) IMDb rating would predict, on average, one point higher (lower) personal rating. Figure 2 plots the relationship between the two variables (for an interactive version of the scatter plot, click here):

The solid black line is the regression fit, the blue one shows a non-parametric loess smoothing which suggests some non-linearity in the relationship that we will explore later.

Although the IMDb score coefficient is highly statistically significant that should not fool us that we have gained much predictive capacity. The model fit is rather poor. The root mean squared error is 1.25 which is large given the variation in the data. But the inadequate fit is most clearly visible if we plot the actual data versus the predictions. Figure 3 below does just that. The grey bars show the prediction plus/minus two predictive standard errors. If the predictions derived from the model were good, the dots (observations) would be very close to the diagonal (indicated by the dotted line). In this case, they are not. The model does a particularly bad job in predicting very low and very high ratings.

We can also see how little information IMDb scores contain about (my) personal scores by going back to the raw data. Figure 4 plots to density of my ratings for two sets of values of IMDb scores – from 6.5 to 7.5 (blue) and from 7.5- to 8.5 (red). The means for the two sets differ somewhat, but the overlap in the density is great.

In sum, knowing the IMDb rating provides some information but on its own doesn’t get us very far in predicting what my score would be.

Take two: adding predictorsLet’s add more variables to see if things improve. Some playing around shows that among the available candidates only the year of release of the movie and dummies for a few genres and directors (selected only from those with more than four movies in the data) give any leverage.

The fit improves somewhat. The root mean squared error of this model is 1.14. Moreover, looking again at the actual versus predicted ratings, the fit is better, especially for highly rated movies – no surprise given that the director dummies pick these up.

The last variable in the regression above is the year of release of the movie. It is coded as the difference from 2014, so the positive coefficient implies that older movies get higher ratings. The statistically significant effect, however, has no straightforward predictive interpretation. The reason is again selection bias. I have only watched movies released before the 1990s that have withstood the test of time. So even though in the sample older films have higher scores, it is highly unlikely that if I pick a random film made in the 1970s I would like it more than a random film made after 2010. In any case, Figure 6 below plots the year of release versus the residuals from the regression of my ratings on IMDb scores (for the subset of films after 1960). We can see that the relationship is likely nonlinear (and that I really dislike comedies from the 1980s).

So far both regressions assumed that the relationship between the predictors and the outcome is linear. Needless to say, there is no compelling reason why this should be the case. Maybe our predictions will improve if we allow the relationships to take any form. This calls for a generalized additive model.

Take three: generalized additive model (GAM)In R, we can use the mgcv library to fit a GAM. It doesn’t make sense to hypothesize non-linear effects for binary variables, so we only smooth the effects of IMDb rating and year of release. But why stop there, perhaps the non-linear effects of IMDb rating and release year are not independent, why not allow them to interact!

Well, the root mean squared error drops to 1.11 and the jointly smoothed (with a full tensor product smooth) variables are significant, but the added predictive value is minimal in this case. Nevertheless, the plot below shows the smoothed terms are more appropriate than the linear ones, and that there is a complex interaction between the two:

Take four: models for categorical dataSo far we treated personal movie ratings as if they were a continuous variable, but they are not – taking into account that they are essentially an ordered categorical variable might help. But ten categories, while possible to model, would make the analysis rather unwieldy, so we recode the personal ratings into five categories without much loss of information: 5 and less, 6,7,8,9 and more.

We can first see a nonparametric conditional destiny plot of the newly created categorical variable as a function of IMDb scores:

The plot shows the observed density for each category of the outcome variable along the range of the predictor. For example, for a film with an IMDb rating of ‘6’, about 35% of the personal scores are ‘5’, a further 50% are ‘6’, and the remaining 15% are ‘7’. Remember that the plot is based on the observed conditional frequencies only (with some smoothing), not on the projections of a model. But the small ups and downs seem pretty idiosyncratic. We can also fit an ordered logistic regression model, which would be appropriated for the categorical outcome variable we have, and plot its predicted probabilities given the model.

The coefficients of the two predictors are significant. The plot below shows the predicted probability of the outcome variable – personal movie rating – being in each of the five categories as a function of IMDb rating and illustrates the substantive scale of the effect.

Compared to the non-parametric conditional density plot above, these model-based predictions are much smoother and have ‘disciplined’ the effect of the predictor to follow a systematic pattern.

It is interesting to ponder which of the two would be more useful for out-of-sample predictions. Despite the fact that the non-parametric one is more faithful to the current data, I think I would go for the parametric model projections. After all, is it really plausible that a random film with an IMDb rating of 5 would have lower chance a getting a 5 from me than a film with an IMDb rating of 6, as the non-parametric conditional density plot suggests? I don’t think so. Interestingly, in this case the parametric model has actually corrected for some of the selection bias and made for more plausible out-of-sample predictions.

ConclusionIn sum, whatever the method, it is not very fruitful to try to predict how much a person (or at least, the particular person writing this) would like a movie based on the average rating the movie gets and covariates like the genre or the director. Non-linear regressions and other modeling tricks offer only marginal predictive improvements over a simple linear regression approach, but bring plenty of insight about the data itself.

What is the way ahead? Obviously, one would want to get more relevant predictors, but, unfortunately, IMDb seems to have a policy against web-scrapping from its database, so one would either have to ask for permission or look at a different website with a more liberal policy (like Rotten Tomatoes perhaps). For me, the purpose of this exercise has been mostly in its methodological educational value, so I think I will leave it at that. Finally, don’t forget to check out the interactive scatterplot of the data used here which shows a user’s entire movie rating history at a glance.

EndnoteAs you would have noted, the IMDb ratings come at a greater level of precision (like 7.3) than the one available for individual users (like 7). So a user who really thinks that a film is worth 7.5 has to pick 7 or 8, but its average IMDb score could well be 7.5. If the rating categories available to the user are indeed too coarse, this would show up in the relationship with the IMDb score: movies with an average score of 7.5 would be less predictable that movies with an average score of either 7 or 8. To test this conjecture, a rerun the linear regression models on two subsets of the data: one comprising the movies with an average IMDb rating between 5.9 and 6.1, 6.9 an 7.1, etc., and a second one comprising those with an average IMDb rating between 5.4 and 5.6, 6.4 and 6.6, etc. The fit of the regression for the first group was better than for the second (RMSE of 1.07 vs. 1.11), but, frankly, I expected a more dramatic difference. So maybe ten categories are just enough.

Follow and connect

Dimiter Toshkov

Associate Professor of Public Administration at Leiden University in The Netherlands