update_reg: Update coefficient vector of multiple linear regression

View source: R/RcppExports.R

update_regR Documentation

Update coefficient vector of multiple linear regression

Description

This function updates the coefficient vector of a multiple linear regression.

Usage

update_reg(mu0, Tau0, XSigX, XSigU)

Arguments

mu0

The mean vector of the normal prior distribution for the coefficient vector.

Tau0

The precision matrix (i.e. inverted covariance matrix) of the normal prior distribution for the coefficient vector.

XSigX

The matrix ∑_{n=1}^N X_n'Σ^{-1}X_n. See below for details.

XSigU

The vector ∑_{n=1}^N X_n'Σ^{-1}U_n. See below for details.

Details

This function draws from the posterior distribution of β in the linear utility equation

U_n = X_nβ + ε_n,

where U_n is the (latent, but here assumed to be known) utility vector of decider n = 1,…,N, X_n is the design matrix build from the choice characteristics faced by n, β is the unknown coefficient vector (this can be either the fixed coefficient vector α or the decider-specific coefficient vector β_n), and ε_n is the error term assumed to be normally distributed with mean 0 and (known) covariance matrix Σ. A priori we assume the (conjugate) normal prior distribution

β \sim N(μ_0,T_0)

with mean vector μ_0 and precision matrix (i.e. inverted covariance matrix) T_0. The posterior distribution for β is normal with covariance matrix

Σ_1 = (T_0 + ∑_{n=1}^N X_n'Σ^{-1}X_n)^{-1}

and mean vector

μ_1 = Σ_1(T_0μ_0 + ∑_{n=1}^N X_n'Σ^{-1}U_n)

. Note the analogy of μ_1 to the generalized least squares estimator

\hat{β}_{GLS} = (∑_{n=1}^N X_n'Σ^{-1}X_n)^{-1} ∑_{n=1}^N X_n'Σ^{-1}U_n

which becomes weighted by the prior parameters μ_0 and T_0.

Value

A vector, a draw from the normal posterior distribution of the coefficient vector in a multiple linear regression.

Examples

### true coefficient vector
beta_true <- matrix(c(-1,1), ncol=1)
### error term covariance matrix
Sigma <- matrix(c(1,0.5,0.2,0.5,1,0.2,0.2,0.2,2), ncol=3)
### draw data
N <- 100
X <- replicate(N, matrix(rnorm(6), ncol=2), simplify = FALSE)
eps <- replicate(N, rmvnorm(mu = c(0,0,0), Sigma = Sigma), simplify = FALSE)
U <- mapply(function(X, eps) X %*% beta_true + eps, X, eps, SIMPLIFY = FALSE)
### prior parameters for coefficient vector
mu0 <- c(0,0)
Tau0 <- diag(2)
### draw from posterior of coefficient vector
XSigX <- Reduce(`+`, lapply(X, function(X) t(X) %*% solve(Sigma) %*% X))
XSigU <- Reduce(`+`, mapply(function(X, U) t(X) %*% solve(Sigma) %*% U, X, U, SIMPLIFY = FALSE))
beta_draws <- replicate(100, update_reg(mu0, Tau0, XSigX, XSigU), simplify = TRUE)
rowMeans(beta_draws)

RprobitB documentation built on Nov. 10, 2022, 5:12 p.m.