Hi everyone,
I am trying to use the generalized normal distribution (https://en.wikipedia.org/wiki/Generalized_normal_distribution) in a Greta model. As this distribution is not available, I have tried to follow the only example I have met so far, which is here:https://mdscheuerell.github.io/gretaDFA/[https://mdscheuerell.github.io/gretaDFA/].
The probelm I have is that when I run the Greta model I have the following error:
Error in self$tf_distrib(parameters, dag)$log_prob(x) :
object ‘tf’ cannot be found
Any idea on how to change my code? Here is the code I wrote:
library (R6)
distrib <- .internals$nodes$constructors$distrib
distribution_node <- .internals$nodes$node_classes$distribution_node
check_dims <- .internals$utils$checks$check_dims
as.greta_array <- .internals$greta_arrays$as.greta_array
is_scalar <- .internals$utils$misc$is_scalar
fl <- .internals$utils$misc$fl
function to call
gnorm <- function (mu, sigma, beta, dim = 1) {
distrib(“gnorm”, mu, sigma, beta, dim)
}
gnorm_distribution <- R6Class (
“gnorm_distribution”,
inherit = distribution_node,
public = list(
initialize = function (mu, sigma, beta, dim) {
# (dim)
if (length(dim) > 1 ||
dim <= 0 ||
!is.finite(dim) ||
dim != floor(dim)) {
stop ("dim must be a scalar positive integer, but was: ",
capture.output(dput(dim)),
call. = FALSE)
}
# check if sigma is scalar
# (sigma)
if (length(sigma) > 1) {
stop ("sigma must be a scalar positive integer, but was: ",
capture.output(dput(sigma)),
call. = FALSE)
}
# check if mu is scalar
# (mu)
if (length(mu) > 1) {
stop ("mu must be a scalar positive integer, but was: ",
capture.output(dput(mu)),
call. = FALSE)
}
# check if beta is scalar
# (beta)
if (length(beta) > 1) {
stop ("beta must be a scalar positive integer, but was: ",
capture.output(dput(beta)),
call. = FALSE)
}
sigma <- as.greta_array(sigma)
mu <- as.greta_array(mu)
beta <- as.greta_array(beta)
self$bounds <- c(0, Inf)
super$initialize("gnorm", dim, truncation = c(0, Inf))
self$add_parameter(sigma, "sigma")
self$add_parameter(mu, "mu")
self$add_parameter(beta, "beta")
},
tf_distrib = function (parameters, dag) {
mu <- parameters$mu
beta <- parameters$beta
sigma <- parameters$sigma
# log pdf(x | i, M, sigma)
log_prob = function (x) {
tf$log(beta) - tf$log(fl(2)*sigma)-tf$lgamma(1/beta) - tf$pow(tf$abs((x-mu)/sigma), (beta))
}
list(log_prob = log_prob, cdf = NULL, log_cdf = NULL)
},
# no CDF for discrete distributions
tf_cdf_function = NULL,
tf_log_cdf_function = NULL
)
)
Sincerely,
Frédéric Gosselin