# Mixed effect predictive model where dependent variable is constant for each random instance

I am looking for a mixed effect solution to my problem.

I try to model data which contain constant dependent variable $$Y$$ for each random level ngame. $$Y$$ is the sport game outcome, ngame is the id of a game. A game outcome is obviously just one per game, but I need to estimate it measurements times as close as possible. You can think of measurements as a time variable. Variables inside one random instance must be dependent.

All the independent variables are varying. The number of random levels is several thousand.

Without using mixed effect models, I treat it by making max(measurements) models, where each observation is a distinct random factor and $$Y$$ is not constant anymore. As an obvious drawback of this solution I keep tens of models and the degrees of freedom are thousand times fewer compared to the total dataset.

I cannot figure out how to design one mixed effect model that would not fail due to signularity related issues.

A toy dataset: My code which is not good:

library(data.table)
library(lme4)

x1 = rnorm(20)
x2 = rnorm(20)
x3 = rnorm(20)
ngame = rep(1:2, each = 10)
measurements = rep(1:10, 2)

y = c(rep(10, 10), rep(5, 10))

dat = data.table(
x1 = x1,
x2 = x2,
x3 = x3,
ngame = ngame,
measurements = measurements,
y = y
)

lme4::lmer(y ~ x1 + x2 + x3 + measurements + (1|ngame), data = dat)


I want to supply c(x1,x2,x3,measurement) to a trained model to get $$Y'$$. However what I have built so far does not create a model without errors or warnings, and I can undesratnd why, but cannot understand how to fix it.

Update:

I actually created a small real dataset for this problem consisting of 20 random factor levels. https://raw.githubusercontent.com/alexmosc/ds_lectures/master/sample_game_dat.csv When I run my lmer model on it I get the error Error in eval_f(x, ...) : Downdated VtV is not positive definite.

• I think a core issue in the simulation showed is that the variable y is devoid of relations with any of the explanatory variables used. In addition to that, the "random effect" has just two levels; a basic rule of thumb is to have at least 5 levels. – usεr11852 Dec 27 '19 at 12:54
• @usεr11852saysReinstateMonic, hello, thank you. I actually created a small real dataset for this problem consisting of 20 random factor levels. raw.githubusercontent.com/alexmosc/ds_lectures/master/… When I run my lmer model on it I get the error Error in eval_f(x, ...) : Downdated VtV is not positive definite. – Alexey Burnakov Dec 27 '19 at 13:33
• In order to estimate a mixed-effects model the outcome variable y needs to change/vary within the same level of the grouping variable ngame. – Dimitris Rizopoulos Dec 27 '19 at 20:04
• Ok, thanks. This is the problem. Is it possible to use pseudo random factor "measurements" and make predictions for each measurement (the way I am doing it with regular models)? Or is it a bare abuse of the method? – Alexey Burnakov Dec 28 '19 at 11:22

I figured out how to deal with this problem (thnx @Dimitris Rizopoulo).

Sample of my data is here: https://raw.githubusercontent.com/alexmosc/ds_lectures/master/sampledat.csv

I included only a limited set of predictors... 50 basketball games drawn randomly.

league,team1,team2,measurements,ngame,score_diff,win1,win2,final_score_diff,score_diff_delta
El Salvador. Liga Superior,A.D. Isidro Metapán,Santa Tecla,1,70,3,3.58,1.3,-2,-5
El Salvador. Liga Superior,A.D. Isidro Metapán,Santa Tecla,3,70,0,4.22,1.24,-2,-2
El Salvador. Liga Superior,A.D. Isidro Metapán,Santa Tecla,6,70,6,2.304,1.61,-2,-8


In the data table by link I want to predict final_score_diff that is sometimes called final point differential, or the delta between team final point total.

But to ensure that my Y is not constant, I derive score_diff_delta as final_score_diff - score_diff (a current score difference). The target is then variable and I also can restore the final difference without information loss.

The target probability density is also looking quite good, and I treat it as a continuous case: Next thing I do is fitting a Laplace mixed linear model where the game ID ngame was chosen to be a random factor.

So far so good... The only problem that remains is that my real data are 1000x as large, and the model fitting time is ridiculous! Can you please advise on how to go about this obstacle? Till then I experiment with modestly sized samples.

## load libs

library(data.table)
library(lqmm)

## Laplace mixed model

qs <- seq(0.05, 0.95, 0.05)

qmem <-
lqmm(
fixed = score_diff_delta ~
measurements *
(
score_diff +
win1 +
win2
),
random = ~ 1,
group = ngame,
covariance = "pdDiag",
tau = qs,
nK = 7,
type = "robust",
rule = 1,
data = sampledat,
contrasts = NULL,
fit = TRUE
)

qmem_coefs <- coefficients(qmem)

## predict

frml <-
score_diff_delta ~
measurements *
(
score_diff +
win1 +
win2
)

desmat <- model.matrix(frml, data = sampledat) #design matrix

y_hat <- desmat %*% qmem_coefs

y <- sampledat[, score_diff_delta]

mean(abs(y - y_hat[, '0.50']), na.rm = T)

cor.test(y, y_hat[, '0.50'])

plot(y, type = 'l', col = 'blue'); lines(y_hat[, '0.50'], col = 'red')

plot(y_hat[, '0.50'], y)