Hi All,
Unfortunately, I have encountered another problem.
The objective of the function I mentioned in the previous post is to return the conditional mean vector and covariance matrix (stuck together as one matrix) based on the original mean vector, covariance matrix and conditional value(s) of mu (taking care to use inverse rather than solve):
conditional_MNV <- nimbleFunction(
run = function(mu_vec = double(1), cov_mat = double(2), mu_vec_cond = double(1)){
mat_size <- length(mu_vec)
cond_vec_size <- length(mu_vec_cond)
index_end <- mat_size-cond_vec_size
#convert vectors to matrices
mu_vec_mat <- matrix(0, nrow = mat_size, ncol = 1)
mu_vec_mat[,1] <- mu_vec
mu_vec_cond_mat <- matrix(0, nrow = cond_vec_size, ncol = 1)
mu_vec_cond_mat[,1] <- mu_vec_cond
resMatrix <- matrix(0, nrow = index_end, ncol = index_end+1)
resMatrix[1:index_end,1:index_end] <- cov_mat[1:index_end,1:index_end] - cov_mat[1:index_end,(index_end+1):mat_size]%*%inverse(cov_mat[(index_end+1):mat_size,(index_end+1):mat_size])%*%cov_mat[(index_end+1):mat_size,1:index_end]
resMatrix[1:index_end,index_end +1] <- mu_vec_mat[1:index_end,] + cov_mat[1:index_end,(index_end+1):mat_size]%*%inverse(cov_mat[(index_end+1):mat_size,(index_end+1):mat_size])%*%(mu_vec_cond_mat - mu_vec_mat[(index_end+1):mat_size,])
returnType(double(2))
return(resMatrix)
}
)
Defining the following values to test the function:
R = matrix(c(1, 0.7, 0.2,
0.7, 1, -0.5,
0.2, -0.5, 1), 3)
sd_vec <- c(.2,0.5,0.1)
cov_mat <-diag(sd_marg)%*%R%*%diag(sd_marg)
mu_vec <- c(5, 2,1)
mu_condit_val <- 3
However, when I want to include this in the nimbleCode I get a large number of compiler errors.
mod_nimble2<- nimbleCode({
condit_dist_1[1:2,1:3] <- conditional_MNV(mu_vec[1:3], cov_mat[1:3, 1:3], mu_condit_val[1:1])
})
Any help would be greatly appreciated.
Philip