I am getting a error called failed to create the shared library. It will be great if anyone can help me. Thank you so much for your time and for helping me. I attached the simulated sample data as well.
library(nimble)
library(MCMCvis)
library(tidyverse)
library(dplyr)
cap_data <- read.csv("simulated_capture.csv")
dd_fl <- read.csv("simulated_forklength.csv")
b_fl <- read.csv("simulated_breeding_w_sex_uk.csv")
# Custom NIMBLE function for SVR prediction
krr_predict <- nimbleFunction(
run = function(fl = double(1), support_vectors = double(2), alphas = double(1), intercept = double(0), C = double(0)) {
returnType(double(0))
nSV <- dim(support_vectors)[1]
prediction <- intercept
for (i in 1:nSV) {
kernel <- exp(-C * sum((fl - support_vectors[i,])^2)) # RBF kernel
prediction <- prediction + alphas[i] * kernel
}
return(prediction)
}
)
# Register the custom NIMBLE function
nimble::nimbleFunction(
name = "krr_predict",
run = krr_predict
)
y <- cap_data %>%
select(Fall_2014:Fall_2020) %>%
#select(year_2014:year_2020) %>%
as.matrix()
#head(y)
dd_fl <- dd_fl %>%
select(Fall_2014: Fall_2020) %>%
as.matrix()
dd_fl[dd_fl == 0] <- NA
#head(dd_fl)
b_fl <- b_fl %>%
select(Fall_2014:Fall_2020) %>%
#select(year_2014:year_2020) %>%
as.matrix()
#head(y)
#Scaling the forklength
#-----------------------------------------------------------------------#
# Define a function to scale each row separately
logscale_row <- function(row) {
# Filter out NA values
row_without_nas <- row[!
is.na(row)]
# Scale the row
logscaled_row <- log(row_without_nas)
# Replace NA positions with NA in scaled row
logscaled_row_with_nas <- rep(NA, length(row))
logscaled_row_with_nas[!
is.na(row)] <- logscaled_row
return(logscaled_row_with_nas)
}
# Apply the scaling function to each row of the forklength
d_fl <- t(apply(dd_fl, 1, logscale_row))
#d_fl <- dd_fl
#head(d_fl)
#' Get occasion of first capture.
#------------------------------------------------------------------------#
get.first <- function(x) min(which(x != 0))
first <- apply(y, 1, get.first)
#first
code_m <- nimbleCode({
# Priors
for (i in 1:2) {
#alpha[i] ~ dnorm(0, sd = 10)
#beta[i] ~ dnorm(0, sd = 10)
psi[i] ~ dbeta(1, 1)
}
mu_sp_o <- 1
mu_sl_o <- 1
sigma_musp_o <- 0.1
sigma_musl_o <- 0.1
a_sigma <- 0.001
b_sigma <- 1000
sigma ~ dinvgamma(a_sigma, b_sigma)
for (t in 1:nyears) {
mu_sp[t] ~ dgamma(shape = mu_sp_o, scale = sigma_musp_o)
mu_sl[t] ~ dgamma(shape = mu_sl_o, scale = sigma_musl_o)
}
for (i in 1:N) {
for (t in (first[i] + 1):nyears) {
b[i, t] ~ dbern((psi[1] * b[i, t - 1]) + (psi[2] * (1 - b[i, t - 1])))
mu[i, t] <- (mu_sp[t] * b[i, t]) + (mu_sl[t] * (1 - b[i, t]))
fl[i, t] ~ T(dnorm(fl[i, t - 1] + mu[i, t], sd = sigma), fl[i, t - 1], )
}
z[i, first[i]] <- y[i, first[i]] - 1
for (t in (first[i] + 1):nyears) {
z[i, t] ~ dcat(px[z[i, t - 1], 1:2, i, t])
y[i, t] ~ dcat(po[z[i, t], 1:2, i, t])
}
}
# Kernel Ridge Regression parameters
for (i in 1:N) {
for (t in first[i]:nyears) {
logit(phi[i, t]) <- krr_predict(fl[i, t], support_vectors_phi[1:n_sv_phi, 1:n_features], alphas_phi[1:n_sv_phi], intercept_phi, C_phi)
logit(p[i, t]) <- krr_predict(fl[i, t], support_vectors_p[1:n_sv_p, 1:n_features], alphas_p[1:n_sv_p], intercept_p, C_p)
# Latent state process probabilities
px[1, 1, i, t] <- phi[i, t]
px[1, 2, i, t] <- 1 - phi[i, t]
px[2, 1, i, t] <- 0
px[2, 2, i, t] <- 1
# Observation process probabilities
po[1, 1, i, t] <- 1 - p[i, t]
po[1, 2, i, t] <- p[i, t]
po[2, 1, i, t] <- 1
po[2, 2, i, t] <- 0
}
}
})
N = nrow(y)
nyears = ncol(y)
# Determine number of features from d_fl
n_features <- 5
# Example initial values for KRR parameters
n_sv_phi <- 10 # Number of support vectors for phi
n_sv_p <- 10 # Number of support vectors for p
support_vectors_phi <- matrix(runif(n_sv_phi * n_features), nrow = n_sv_phi)
alphas_phi <- runif(n_sv_phi)
intercept_phi <- 0
C_phi <- 0.1
support_vectors_p <- matrix(runif(n_sv_p * n_features), nrow = n_sv_p)
alphas_p <- runif(n_sv_p)
intercept_p <- 0
C_p <- 0.1
# List of constants for the model
constants <- list(
N = N, # number of individuals
nyears = nyears, # number of years
first = first, # random initial times for individuals
n_sv_phi = n_sv_phi,
n_sv_p = n_sv_p,
n_features = n_features
)
# Data for the model
data <- list(y=y + 1,b = b_fl, fl = d_fl)
# Initializing fl
fl_inits_temp <- dd_fl
replace_nas_increasing_order <- function(row) {
na_indices <- which(
is.na(row))
for (i in na_indices) {
if (i == 1) {
row[i] <- row[i + 1] + 1
} else if (i == length(row)) {
row[i] <- row[i - 1] + 1
} else {
# Adjust the range to handle consecutive NAs
start_range <- ifelse(!is.finite(row[i - 1]), 0, row[i - 1] + 1)
end_range <- ifelse(!is.finite(row[i + 1]), start_range + 1, row[i + 1] - 1)
if (!is.finite(start_range) || !is.finite(end_range)) {
row[i] <- row[i - 1] + 1
} else {
row[i] <- sample(seq(start_range, end_range), 1)
}
}
}
return(row)
}
fl_inits_1 <- apply(fl_inits_temp, 1, replace_nas_increasing_order)
fl_inits_2 <- t(fl_inits_1)
#head(fl_inits_1)
#head(fl_inits_2)
# Function to replace values in Matrix B with NAs based on NAs in Matrix A
replace_values_with_nas <- function(matrix_a, matrix_b) {
# Get the indices where Matrix A has NAs
na_indices <- which(!
is.na(matrix_a), arr.ind = TRUE)
# Replace values in Matrix B with NAs at the corresponding positions
matrix_b[na_indices] <- NA
return(matrix_b)
}
d_fl_inits <- replace_values_with_nas(fl_inits_temp, fl_inits_2)
d_fl_inits <- as.matrix(d_fl_inits)
#head(d_fl_inits)
for (i in 1:N){
if (first[i] == 1) next
if (first[i] > 1) d_fl_inits[i,1:(first[i]-1)] <- NA
}
# Apply the scaling function to each row of the initial forklength
fl_inits <- t(apply(d_fl_inits, 1, logscale_row))
#------------------------------------------------------------------------#
# Initializing alive state
x.init <- matrix(1, N, nyears) # Set to 1 or another appropriate value for initial state
for (i in 1:N) {
if (first[i] > 1) x.init[i, 1:(first[i]-1)] <- NA
}
z_inits <- x.init
#------------------------------------------------------------------------#
# Initialize Breeding
b_d <- b_fl
# Find the indices of the first occurrence of 0 or 1 in each row
first_01_indices <- apply(b_d, 1, function(x) which(!
is.na(x) & x %in% c(0, 1))[1])
# Replace NAs with 0s and 1s, and 0s and 1s with NAs, keeping NAs before the first 0 or 1 unchanged
b_inits_d <- b_d
for (i in 1:nrow(b_d)) {
if (!
is.na(first_01_indices[i])) {
b_inits_d[i, 1:(first_01_indices[i] - 1)] <- b_d[i, 1:(first_01_indices[i] - 1)]
b_inits_d[i, first_01_indices[i]:ncol(b_d)] <- ifelse(
is.na(b_d[i, first_01_indices[i]:ncol(b_d)]), sample(c(0, 1), sum(
is.na(b_d[i, first_01_indices[i]:ncol(b_d)])), replace = TRUE), NA)
} else {
b_inits_d[i, ] <- ifelse(
is.na(b_d[i, ]), sample(c(0, 1), sum(
is.na(b_d[i, ])), replace = TRUE), NA)
}
}
b_inits <- b_inits_d
mu_sp_inits <- runif(nyears)
mu_sl_inits <- runif(nyears)
# Initial values for the model parameters
inits <- list(
#alpha = runif(2, 0, 1), beta = runif(2,0,1),
psi = runif(2, 0, 1),
mu_sp_o = 1, sigma_musp_o = 0.1,
mu_sl_o = 1, sigma_musl_o = 0.1,
sigma = 1, a_sigma = 0.001, b_sigma = 1000,
fl = fl_inits, z = z_inits, b = b_inits,
mu_sp = mu_sp_inits, mu_sl = mu_sl_inits,
support_vectors_phi = support_vectors_phi,
alphas_phi = alphas_phi,
intercept_phi = intercept_phi,
C_phi = C_phi,
support_vectors_p = support_vectors_p,
alphas_p = alphas_p,
intercept_p = intercept_p,
C_p = C_p
)
parameters.to.save <- c("alpha", "beta", "psi")
n.iter <- 400
n.burnin <- 50
n.chains <- 2
mcmc.nim_model <- nimbleModel(code = code_m,
constants = constants,
data = data,
inits = inits)
mcmc.nim_model$calculate()
McMC_m1 <- buildMCMC(mcmc.nim_model, enableWAIC = TRUE)
compile_model_m1<- compileNimble(mcmc.nim_model)