# Measurement error for predictors in GLM

**URL:** <https://discourse.mc-stan.org/t/measurement-error-for-predictors-in-glm/1881>\
**Category:** Modeling\
**Tags:** performance, techniques\
**Created:** [September 13, 2017, 11:16am UTC](https://discourse.mc-stan.org/t/measurement-error-for-predictors-in-glm/1881 "2017-09-13T11:16:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [September 13, 2017, 11:16am UTC](https://discourse.mc-stan.org/t/measurement-error-for-predictors-in-glm/1881/1 "2017-09-13T11:16:37Z")

</div>

I am trying to construct a linear model with a poisson measurement error for both predictors and outcomes. The model currently looks like this:

X ~ Poisson(X\_true)  
Y\_mean = X\_true \* beta  
Y ~ Poisson(Y\_mean)  
beta ~ Normal(0, sigma)  
X\_true ~ Lognormal(a,b)  
X\_true, beta \>= 0

where X and Y are both matrices of measured count data (single cell RNA-seq), X\_true and Y\_mean are parameters, a and b are guessed separately from data and sigma is a user-specified constant, currently set to 0.05 (I expect most coefficients to be close to 0).

I have two qeustions about this model:

1. Is the prior for X\_true necessary? The actual distribution of X is quite complex and hard to capture in a reasonable prior - my guess is that bad prior may hinder convergence. I also noticed than in some examples on the forums as well as in McElreath’s book (Chapter 14.1.), no prior is given for parameters corresponding to measurement error on predictors.

2. The number of parameters grows quickly - it is dominated by the size of X\_true - and it takes prohibitively long to fit real datasets (the smallest I am really interested in has X of dimension 20286 \* 124, currently at 5% after 20hours of computation). Initializing X\_true with X greatly sped up convergence for small simulated datasets, but seems to have made little difference for the real data. Since I am not really interested in the latent X\_true, I tried to integrate it out, but failed. My calculus is however rusty - are there any examples of models where it is possible to integrate out non-normal measurement error on predictors? Or is it known to be impossible to integrate out the measurement error in some classes of models?

Thanks for any hints.

Below is the Stan model for completeness:

```
data {
  int num_cells;
  int num_regulators;
  int num_targets;
  int<lower=0> regulator_expression[num_regulators,num_cells];
  int<lower=0> target_expression[num_targets,num_cells];
  real<lower=0> w_prior_sigma;

  real<lower=0> regulator_prior_sigma;
  real<lower=0> regulator_prior_mean;
}

parameters {
  matrix<lower=0>[num_regulators, num_targets] w;
  matrix<lower=0>[num_cells, num_regulators] regulator_expression_mean;
}

model {
  matrix[num_cells, num_targets] target_expression_mean = regulator_expression_mean * w;
  for(reg in 1:num_regulators) {
    regulator_expression[reg,] ~ poisson(regulator_expression_mean[,reg]);
  }

  for(target in 1:num_targets) {
    target_expression[target,] ~ poisson(target_expression_mean[,target]);
  }

  for(reg in 1:num_regulators) {
    regulator_expression_mean[,reg] ~ lognormal(regulator_prior_mean, regulator_prior_sigma);
  }

  for(target in 1:num_targets){
    w[,target] ~ normal(0, w_prior_sigma);
  }
}
```

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [September 17, 2017, 5:04pm UTC](https://discourse.mc-stan.org/t/measurement-error-for-predictors-in-glm/1881/2 "2017-09-17T17:04:55Z")

</div>

> [@martinmodrak](#):
>
> linear model with a poisson measurement error for both predictors and outcomes

You can’t have Poisson measurement error in predictors because Stan doesn’t support discrete parameters. But you can declare `X_true` as continuous, as you seem to have done.

Priors are only necessary insofar as the posterior must be proper. If you want a generative joint model, then `X_true` has to be given a prior distribution because the flat prior on positive real values is improper. Gelman et al.'s _BDA_ also leaves out a lot of priors, but I believe Gelman’s changing his recommended practice going forward to include priors everywhere (publishers already nagging him for _BDA 4_!).

> [@martinmodrak](#):
>
> I am really interested in has X of dimension 20286 \* 124, currently at 5% after 20hours of computation

The speed will depend on how efficiently you coded your model as a Stan program (scale, identification, vectorization, shared computation, initialization, etc.) as well as on how well the model fits the actual data.

How many iterations are you trying?

> [@martinmodrak](#):
>
> is it known to be impossible to integrate out the measurement error in some classes of models?

That’s the right question, but I don’t know the answer. It’d be more efficient to integrate out parameters (marginalize) due to Rao-Blackwell. Obviously, MCMC is integrating out the measurement error in the explicit encoding, but it’s not very efficient.

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [September 18, 2017, 7:36am UTC](https://discourse.mc-stan.org/t/measurement-error-for-predictors-in-glm/1881/3 "2017-09-18T07:36:34Z")

</div>

> [@Bob\_Carpenter](#):
>
> I believe Gelman’s changing his recommended practice going forward to include priors everywhere

Thanks for the input. So I guess the message is that if you cannot formulate a prior for your noisy predictors then you should learn more about your data-generating process :-)

> [@Bob\_Carpenter](#):
>
> How many iterations are you trying?

I am aiming for the default 4000.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [September 19, 2017, 11:12pm UTC](https://discourse.mc-stan.org/t/measurement-error-for-predictors-in-glm/1881/4 "2017-09-19T23:12:59Z")

</div>

> [@martinmodrak](#):
>
> if you cannot formulate a prior for your noisy predictors then you should learn more about your data-generating process :-)

You also know something from the scale of your predictors and output, since you know the form of your likelihood.

> [@martinmodrak](#):
>
> I am aiming for the default 4000.

That’s the default number of post-warmup iterations with four chains (2000 iterations equally divided between warmup and sampling).
