Dear Perry,
Indeed the first one would not be good because later I will use more than two slopes.
However I don't know why the second solution is not working (I think I do something wrong but don't know what).
Thank you for your help.
He is my script as it is now (I tested the choose_slope nimbleFunction outside the model and it works) :
choose_slope <- nimbleFunction(
run = function(slopes = double(1), xobs = double(), bp = double()) {
if(xobs < bp) return(slopes[1])
return(slopes[2])
returnType(double())
})
modelCodeSLR <- nimbleCode(
{
intercept ~ dnorm(150, 25) # Average Y at breakpoint
slope_before ~ dnorm(0, 5) # Slope before breakpoint
slope_after ~ dnorm(0, 5) # Slope after breakpoint
bp ~ dnorm(12, 6) # Breakpoint X
error ~ dnorm(0, 20) # Residual error
both_slopes <- c(slope_before,slope_after)
for (obs in 1:Nobs) {
conditional_mean[obs] <- intercept + choose_slope(both_slopes, X[obs], bp) * (X[obs] - bp)
}
for (obs in 1:Nobs) {
Y[obs] ~ dnorm(conditional_mean[obs], error)
}
})
Here is the error:
> Model <- nimbleModel(code = modelCodeSLR, name = 'Nimble', constants = ModelConsts, data = ModelData)
defining model...
building model...
setting data and initial values...
running calculate on model (any error reports that follow may simply reflect missing values in model variables) ... Error in if (xobs < bp) return(slopes[1]) :
valeur manquante là où TRUE / FALSE est requis
checking model sizes and dimensions... 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).
model building finished.
Warning message:
In model$both_slopes[1] <<- nimC(model$slope_before[1], model$slope_after[1]) :
le nombre d'objets à remplacer n'est pas multiple de la taille du remplacement
> CModel <- compileNimble(Model)
compiling... this may take a minute. Use 'showCompilerOutput = TRUE' to see C++ compilation details.
Warning, in eigenizing model_both_slopes[1] the [ is still there but nDim is not 0 (not a scalar).
Erreur : Failed to create the shared library. Run 'printErrors()' to see the compilation errors.