# get_moments_cpp: Calculate variational moments during the updates In zhenkewu/lotR: Latent class analysis with Observed Trees in R (lotR)

 get_moments_cpp R Documentation

## Calculate variational moments during the updates

### Description

Get all moments that need update when iterating over a total of p internal and leaf nodes

### Usage

```get_moments_cpp(
prob,
prob_gamma,
mu_gamma,
sigma_gamma,
mu_alpha,
Sigma_alpha,
anc,
cardanc
)
```

### Arguments

 `prob` variational probabilities for `s_u`; length p `prob_gamma` should be fixed: `c(1,rep(0,p-1))` `mu_gamma` variational Gaussian means (for `s_u=1` component) for J*K logit(class-specific response probabilities); (J,K,p) array; In R, we used a list of p (J,K) matrices `sigma_gamma` variational Gaussian variances (for `s_u=1` component) for J*K logit(class-specific response probabilities); (J,K,p) array `mu_alpha` variational Gaussian mean vectors (for `s_u=1` component) - this is a p by K-1 matrix; in R, we used a list of p vectors (each of length K-1) `Sigma_alpha` variational Gaussian variances (for `s_u=1` component) this is an array of dimension (K-1, K-1, p); in R, we used a list of p matrices, each of dimension K-1 by K-1. `anc` a list of pL vectors, each vector has the node ids of the ancestors; lengths may differ. The ancestors include the node concerned. `cardanc` a numeric vector of length pL; integers. The number of ancestors for each leaf node

### Value

a List

return List::create(Named("E_beta")=E_beta, Named("E_beta_sq")=E_beta_sq, Named("E_eta")=E_eta, Named("E_eta_sq")=E_eta_sq);

### Examples

```# illustrates the calculation of the various moments:

prob = 0.2
mu1 = 2
mu0 = 0

sigma1 = 3
sigma0 = 1

N = 10000
s = rbinom(N,1,prob)
y <- rep(NA,length(s))
for (i in seq_along(s)){
y[i] <- rnorm(1,mu1,sqrt(sigma1))*s[i] +
rnorm(1,mu0,sqrt(sigma0))*(1-s[i])
}

var(s*y)
prob*(sigma1+(1-prob)*mu1^2)

mean(y^2)
prob*(sigma1+mu1^2) + (1-prob)*sigma0

mean(s*y^2)
prob*(sigma1+mu1^2)

mean(s*y)
prob*mu1
```

zhenkewu/lotR documentation built on April 24, 2022, 2:36 a.m.