# Interpreting matrix notation to run MLE in R

I am trying to re-create some indicators from the World Bank, using the methodology described in this paper, and I need to do maximum likelihood estimation, preferably using R.

The aim is to get an overall (unobserved) indicator from many different (observed) scores. My data looks like this:

   Country   Score Source
<chr>     <dbl> <chr>
1 Albania   0.6   EIU
2 Albania   0.797 GCS
3 Albania   0.833 WMO
4 Albania   0.756 PRS
5 Algeria   0.45  EIU
6 Algeria   0.739 GCS
7 Algeria   0.5   WMO
8 Algeria   0.634 PRS
9 Argentina 0.65  EIU
10 Argentina 0.725 GCS
11 Argentina 0.75  WMO
12 Argentina 0.795 PRS


Based on the formulas and variables given in the paper, I am estimating the following model:

$$y_j{_k} = \alpha_k + \beta_k(g_j + \varepsilon_j{_k})$$

Where $$y{_j}{_k}$$ is the observed score of country $$j$$ from source $$k$$. This observed score is a linear function of an unobserved indicator, $$g_j$$, for each country $$j$$ and a disturbance term, $$\varepsilon_j{_k}$$, where $$\alpha_k$$ and $$\beta_k$$ are parameters which map the unobserved indicator in country $$j$$, $$g_j$$ into the observed data $$y_j{_k}$$. $$g_j$$ is assumed to be a normally-distributed random variable with mean zero and variance one. We also assume the error term is normally distributed, with zero mean and a variance that is the same across countries, but differs across indicators, i.e. $$V[\varepsilon_j{_k}] = \sigma_k^{2}$$. We also assume that the errors are independent across sources, i.e., $$E[\varepsilon_j{_k}\varepsilon_j{_m}] = 0$$ for source $$m$$ different from source $$k$$.

I want to estimate the parameters $$\alpha_k$$, $$\beta_k$$, and $$\sigma_k^2$$ for every data source $$k$$, using maximum likelihood. The paper I referenced says that the contribution to the log-likelihood of country $$j$$ is given by:

$$ln L(\alpha, \beta, \sigma^2) \propto ln|\Omega| + (y_j - \alpha)'\Omega^{-1}(y_j-\alpha)$$

Where $$\alpha = \Big(\alpha_1, ..., \alpha_K{_j}\Big)$$, $$\beta = \Big(\beta_1, ..., \beta_K{_j}\Big)$$, and $$\sigma^2 = \Big(\sigma^2_1, ..., \sigma^2_K{_j}\Big)$$, and $$B$$ and $$\Sigma$$ are diagonal matrices with $$\alpha$$ and $$\sigma^2$$ on the diagonal. Furthermore, the mean of the vector of observed data for each country $$j$$, $$y_j$$, is $$\alpha$$ and the variance is $$\Omega = \beta\beta' + B\Sigma B'$$. Summing over all countries $$j$$ and then maximizing over the unknown parameters should give the maximum-likelihood estimates of $$\alpha_k$$, $$\beta_k$$, and $$\sigma^2_k$$ for every data source $$k$$.

There are a couple of things that confuse me about this notation:

• are $$\alpha$$, $$\beta$$, and $$\sigma^2$$ matrices or vectors? The paper gives them inside of big parens, which I take to be matrix notation. But they only have one dimension, so are they $$1$$ X $$k$$, or $$k$$ X $$k$$, with $$\alpha ... \alpha_K{_j}$$ as the diagonal?
• why are we taking transposes of diagonal matrices (like $$B$$)?
• why are there pipes around the log of omega, i.e., $$ln|\Omega|$$ instead of parens, i.e., $$ln(\Omega)$$

So, most of the resources I can find for calculating MLE in R are based on pretty simple examples, where you estimate only a few parameters. In this case, I want to estimate whole matrices of parameters, so I am a little unclear on how to do that. Moreover, we are taking the formula given and summing it across every country.

The best attempt I have so far looks like this (where dat is the dataframe with my data):

nll <- function(alpha1, alpha2, alpha3, alpha4, beta1, beta2, beta3, beta4, sigma1, sigma2, sigma3, sigma4){
alpha <- diag(c(alpha1, alpha2, alpha3, alpha4))
beta <- diag(c(beta1, beta2, beta3, beta4))
sigma <- diag(c(sigma1, sigma2, sigma3, sigma4))

LL <- 0
for (country in unique(dat$$Country)){ ix <- dat$$Country == country