Hello All, I am trying to implement a latent variable instrument model based on Ebbes, P 2004.
I am seeing if I can recover an unbiased parameter if there is endogeneity in the regression model.
The problem is I keep gettin this error: Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: normal_lpdf: Location parameter[17] is nan, but must be finite! (in ‘string’, line 32, column 2 to column 28)
Here is my R code which generates the data
Set the seed for reproducibility
set.seed(42)
beta0_true ← 1 # true intercept
beta1_true ← 2 # true coefficient for x
sigma_true ← 1 # true error standard deviation
Simulate endogenous independent variable x
N ← 400 # number of observations
theta_true ← rbeta(N, 2, 5) # true values for theta (latent instrument)
x ← rnorm(N, 5 * theta_true, 1) # endogenous independent variable x
Simulate dependent variable y
v ← rnorm(N, 0, 1) # endogenous part of x
mu ← beta0_true + beta1_true * x # linear predictor
y ← rnorm(N, mu , sigma_true) # dependent variable y
model ~ lm(y ~ x)
summary(model)
data ← list(N = N, x = x, y = y)
And here is the Stan Code
data {
int<lower=0> N; // number of observations
vector[N] y; // outcome variable
vector[N] x; // endogenous variable
}
parameters {
real<lower=0> alpha; // concentration parameter for Dirichlet process
real<lower=0> lambda; // parameter for the baseline prior distribution
simplex[N] theta; // latent instrument variable
real beta0; // intercept
real beta1; // coefficient for x
real<lower=0> sigma; // error standard deviation
}
transformed parameters {
vector[N] v; // endogenous part of x
// Compute the endogenous part of x
vector[N] theta_normalized = softmax(theta);
v = theta_normalized .* sqrt(x) / sum(theta_normalized);
}
model {
vector[N] mu; // linear predictor
// Priors
alpha ~ gamma(1, 1); // prior for the concentration parameter
lambda ~ gamma(1, 1); // prior for the parameter of the baseline prior distribution
theta ~ dirichlet(rep_vector(lambda, N)); // prior for the latent instrument from Dirichlet process
beta0 ~ normal(0, 1); // prior for the intercept
beta1 ~ normal(0, 1); // prior for the coefficient
sigma ~ cauchy(0, 5); // prior for the error standard deviation
// Linear predictor
mu = beta0 + x * beta1;
// Likelihood
y ~ normal(mu + v, sigma);
}
generated quantities {
vector[N] v_generated;
v_generated = softmax(theta) .* sqrt(x) / sum(softmax(theta));
}
****