@@ -787,6 +787,153 @@ axis(1,at=1:nf,labels=c("A","B","C","D"))
787787```
788788
789789# Section 6.5: Shrinkage beyond the Horseshoe Prior
790+
791+ ## Figure 6.10
792+ We next investigate different shrinkage priors and
793+ plot the marginal prior on a regression coefficient for various choices of the
794+ hyperparameters.
795+
796+ ``` {r, echo = -1, fig.width=9, fig.height=3}
797+ if (pdfplots) {
798+ pdf("6-4_9.pdf", width = 9, height = 3)
799+ }
800+ # Thanks to Peter Knaus for providing the code
801+ # Marginal densities for Triple Gamma, Horseshoe, Double Gamma, and LASSO ######
802+ require(RColorBrewer)
803+ require(gsl)
804+
805+ #par(mar = c(4, 3.5, .1, .1), mgp = c(1.6, .6, 0))
806+
807+ # Some functions that return the log-marginal densities
808+ ## Log-marginal and marginal for Triple Gamma
809+ logmarginal_TG <- function(x, a, c, kappa2){
810+ -0.5*log(4*pi/kappa2) - lbeta(a, c) + 0.5*(log(a) - log(c)) + lgamma(c + 0.5) +
811+ log(hyperg_U(c + 0.5, -a + 1.5 ,kappa2*a*x^2/(4*c), give = FALSE, strict = TRUE))
812+ }
813+
814+ ## Log-marginal and marginal for Double Gamma
815+ logmarginal_DG <- function(x, a, kappa2){
816+ 0.5*(a + 0.5)*log(a*kappa2) - 0.5*log(pi) - (a-0.5)*log(2) - lgamma(a) + (a - 0.5)*log(abs(x)) +
817+ log(besselK(sqrt(a*kappa2)*abs(x), a-0.5))
818+ }
819+
820+ # Select color palette
821+ color <- brewer.pal(5, "Dark2")
822+
823+ # Create layout matrix for plots
824+ m <- matrix(c(1, 2, 3, 3), nrow = 2, ncol = 2,byrow = TRUE)
825+ layout(mat = m, heights = c(0.9, 0.1))
826+
827+ # Plots around the origin
828+ # Draw horseshoe marginal
829+ curve((logmarginal_TG(x, 0.5, 0.5, 2)),
830+ from=c(-1, to = 1), n = 10000, col = color[1], lwd = 2, ylab = "",
831+ xlab = "", cex = 3, ylim = c(-4,4), main = "")
832+
833+ # Draw double gamma marginal
834+ curve((logmarginal_DG(x, 0.1, 2)),
835+ n = 10000, col = color[2], lwd = 2, add = TRUE)
836+
837+ # Draw LASSO marginal
838+ curve((logmarginal_DG(x, 1, 2)),
839+ n = 10000, col = color[3], lwd = 2, add = TRUE)
840+
841+ # Draw triple gamma marginal
842+ curve((logmarginal_TG(x, 0.1, 0.1, 2)),
843+ n = 10000, col = color[4], lwd = 2, add = TRUE)
844+
845+ # Add labels to x and y axes
846+ title(ylab = (expression(log~p(beta))), line = 2, cex.lab = 1.2)
847+ title(xlab = expression(beta), line = 3, cex.lab = 1.2)
848+
849+ # Plots in the tails
850+ # Draw horseshoe marginal
851+ curve((logmarginal_TG(x, 0.5, 0.5, 2)),
852+ from = 6, to = 11, n = 10000, col = color[1], lwd = 2, ylab = "",
853+ xlab = "", cex = 3, ylim = c(-18,-5), main = "")
854+
855+ # Draw double gamma marginal
856+ curve((logmarginal_DG(x, 0.1, 2)),
857+ n = 10000, col = color[2], lwd = 2, add = TRUE)
858+
859+ # Draw LASSO marginal
860+ curve((logmarginal_DG(x, 1, 2)),
861+ n = 10000, col = color[3], lwd = 2, add = TRUE)
862+
863+ # Draw triple gamma marginal
864+ curve((logmarginal_TG(x, 0.1, 0.1, 2)),
865+ n = 10000, col = color[4], lwd = 2, add = TRUE)
866+
867+ # Add labels to x and y axes
868+ title(ylab=(expression(log~p(beta))), line=2, cex.lab=1.2)
869+ title(xlab=expression(beta), line=3, cex.lab=1.2)
870+
871+ # Create legend at bottom of plot
872+ par(mar = c(0,0,0,0))
873+
874+ plot(1, type = "n", axes = FALSE)
875+ legend(x = "top", inset = 0,
876+ legend = c("Normal Gamma Gamma", "Horseshoe", "Normal Gamma", "Lasso"),
877+ col = color[c(4, 1, 2, 3)],
878+ lwd = 2, cex = 1, horiz = TRUE, xjust=0.5)
879+
880+ ```
881+
882+ The shrinkage profiles of these priors are visualized in the following Figure.
883+ ``` {r, echo = -1, fig.width=9, fig.height=3}
884+ if (pdfplots) {
885+ pdf("6-4_10.pdf", width = 9, height = 3)
886+ }
887+ # Thanks again to Peter Knaus for providing the code
888+
889+ par(mfrow=c(1,1))
890+
891+ # Some functions that return shrinkage profiles
892+ # Shrinkage profile for triple gamma
893+ dTPB<-function(x,a,c, phi){
894+ res <- lgamma(a + c) -lgamma(a) -lgamma(c) + c*log(phi) + (c-1)*log(x) + (a-1)*log(1-x) - (a+c)*log(1 + (phi -1)*x)
895+ exp(res)
896+ }
897+
898+ # Shrinkage profile for double gamma
899+ dTPB_DG<-function(x,a, k2){
900+ res <- -lgamma(a) + a*log(a*k2/2) + (a-1)*log(1-x) - (a+1)*log(x) - ((1-x)/x*a*k2/2)
901+ exp(res)
902+ }
903+
904+ # Set up plotting area
905+ par(mar = c(4, 4, 0, 12.5) )
906+
907+ # Choose color palette
908+ color <- RColorBrewer::brewer.pal(5, "Dark2")
909+
910+ # Draw shrinkage profile of horsehoe
911+ curve((dTPB(x, 0.5, 0.5, 1)),
912+ from = 0, to = 1, n = 1000, col = color[1], lwd = 2, ylab = "",
913+ xlab = "", cex = 3, ylim = c(0,4), main = "")
914+
915+ # Draw shrinkage profile of double gamma
916+ curve((dTPB_DG(x, 0.1, 2)),
917+ n = 1000, col = color[2], lwd = 2, add = TRUE)
918+
919+ # Draw shrinkage profile of lasso
920+ curve((dTPB_DG(x, 1, 2)), n = 1000, col = color[3], lwd = 2, add = TRUE)
921+
922+ # Draw shrinkage profile of triple gamma
923+ curve((dTPB(x, 0.1, 0.1, 1)),
924+ n = 1000, col = color[4], lwd = 2, add = TRUE)
925+
926+ # Add labels to x and y axes
927+ title(ylab=expression(p(kappa[j])), line=2, cex.lab=1.2)
928+ title(xlab=expression(kappa[j]), line=2, cex.lab=1.2)
929+
930+ par(xpd = TRUE)
931+ legend(x = 1.05, y=3,
932+ legend = c("Normal Gamma Gamma", "Horseshoe", "Normal Gamma", "Lasso"),
933+ col = color[c(4,1,2,3)],bty="n",
934+ lwd = 2, cex = 1, horiz = FALSE)
935+
936+ ```
790937## Example 6.10
791938``` {r, echo = -1, fig.width=9, fig.height=3}
792939if (pdfplots) {
0 commit comments