if(require("plotly")) {
x_ <- seq(0, 2, length.out = 100L)
alpha_ <- seq(0.5, 2.5, length.out = 100L)
dsty <- vapply(alpha_, function(alpha) {
InverseGamma$new(alpha, beta = 1)$d(x_)
}, numeric(length(x_)))
#
txt <- matrix(NA_character_, nrow = length(x_), ncol = length(alpha_))
for(i in 1L:nrow(txt)) {
for(j in 1L:ncol(txt)) {
txt[i, j] <- paste0(
"x: ", formatC(x_[i]),
" alpha: ", formatC(alpha_[j]),
" density: ", formatC(dsty[i, j])
)
}
}
#
plot_ly(
x = ~alpha_, y = ~x_, z = ~dsty, type = "surface",
text = txt, hoverinfo = "text", showscale = FALSE
) %>% layout(
title = "Inverse Gamma distribution",
margin = list(t = 40, r= 5, b = 5, l = 5),
scene = list(
xaxis = list(
title = "alpha"
),
yaxis = list(
title = "x"
),
zaxis = list(
title = "density"
)
)
)
}
Run the code above in your browser using DataLab