I have a standard code for ggplot2 which I use to make line graphs, scatter plots, and histograms.
For lines or scatters:
p<- ggplot(x, aes(x=Year, y=Rank, colour=Uni, group=Uni)) #colour lines by variable Uni #group Uni labelled variables in the same line
p + #you get an error if not for this step
ggtitle(“QS University Rankings 2008-2013”) +
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank()) +
For all the flak China receives about its greenhouse gas emissions, the average Chinese produces less than a third the amount of CO2 than his American counterpart. It just so happens that there are 1.3 billion Chinese, and 0.3 billion Americans, so China ends up producing more CO2.
Carbon dioxide and other greenhouse gases, such as methane and carbon monoxide, are produced from burning petrol, growing rice, and raising cattle . These greenhouse gases let in sun rays, but do not let out the heat that the rays generate on earth. This results in a greenhouse effect, where global temperatures are purported to be rising as a result of human activities.
The below map shows the per-capita emissions of greenhouse gases:
As you can see, the least damage is done by people in Africa, South Asia, and Latin America. But these places also happen to be the poorest places: Because they don’t have much industry, they don’t churn out much CO2.
The below plot shows the correlation between poverty and green-ness. As you can see, each dollar of a rich person is attached to a smaller carbon cost than the dollar of a poor person. This is partially because rich people get most of their manufacturing done by poor people, but also because rich people are more environmentally conscious.
Lastly, here is a map of CO2 emissions per dollar of GDP, which shows how green different economies are:
CO2 emissions per Dollar of output are lowest in:
EU and Japan: highly regulated and environmentally conscious
sub-Saharan Africa: subsistence-based economies
…and highest in the industrializing economies of Asia.
Kudos to Brazilian output for being so green, despite the country’s middle-income status. Were these statistics to factor in the CO2 absorption from rainforests, Brazil and other equatorial countries would appear even greener.
The QS Rankings are an influential score sheet of universities around the world. They are published annually by Quacquarelli Symonds (QS), a British research company based in London. The rankings for 2013 are out, and I have charted the rankings of this year’s top 10 over the last five years:
Observations from this year’s ranking:
MIT (#1 in 2013) has shot up in the rankings. This is in line with the increasing demand for technical and computer science education. At Harvard, enrollment into the college’s introductory computer science course went up, from around 300 students in 2008 to almost 800 students in 2013!
Asia’s top scorer is National University of Singapore
The QS Rankings produce an aggregate score, on a scale of 0-100, for each university. The aggregate score is a sum of six weighted scores:
Academic reputation: from a global survey of 62,000 academics (40%)
Student:Faculty ratio (20%)
Citations per Faculty: How many times the university’s research is cited in other sources on Scopus, a database of research (20%)
Employer reputation: from a global survey of 28,000 employers (20%)
Int’l Faculty (Students): proportion of faculty (students) from abroad (5% each)
Note that many of the universities are apart by tiny numbers (MIT, Harvard, Cambridge, UCL, Imperial are all within 1.3 points of each other), which increases the likelihood of bias or error influencing the ranking.
In any case, it appears futile to try and compare massive multi-disciplinary institutions by a single statistic.
However, larger trends – like MIT’s and Stanford’s ascendancy – are noteworthy.
In America, the richest 1% of households earned almost 20% of the income in 2012, which points to a very wide income gap. This presents many social and economic problems, but also a statistical problem: what is the “average” American’s salary?
This average is often reported as GDP per capita: the mean of household incomes. In 2011, the mean household earned $70,000. However, the majority of Americans earned well below $70K that year. The reason for this misrepresentation is rich people: In 2011, Oracle CEO Larry Ellison made almost $100 million, alone adding a dollar to each household’s income, were his salary distributed among everyone – as indeed the mean makes it appear it is.
Here is a graphic of American inequity:
As you can see, the mean would not be such a poor representation (or rich representation) of the average salary if we discounted the top 5%.
In fact, the trimmed mean removes extreme values before calculating the mean. Unfortunately, the trimmed mean is not widely used in data reporting by the agencies that report incomes – the IRS, Bureau of Economic Analysis and the US Census.
In this case, the median is a much better average. This is simply the income right in the middle of the list of incomes.
As you can see, whether you use the Mean or Median makes a very big difference. The median household income is $20,000 lower than the mean household income.
Of course, America is not the only country with a wide economic divide. China, Mexico and Malaysia have similar disparities between rich and poor, while most of South America and Southern Africa are even more polarized, as measured by the Gini coefficient, a measure of economic inequality.
Data from the US Census. Available income data typically lags by two years, which is why graphs stop at 2011; 2012 Data is projected. Graphics produced on R.
The Current Account Balance is a measure of a country’s “profitability”. It is the sum of profits (losses) made from trading with other countries, profits (losses) made from investments in other countries, and cash transfers, such as remittances from expatriates.
As the infographic shows, there isn’t much middle ground when it comes to a current account balance. Most countries have:
large deficits (America, most of Europe, Australia, Brazil, India)
large surpluses (China, most of Southeast Asia, Northern European countries, Russia, Gulf oil producers).
There are a few countries with
small deficits (most Central American states, Pakistan)
small surpluses (most Baltics)
…but they are largely outnumbered by the clear winners and losers of world trade.
The above is not a per-capita infographic, so larger countries tend to be clear winners or losers, while smaller countries are more likely to straddle the divide. Here is the per-capita Current Account Balance map:
This can be interpreted as how much people around the world earn from eachother:
Anglophones (people in America, Canada, UK, Australia) chock up some of the biggest deficits, as do people in the Mediterranean
Oil producers (Saudis, Libyans, Norwegians) profit the most off trade, as do industrious Germans and Japanese. I sense an automobile theme
Most of us are somewhere in the middle
Records: Icelanders have the biggest deficits, Singaporeans the biggest surpluses
On the topic of English-speakers running deficits, here is a recent BBC article that explores the link between the English language and the act of saving money.
2010 Current Account Balance and Population data from World Bank; graphics produced on R.
Suppose you have decided on a suitable model for a timeseries. In this case, we have selected an ARIMA(2,1,3) model, using the Akaike Information Criteria (AIC) as our sole criterion for choosing between various models here, where we model the DJIA.
Note: There are many criteria for choosing a model, and the AIC is only one of them. Thus, the AIC should be used heuristically, in conjunction with t-tests and the Coefficient of Determination, among other statistics. Nonetheless, let us assume that we ran all these tests, and were still satisfied with ARIMA(2,1,3).
An ARIMA(2,1,3) looks like this:
This is not very informative for forecasting future reaizations of a timeseries, because we need to know the values of the coefficients , , etcetera. So we use R’s arima() function, which spits out the following output:
Thus, we revise our model to:
Then, we can forecast the next, say 20, realizations of the DJIA, to produce a forecast plot. We are forecasting values for January 1st 1990 to January 26th 1990, dates for which we have the real values. So, we can overlay these values on our forecast plot:
Note that the forecast is more accurate for predicting the DJIA a few days ahead than later dates. This could be due to:
the model we use
fundamental market movements that could not be forecasted
Which is why data in a vacuum is always pleasant to work with. Next: Data in a vacuum. I will look at data from the biggest vacuum of all – space.
The Akaike Information Critera (AIC) is a widely used measure of a statistical model. It basically quantifies 1) the goodness of fit, and 2) the simplicity/parsimony, of the model into a single statistic.
When comparing two models, the one with the lower AIC is generally “better”. Now, let us apply this powerful tool in comparing various ARIMA models, often used to model time series.
The dataset we will use is the Dow Jones Industrial Average (DJIA), a stock market index that constitutes 30 of America’s biggest companies, such as Hewlett Packard and Boeing. First, let us perform a time plot of the DJIA data. This massive dataframe comprises almost 32000 records, going back to the index’s founding in 1896. There was an actual lag of 3 seconds between me calling the function and R spitting out the below graph!
But it immediately becomes apparent that there is a lot more at play here than an ARIMA model. Since 1896, the DJIA has seen several periods of rapid economic growth, the Great Depression, two World Wars, the Oil shock, the early 2000s recession, the current recession, etcetera. Therefore, I opted to narrow the dataset to the period 1988-1989, which saw relative stability. As is clear from the timeplot, and slow decay of the ACF, the DJIA 1988-1989 timeseries is not stationary:
So, we may want to take the first difference of the DJIA 1988-1989 index. This is expressed in the equation below:
The first difference is thus, the difference between an entry and entry preceding it. The timeseries and AIC of the First Difference are shown below. They indicate a stationary time series.
Now, we can test various ARMA models against the DJIA 1988-1989 First Difference. I will test 25 ARMA models: ARMA(1,1); ARMA(1,2), … , ARMA(3,3), … , ARMA(5,5). To compare these 25 models, I will use the AIC.
I have highlighted in green the two models with the lowest AICs. Their low AIC values suggest that these models nicely straddle the requirements of goodness-of-fit and parsimony. I have also highlighted in red the worst two models: i.e. the models with the highest AICs. Since ARMA(2,3) is the best model for the First Difference of DJIA 1988-1989, we use ARIMA(2,1,3) for DJIA 1988-1989.
The AIC works as such: Some models, such as ARIMA(3,1,3), may offer better fit than ARIMA(2,1,3), but that fit is not worth the loss in parsimony imposed by the addition of additional AR and MA lags. Similarly, models such as ARIMA(1,1,1) may be more parsimonious, but they do not explain DJIA 1988-1989 well enough to justify such an austere model.
Note that the AIC has limitations and should be used heuristically. The above is merely an illustration of how the AIC is used. Nonetheless, it suggests that between 1988 and 1989, the DJIA followed the below ARIMA(2,1,3) model:
Next: Determining the above coefficients, and forecasting the DJIA.
Analysis conducted on R. Credits to the St Louis Fed for the DJIA data.
In my previous post, I wrote about using the autocorrelation function (ACF) to determine if a timeseries is stationary. Now, let us use the ACF to determine seasonality. This is a relatively straightforward procedure.
Firstly, seasonality in a timeseries refers to predictable and recurring trends and patterns over a period of time, normally a year. An example of a seasonal timeseries is retail data, which sees spikes in sales during holiday seasons like Christmas. Another seasonal timeseries is box office data, which sees a spike in sales of movie tickets over the summer season. Yet another example is sales of Hallmark cards, which spike in February for Valentine’s Day.
The below graphs show sales of clothing in the UK, and how these sales follow seasonal trends, spiking in the holiday season:
Note the spikes in sales, which obediently occur every December, in time for Christmas. This is evident in the trail of December plot points (Graph 1), which hover significantly above the sales data for other months, and also in the actual spikes of the line graph (Graph 2).
The above is a simple example of a seasonal timeseries. However, timeseries are not always simply seasonal. For example, a SARMA process comprises of seasonal, autoregressive, and moving average components, hence the acronym. This will not look as obviously seasonal, as the AR and MA processes may overlap with the seasonal process. Thus, a simple timeseries plot, as shown above, will not allow us to appreciate and identify the seasonal element in the series.
Thus, it may be advisable to use an autocorrelation function to determine seasonality. In the case of seasonality, we will observe an ACF as below:
Note that the ACF shows an oscillation, indicative of a seasonal series. Note the peaks occur at lags of 12 months, because April 2011 correlates with April 2012, and 24 months, because April 2011 correlates with April 2013, and so on.
The above analyses were conducted on R. Credits to data.gov.uk and the Office of National Statistics, UK for the data.
The Autocorrelation function is one of the widest used tools in timeseries analysis. It is used to determine stationarity and seasonality.
This refers to whether the series is “going anywhere” over time. Stationary series have a constant value over time.
Below is what a non-stationary series looks like. Note the changing mean.
And below is what a stationary series looks like. This is the first difference of the above series, FYI. Note the constant mean (long term).
The above time series provide strong indications of (non) stationary, but the ACF helps us ascertain this indication.
If a series is non-stationary (moving), its ACF may look a little like this:
The above ACF is “decaying”, or decreasing, very slowly, and remains well above the significance range (dotted blue lines). This is indicative of a non-stationary series.
On the other hand, observe the ACF of a stationary (not going anywhere) series:
Note that the ACF shows exponential decay. This is indicative of a stationary series.
Consider the case of a simple stationary series, like the process shown below:
We do not expect the ACF to be above the significance range for lags 1, 2, … This is intuitively satisfactory, because the above process is purely random, and therefore whether you are looking at a lag of 1 or a lag of 20, the correlation should be theoretically zero, or at least insignificant.
You produce a non-parametric distribution. Then you obtain, say, 10 random variables (RV) from this non-parametric distribution- much the same way as you would obtain random variables from a (parametric) normal distribution with stated mean and variance. But unlike the parametric distribution, where our RVs would occur around the mean (our parameter), RVs from a non-parametric distribution occur within the range bound by the lowest and highest mass point. This was not necessarily an intuitive concept to me, when I first stumbled across it. Which is why this mathematical proof of this range made me feel so much more comfortable:
If our estimate of the RV is a simple weighted-mean of the mass points:
Furthermore, since for RV :
Since , we can express the inequality as:
On the other hand, If we know further information, like individual weights:
Furthermore, since for intercept :
Since , we can express the inequality as:
Thus, it is proven that any estimates of an RV drawn from a non-parametric distribution will be bound by the highest and lowest mass point.
We often use AIC to discern the best model among candidates.
Now suppose we have two (non-parametric) models, which use mass points and weights to model a random variable:
model A uses 4 mass points to model a random variable (i.e. the height of men in Singapore)
model B uses 5 mass points to mode the same random variable
We consider model A to be nested in model B. This is because model A is basically a version of model B, where one mass point is “de-activated”.
Thus, we must not use small differences in AIC or BIC alone to judge between these models. If the model with a constraint on one or more parameters (model A) is regarded as nested on within the model without the constraint (model B) , a chi-square difference test, or Likelihood Ratio (LR) test, is performed to test the reasonableness of the constraint, using a central chi-square with degrees of freedom equal to the number of parameters constraints.
However, under the null hypothesis, the parameter of interest takes its value on the boundary of the parameter space (next post). For this reason, the asymptotic distribution of the chi-square difference, or Likelihood Ratio (LR) statistic, is not that of a central chi-square distributed random variable with one degree of freedom. This boundary problem affects goodness of fit measures like AIC and BIC4. As a result, the AIC and BIC should be used heuristically, in conjunction with graphs and other criteria to evaluate estimates from the chosen model.
Figure 1: Gradient appears to follow a normal distribution more than intercept .
When do we use a parametric model, and when do we use a non-parametric one? In the above example, “Intercept” is one random variable, and “Gradient” is another. I will show you why “Intercept” is better modeled by a non-parametric model, and “Gradient” is better modeled by a parametric one.
In Figure 1, histograms and QQ plots of “Intercept” and “Gradient” show that the latter appears to follow a normal distribution whereas the former does not. As such, a parametric (normal) distribution would not be appropriate for modelling “Intercept”. This leads us to believe that a non-parametric distribution is a better method for estimating “Intercept”.
However, a parametric (normal) distribution might be appropriate for modelling “Gradient”, which appears to follow a normal distribution, according to both its histogram and QQ plot.