Error: Failed to create the shared library.

37 views
Skip to first unread message

ARJUN BANIK

unread,
May 19, 2024, 7:00:19 AMMay 19
to nimble-users
Hi All,

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.

Here is my code,

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)

_____OUTPUT_____

> mcmc.nim_model <- nimbleModel(code = code_m, + constants = constants, + data = data, + inits = inits) Defining model Building model Setting data and initial values Running calculate on model [Note] Any error reports that follow may simply reflect missing values in model variables. Checking model sizes and dimensions [Note] This model is not fully initialized. This is not an error. To see which variables are not initialized, use model$initializeInfo(). For more information on model initialization, see help(modelInitialization). > mcmc.nim_model$calculate() [1] -1296.19 > > McMC_m1 <- buildMCMC(mcmc.nim_model, enableWAIC = TRUE) ===== Monitors ===== thin = 1: mu_sl, mu_sp, psi, sigma ===== Samplers ===== RW sampler (47) - psi[] (2 elements) - mu_sp[] (6 elements) - mu_sl[] (6 elements) - sigma - fl[] (32 elements) posterior_predictive sampler (2) - mu_sp[] (1 element) - mu_sl[] (1 element) categorical sampler (80) - z[] (80 elements) binary sampler (32) - b[] (32 elements) > compile_model_m1<- compileNimble(mcmc.nim_model) Compiling [Note] This may take a minute. [Note] Use 'showCompilerOutput = TRUE' to see C++ compilation details. Error: Failed to create the shared library. Run 'printErrors()' to see the compilation errors.
simulated_forklength.csv
simulated_capture.csv
simulated_breeding_w_sex_uk.csv

Perry de Valpine

unread,
May 20, 2024, 10:44:33 AMMay 20
to ARJUN BANIK, nimble-users
Dear Arjun,
Thanks for the question and the self-contained reproducible example. This looks interesting, evidently with the use of a support vector machine.
The problem appears to be that krr_predict declares fl to be a vector ("double(1)"), but in the model code the first argument is a scalar. If it should be a scalar, please try declaring it as "double(0)". If it should be a vector, please try passing the first argument in a way such as "fl[i, 1:some_max]" or something like that. I tried the first solution and was able to compile the model, but I am not 100% sure if that is what you want.
I also noted that the "register" (per your code comment) step is not necessary (but also does nothing). The "krr_predict <- nimbleFunction( <etc> )" is all you need. You can give it an internal name via the name argument, but it doesn't really matter and (I think) most people don't. It only really helps if one is trying to read the generated C++. The krr_predict object (returned from nimbleFunction) is what is used when the model sees "krr_predict" in its code.
HTH
Perry
 
 

--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/ea4d329d-c39e-4a48-95f6-16b9199ecbffn%40googlegroups.com.

ARJUN BANIK

unread,
May 22, 2024, 7:05:12 PMMay 22
to Perry de Valpine, nimble-users
Hi Perry,

Thank you, Perry. It is very helpful and I really appreciate that you understood the problem. You are correct, it has complied after declaring fl as "double(0)". I think it makes sense because fl [i,t] is taking one single value at a time, which can be considered a scalar for each individual and for each time point. Also, I appreciate your explanation about the nimble function, it is working fine without the registration step.

Thank you so much Perry.

Thank you,
Arjun


Arjun Banik


"Man is the most insane species. He worships an invisible God and destroys a visible Nature. Unaware that this Nature that he has been destroying is this God he's worshiping."
   - Hubert Reeves


MA21D501 SHIVSHANKAR CHANDRAKANT NILA

unread,
Jun 18, 2024, 2:40:35 PM (11 days ago) Jun 18
to nimble-users
Dear all
I am facing an error message when using the nimble package; I have attached the error message below. How to solve this issue, kindly help if one knows.

Error in { : task 1 failed - "Failed to create the shared library.
g++ -std=gnu++17 -I"/home/ma21d501/R-4.4.0/include" -DNDEBUG -DR_NO_REMAP   -DEIGEN_MPL2_ONLY=1 -I"/home/ma21d501/R-4.4.0/library/nimble/include" -Wno-misleading-indentation -Wno-ignored-attributes -Wno->
g++ -std=gnu++17 -I"/home/ma21d501/R-4.4.0/include" -DNDEBUG -DR_NO_REMAP   -DEIGEN_MPL2_ONLY=1 -I"/home/ma21d501/R-4.4.0/library/nimble/include" -Wno-misleading-indentation -Wno-ignored-attributes -Wno->
using C++ compiler: 'g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0'
In file included from P_4_BivExtMixc_MID_4.h:6,
                 from P_4_BivExtMixc_MID_4.cpp:7:
/home/ma21d501/R-4.4.0/library/nimble/include/nimble/NimArr.h: In instantiation of 'NimArr<nDim, T>& VecNimArr<ndim, T
Calls: %dopar% -> <Anonymous>
Execution halted


Perry de Valpine

unread,
Jun 18, 2024, 3:00:12 PM (11 days ago) Jun 18
to MA21D501 SHIVSHANKAR CHANDRAKANT NILA, nimble-users
Just for thread sanity I am noting this is a duplicate (perhaps due to the moderation we had to put in place to avoid spam problems).

Reply all
Reply to author
Forward
0 new messages