Functions providing access to the Log Likelihood, Gradient, and Hessian of the collapsed pibble model. Note: These are convenience functions but are not as optimized as direct coding of the PibbleCollapsed C++ class due to a lack of Memoization. By contrast function optimPibbleCollapsed is much more optimized and massively cuts down on repeated calculations. A more efficient Rcpp module based implementation of these functions may following if the future. For model details see optimPibbleCollapsed documentation

loglikPibbleCollapsed(Y, upsilon, ThetaX, KInv, AInv, eta, sylv = FALSE)

gradPibbleCollapsed(Y, upsilon, ThetaX, KInv, AInv, eta, sylv = FALSE)

hessPibbleCollapsed(Y, upsilon, ThetaX, KInv, AInv, eta, sylv = FALSE)

Arguments

Y

D x N matrix of counts

upsilon

(must be > D)

ThetaX

D-1 x N matrix formed by Theta*X (Theta is Prior mean for regression coefficients)

KInv

Inverse of K for LTP (for Pibble defined as KInv = solve(Xi))

AInv

Inverse of A for LTP (for Pibble defined as AInv = solve(diag(N)+ X'GammaX) )

eta

matrix (D-1)xN of parameter values at which to calculate quantities

sylv

(default:false) if true and if N < D-1 will use sylvester determinant identity to speed computation

Value

see below

  • loglikPibbleCollapsed - double

  • gradPibbleCollapsed - vector

  • hessPibbleCollapsed- matrix

Examples

D <- 10
Q <- 2
N <- 30

# Simulate Data
Sigma <- diag(sample(1:8, D-1, replace=TRUE))
Sigma[2, 3] <- Sigma[3,2] <- -1
Gamma <- diag(sqrt(rnorm(Q)^2))
Theta <- matrix(0, D-1, Q)
Phi <-  Theta + t(chol(Sigma))%*%matrix(rnorm(Q*(D-1)), nrow=D-1)%*%chol(Gamma)
X <- matrix(rnorm(N*(Q-1)), Q-1, N)
X <- rbind(1, X)
Eta <- Phi%*%X + t(chol(Sigma))%*%matrix(rnorm(N*(D-1)), nrow=D-1)
Pi <- t(alrInv(t(Eta)))
Y <- matrix(0, D, N)
for (i in 1:N) Y[,i] <- rmultinom(1, sample(5000:10000), prob = Pi[,i])

# Priors
upsilon <- D+10
Xi <- Sigma*(upsilon-D)

# Precompute
KInv <- solve(Xi)
AInv <- solve(diag(N)+ t(X)%*%Gamma%*%X)
ThetaX <- Theta%*%X


loglikPibbleCollapsed(Y, upsilon, ThetaX, KInv, AInv, Eta)
#> [1] -115558.2
gradPibbleCollapsed(Y, upsilon, ThetaX, KInv, AInv, Eta)[1:5]
#> [1]  4.7440863 -2.0141509 -0.6579329 -5.0887964 -0.9877219
hessPibbleCollapsed(Y, upsilon, ThetaX, KInv, AInv, Eta)[1:5,1:5]
#>               [,1]        [,2]          [,3]          [,4]          [,5]
#> [1,] -1454.9780390  1.23680090  0.2229720363  1.450126e+03  0.0531649019
#> [2,]     1.2368009 -3.57827522 -0.2111650936  6.691041e-01  0.0287388480
#> [3,]     0.2229720 -0.21116509 -0.5506432005  1.461724e-01 -0.0003659692
#> [4,]  1450.1262939  0.66910413  0.1461724285 -1.453056e+03 -0.0140213131
#> [5,]     0.0531649  0.02873885 -0.0003659692 -1.402131e-02 -0.4031252063