Bivariate Normal Distribution Plots

Author

Prof. Hazem Al-Mofleh

# Lines 2-69
library(ggplot2)
library(rgl)
library(rlang)
library(plotly)
library(metR)
library(data.table)
library(processx)
library(lattice)
library(shiny)
library(bslib)
library(gridlayout)
NGHS = function(x,theta)
{
  mu1    = theta[1]
  sigma1 = theta[2]
  result = dnorm(x, mean = mu1, sd = sigma1)
  return(result) 
}
DNGHS = function(x,theta)
{
  mu1    = theta[1]
  sigma1 = theta[2]
  result = pnorm(x, mean = mu1, sd = sigma1)
  return(result) 
}
##############################################
################# Bivar-Normal ###############
##############################################
############### Bivariate Normal distribution ###############
FXY = function(x,y,theta1,theta2,rho)
{
  DNGHS(x,theta1)*DNGHS(y,theta2)*(1+rho*(1-DNGHS(x,theta1))*(1-DNGHS(y,theta2)))
}
fXY = function(x,y,theta1,theta2,rho)
{
  NGHS(x,theta1)*NGHS(y,theta2)*(1+rho*(1-2*DNGHS(x,theta1))*(1-2*DNGHS(y,theta2)))
}
xseq   = seq(-2,2,length=100)
yseq   = seq(-2,2,length=100)
theta1 = c(-1,2.75)
theta2 = c( 1,2.75)
rho    = 0
theta = c(theta1,theta2,rho)
pdfmat = cdfmat = matrix(NA,length(xseq),length(yseq))
for (i in 1:length(xseq))
{
  for (j in 1:length(yseq))
  {
    pdfmat[j,i]=fXY(xseq[i],yseq[j],theta1,theta2,rho)
    cdfmat[j,i]=FXY(xseq[i],yseq[j],theta1,theta2,rho)
  }
}

Define User Interface

# Lines 73-282
ui <- page_navbar(
  title = "Bivariate Normal Plots",
  theme = bs_theme(version = 5, bootswatch = "minty"), # Optional styling
  # First Page
  nav_panel(
    title = "PDF Plot by plot_ly",
    layout_sidebar(
      sidebar = sidebar(
        bg = "white",
        accordion(
          accordion_panel(
            "Parameters",
            sliderInput("rho",HTML("<b><i>&rho;</i></b>"), 
                        min = -1    , max = 1 , value = 0, width = "100%", step = 0.05),
            sliderInput("mu1",HTML("<b><i>&mu;<sub>1</sub></i></b>"), 
                        min = -10   , max = 10, value = 0, width = "100%", step = 0.1),
            sliderInput("sigma1"   ,HTML("<b><i>&sigma;<sub>1</sub></i></b>"   ), 
                        min = 0.0001, max = 20, value = 1, width = "100%", step = 0.1),
            sliderInput("mu2",HTML("<b><i>&mu;<sub>2</sub></i></b>"  ), 
                        min = -10   , max = 10, value = 0, width = "100%", step = 0.1),
            sliderInput("sigma2",HTML("<b><i>&sigma;<sub>2</sub></i></b>"), 
                        min = 0.0001, max = 20, value = 1, width = "100%", step = 0.1),
          ),
          accordion_panel(
            "Axes Limits",
            numericInput(inputId = "min_x",label = "Min of x-axis:", value = -4, min = -50,max = 50),
            numericInput(inputId = "max_x",label = "Max of x-axis:", value =  4, min = -50,max = 50),
            numericInput(inputId = "min_y",label = "Min of y-axis:", value = -4, min = -50,max = 50),
            numericInput(inputId = "max_y",label = "Max of y-axis:", value =  4, min = -50,max = 50),
          )
        )
      ),
      plotlyOutput("plot1")
    )
  ),
  # Second Page
  nav_panel(
    title = "CDF Plot by plot_ly",
    layout_sidebar(
      sidebar = sidebar(
        bg = "white",
        accordion(
          accordion_panel(
            "Parameters",
            sliderInput("rho",HTML("<b><i>&rho;</i></b>"), 
                        min = -1    , max = 1 , value = 0, width = "100%", step = 0.05),
            sliderInput("mu1",HTML("<b><i>&mu;<sub>1</sub></i></b>"), 
                        min = -10   , max = 10, value = 0, width = "100%", step = 0.1),
            sliderInput("sigma1"   ,HTML("<b><i>&sigma;<sub>1</sub></i></b>"   ), 
                        min = 0.0001, max = 20, value = 1, width = "100%", step = 0.1),
            sliderInput("mu2",HTML("<b><i>&mu;<sub>2</sub></i></b>"  ), 
                        min = -10   , max = 10, value = 0, width = "100%", step = 0.1),
            sliderInput("sigma2",HTML("<b><i>&sigma;<sub>2</sub></i></b>"), 
                        min = 0.0001, max = 20, value = 1, width = "100%", step = 0.1),
          ),
          accordion_panel(
            "Axes Limits",
            numericInput(inputId = "min_x",label = "Min of x-axis:", value = -4, min = -50,max = 50),
            numericInput(inputId = "max_x",label = "Max of x-axis:", value =  4, min = -50,max = 50),
            numericInput(inputId = "min_y",label = "Min of y-axis:", value = -4, min = -50,max = 50),
            numericInput(inputId = "max_y",label = "Max of y-axis:", value =  4, min = -50,max = 50),
          )
        )
      ),
      card(plotlyOutput("plot2"))
    )
  ),
  # Third Page
  nav_panel(
    title = "PDF Plot",
    layout_sidebar(
      sidebar = sidebar(
        bg = "white",
        accordion(
          accordion_panel(
            "Parameters",
            sliderInput("rho",HTML("<b><i>&rho;</i></b>"), 
                        min = -1    , max = 1 , value = 0, width = "100%", step = 0.05),
            sliderInput("mu1",HTML("<b><i>&mu;<sub>1</sub></i></b>"), 
                        min = -10   , max = 10, value = 0, width = "100%", step = 0.1),
            sliderInput("sigma1"   ,HTML("<b><i>&sigma;<sub>1</sub></i></b>"   ), 
                        min = 0.0001, max = 20, value = 1, width = "100%", step = 0.1),
            sliderInput("mu2",HTML("<b><i>&mu;<sub>2</sub></i></b>"  ), 
                        min = -10   , max = 10, value = 0, width = "100%", step = 0.1),
            sliderInput("sigma2",HTML("<b><i>&sigma;<sub>2</sub></i></b>"), 
                        min = 0.0001, max = 20, value = 1, width = "100%", step = 0.1),
          ),
          accordion_panel(
            "Axes Limits",
            numericInput(inputId = "min_x",label = "Min of x-axis:", value = -4, min = -50,max = 50),
            numericInput(inputId = "max_x",label = "Max of x-axis:", value =  4, min = -50,max = 50),
            numericInput(inputId = "min_y",label = "Min of y-axis:", value = -4, min = -50,max = 50),
            numericInput(inputId = "max_y",label = "Max of y-axis:", value =  4, min = -50,max = 50),
          )
        )
      ),
      card(plotOutput("plot3"))
    )
  ),
  # Forth Page
  nav_panel(
    title = "CDF Plot",
    layout_sidebar(
      sidebar = sidebar(
        bg = "white",
        accordion(
          accordion_panel(
            "Parameters",
            sliderInput("rho",HTML("<b><i>&rho;</i></b>"), 
                        min = -1    , max = 1 , value = 0, width = "100%", step = 0.05),
            sliderInput("mu1",HTML("<b><i>&mu;<sub>1</sub></i></b>"), 
                        min = -10   , max = 10, value = 0, width = "100%", step = 0.1),
            sliderInput("sigma1"   ,HTML("<b><i>&sigma;<sub>1</sub></i></b>"   ), 
                        min = 0.0001, max = 20, value = 1, width = "100%", step = 0.1),
            sliderInput("mu2",HTML("<b><i>&mu;<sub>2</sub></i></b>"  ), 
                        min = -10   , max = 10, value = 0, width = "100%", step = 0.1),
            sliderInput("sigma2",HTML("<b><i>&sigma;<sub>2</sub></i></b>"), 
                        min = 0.0001, max = 20, value = 1, width = "100%", step = 0.1),
          ),
          accordion_panel(
            "Axes Limits",
            numericInput(inputId = "min_x",label = "Min of x-axis:", value = -4, min = -50,max = 50),
            numericInput(inputId = "max_x",label = "Max of x-axis:", value =  4, min = -50,max = 50),
            numericInput(inputId = "min_y",label = "Min of y-axis:", value = -4, min = -50,max = 50),
            numericInput(inputId = "max_y",label = "Max of y-axis:", value =  4, min = -50,max = 50),
          )
        )
      ),
      card(plotOutput("plot4"))
    )
  ),
  # Fifth Page
  nav_panel(
    title = "PDF Plot with Contour",
    layout_sidebar(
      sidebar = sidebar(
        bg = "white",
        accordion(
          accordion_panel(
            "Parameters",
            sliderInput("rho",HTML("<b><i>&rho;</i></b>"), 
                        min = -1    , max = 1 , value = 0, width = "100%", step = 0.05),
            sliderInput("mu1",HTML("<b><i>&mu;<sub>1</sub></i></b>"), 
                        min = -10   , max = 10, value = 0, width = "100%", step = 0.1),
            sliderInput("sigma1"   ,HTML("<b><i>&sigma;<sub>1</sub></i></b>"   ), 
                        min = 0.0001, max = 20, value = 1, width = "100%", step = 0.1),
            sliderInput("mu2",HTML("<b><i>&mu;<sub>2</sub></i></b>"  ), 
                        min = -10   , max = 10, value = 0, width = "100%", step = 0.1),
            sliderInput("sigma2",HTML("<b><i>&sigma;<sub>2</sub></i></b>"), 
                        min = 0.0001, max = 20, value = 1, width = "100%", step = 0.1),
          ),
          accordion_panel(
            "Axes Limits",
            numericInput(inputId = "min_x",label = "Min of x-axis:", value = -4, min = -50,max = 50),
            numericInput(inputId = "max_x",label = "Max of x-axis:", value =  4, min = -50,max = 50),
            numericInput(inputId = "min_y",label = "Min of y-axis:", value = -4, min = -50,max = 50),
            numericInput(inputId = "max_y",label = "Max of y-axis:", value =  4, min = -50,max = 50),
          )
        )
      ),
      card(plotOutput("plot5"))
    )
  ),
  # Sixeth Page (Using a dropdown menu for nested sub-pages)
  nav_panel(
    title = "CDF Plot with Contour",
    layout_sidebar(
      sidebar = sidebar(
        bg = "white",
        accordion(
          accordion_panel(
            "Parameters",
            sliderInput("rho",HTML("<b><i>&rho;</i></b>"), 
                        min = -1    , max = 1 , value = 0, width = "100%", step = 0.05),
            sliderInput("mu1",HTML("<b><i>&mu;<sub>1</sub></i></b>"), 
                        min = -10   , max = 10, value = 0, width = "100%", step = 0.1),
            sliderInput("sigma1"   ,HTML("<b><i>&sigma;<sub>1</sub></i></b>"   ), 
                        min = 0.0001, max = 20, value = 1, width = "100%", step = 0.1),
            sliderInput("mu2",HTML("<b><i>&mu;<sub>2</sub></i></b>"  ), 
                        min = -10   , max = 10, value = 0, width = "100%", step = 0.1),
            sliderInput("sigma2",HTML("<b><i>&sigma;<sub>2</sub></i></b>"), 
                        min = 0.0001, max = 20, value = 1, width = "100%", step = 0.1),
          ),
          accordion_panel(
            "Axes Limits",
            numericInput(inputId = "min_x",label = "Min of x-axis:", value = -4, min = -50,max = 50),
            numericInput(inputId = "max_x",label = "Max of x-axis:", value =  4, min = -50,max = 50),
            numericInput(inputId = "min_y",label = "Min of y-axis:", value = -4, min = -50,max = 50),
            numericInput(inputId = "max_y",label = "Max of y-axis:", value =  4, min = -50,max = 50),
          )
        )
      ),
      card(plotOutput("plot6"))
    )
  )
)

Define Server Logic

# Lines 288-637
server <- function(input, output) {
  output$plot1 <- renderPlotly({
    FXY = function(x,y,theta1,theta2,rho)
    {
      DNGHS(x,theta1)*DNGHS(y,theta2)*(1+rho*(1-DNGHS(x,theta1))*(1-DNGHS(y,theta2)))
    }
    fXY = function(x,y,theta1,theta2,rho)
    {
      NGHS(x,theta1)*NGHS(y,theta2)*(1+rho*(1-2*DNGHS(x,theta1))*(1-2*DNGHS(y,theta2)))
    }
    xseq   = seq(input$min_x,input$max_x,len=200)
    yseq   = seq(input$min_y,input$max_y,len=200)
    theta1 = c(input$mu1,input$sigma1)
    theta2 = c(input$mu2,input$sigma2)
    theta = c(theta1,theta2,input$rho)
    pdfmat = cdfmat = matrix(NA,length(xseq),length(yseq))
    for (i in 1:length(xseq))
    {
      for (j in 1:length(yseq))
      {
        pdfmat[j,i]=fXY(xseq[i],yseq[j],theta1,theta2,input$rho)
        cdfmat[j,i]=FXY(xseq[i],yseq[j],theta1,theta2,input$rho)
      }
    }
    #############################################################
    f1 <- list(
      family = "Arial",
      size   = 17,
      color  = "black")
    axis1 <- list(
      titlefont = list(size = 30),
      tickfont  = f1,color  = "black",
      title = list(text = "<b><i>x<sub>1</sub></i></b>"))
    axis2 <- list(
      titlefont = list(size = 30),
      tickfont = f1,color  = "black",
      title = list(text = "<b><i>x<sub>2</sub></i></b>"))
    axis3 <- list(
      titlefont = list(size = 30),
      tickfont  = f1,color  = "black",
      title = list(text ="<b><i>f<sub>X<sub>1</sub></sub><sub>,X<sub>2</sub></sub>(X<sub>1</sub>, X<sub>2</sub>)</i></b>"))
    scene1 = list(
      xaxis =axis1, 
      yaxis =axis2, 
      zaxis =axis3)
    scene2 = list(
      xaxis =axis1, 
      yaxis =axis2, 
      zaxis =axis4)
    plot_ly(z = ~pdfmat,x=~xseq,y=~yseq,type = "surface",showscale = FALSE,
            contours = list(z = list(show = TRUE,project = list(z = TRUE)))) %>%
      layout(scene=scene1, title=list(font  = list(size = 17),
                                      color = "black",
                                      text  = paste("<i><b>\u0398</b></i> = ( <i>\u03c1<sub>12</sub></i> = ",input$rho,", <i>\u03bc<sub>1</sub></i> = ",input$mu1,", <i>\u03c3<sub>11</sub></i> = ",input$sigma1,", <i>\u03bc<sub>2</sub></i> = ",input$mu2,", <i>\u03c3<sub>22</sub></i> = ",input$sigma2," )<i><sup>T</sup></i>"))
      )
  })
  output$plot2 <- renderPlotly({
    FXY = function(x,y,theta1,theta2,rho)
    {
      DNGHS(x,theta1)*DNGHS(y,theta2)*(1+rho*(1-DNGHS(x,theta1))*(1-DNGHS(y,theta2)))
    }
    fXY = function(x,y,theta1,theta2,rho)
    {
      NGHS(x,theta1)*NGHS(y,theta2)*(1+rho*(1-2*DNGHS(x,theta1))*(1-2*DNGHS(y,theta2)))
    }
    xseq   = seq(input$min_x,input$max_x,len=200)
    yseq   = seq(input$min_y,input$max_y,len=200)
    theta1 = c(input$mu1,input$sigma1)
    theta2 = c(input$mu2,input$sigma2)
    theta = c(theta1,theta2,input$rho)
    pdfmat = cdfmat = matrix(NA,length(xseq),length(yseq))
    for (i in 1:length(xseq))
    {
      for (j in 1:length(yseq))
      {
        pdfmat[j,i]=fXY(xseq[i],yseq[j],theta1,theta2,input$rho)
        cdfmat[j,i]=FXY(xseq[i],yseq[j],theta1,theta2,input$rho)
      }
    }
    ##############################################################
    f1 <- list(
      family = "Arial",
      size   = 17,
      color  = "black")
    axis1 <- list(
      titlefont = list(size = 30),
      tickfont  = f1,color  = "black",
      title = list(text = "<b><i>x<sub>1</sub></i></b>"))
    axis2 <- list(
      titlefont = list(size = 30),
      tickfont = f1,color  = "black",
      title = list(text = "<b><i>x<sub>2</sub></i></b>"))
    axis4 <- list(
      titlefont = list(size = 30),
      tickfont  = f1,color  = "black",
      title = list(text ="<b><i>F<sub>X<sub>1</sub></sub><sub>,X<sub>2</sub></sub>(X<sub>1</sub>, X<sub>2</sub>)</i></b>"))
    scene1 = list(
      xaxis =axis1, 
      yaxis =axis2, 
      zaxis =axis3)
    scene2 = list(
      xaxis =axis1, 
      yaxis =axis2, 
      zaxis =axis4)
    plot_ly(z = ~cdfmat,x=~xseq,y=~yseq,type = "surface",showscale = FALSE, 
            contours = list(z = list(show = TRUE,project = list(z = TRUE)))) %>%
      layout(scene=scene2, title=list(font  = list(size = 17),
                                      color = "black",
                                      text  = paste("<i><b>\u0398</b></i> = ( <i>\u03c1<sub>12</sub></i> = ",input$rho,", <i>\u03bc<sub>1</sub></i> = ",input$mu1,", <i>\u03c3<sub>11</sub></i> = ",input$sigma1,", <i>\u03bc<sub>2</sub></i> = ",input$mu2,", <i>\u03c3<sub>22</sub></i> = ",input$sigma2," )<i><sup>T</sup></i>"))
      )   
  })
  output$plot3 <- renderPlot({
    xseq   = seq(input$min_x,input$max_x,len=100)
    yseq   = seq(input$min_y,input$max_y,len=100)
    theta1 = c(input$mu1,input$sigma1)
    theta2 = c(input$mu2,input$sigma2)
    theta = c(theta1,theta2,input$rho)
    pdfmat = cdfmat = matrix(NA,length(xseq),length(yseq))
    for (i in 1:length(xseq))
    {
      for (j in 1:length(yseq))
      {
        pdfmat[j,i]=fXY(xseq[i],yseq[j],theta1,theta2,input$rho)
        cdfmat[j,i]=FXY(xseq[i],yseq[j],theta1,theta2,input$rho)
      }
    }
    xy <- expand.grid(easting = xseq, northing = yseq)
    data1=as.data.frame(cbind(x=xy[,1],y=xy[,2],z=c(pdfmat)))
    asp <- diff(range(xy$n)) / diff(range(xy$e))
    wireframe(data1$z~data1$x*data1$y,screen = list(z=-55,x=-80),
              shade  = TRUE,aspect = asp,
              scales = list(arrows=FALSE,cex=0.8,rot = 45,tick.number = 10,
                            z = list(arrows=FALSE,cex=0.75,rot =0)),
              panel.aspect = 0.875,
              main = substitute(paste(bolditalic(Theta),' = ( ', rho[1][2], ' = ', bb0, ' , ',
                                      mu[1], ' = ',bb11, ' , ',sigma[1][1], ' = ', bb12,' , ',
                                      mu[2], ' = ',bb21, ' , ',sigma[2][2], ' = ', bb22, ' )',''^T), 
                                list(bb0=input$rho,bb11=input$mu1,bb12=input$sigma1,
                                     bb21=input$mu2,bb22=input$sigma2)),
              xlab = list(label=expression(italic(x[1])), cex=1.30),
              ylab = list(label=expression(italic(x[2])), cex=1.30),
              zlab = list(label=
                            substitute(paste(italic(f[X[1]*", "*X[2]]),'(',italic(x[1]),', ',italic(x[2]),'; ',Theta,')')), cex=1.30,
                          rot = 90),drape = TRUE)
  })
  output$plot4 <- renderPlot({
    xseq   = seq(input$min_x,input$max_x,len=100)
    yseq   = seq(input$min_y,input$max_y,len=100)
    theta1 = c(input$mu1,input$sigma1)
    theta2 = c(input$mu2,input$sigma2)
    theta = c(theta1,theta2,input$rho)
    pdfmat = cdfmat = matrix(NA,length(xseq),length(yseq))
    for (i in 1:length(xseq))
    {
      for (j in 1:length(yseq))
      {
        pdfmat[j,i]=fXY(xseq[i],yseq[j],theta1,theta2,input$rho)
        cdfmat[j,i]=FXY(xseq[i],yseq[j],theta1,theta2,input$rho)
      }
    }
    xy <- expand.grid(easting = xseq, northing = yseq)
    data1=as.data.frame(cbind(x=xy[,1],y=xy[,2],z=c(cdfmat )))
    asp <- diff(range(xy$n)) / diff(range(xy$e))
    wireframe(data1$z~data1$x*data1$y,screen = list(z=-55,x=-80),
              shade  = TRUE,aspect = asp,
              scales = list(arrows=FALSE,cex=0.8,rot = 45,tick.number = 10,
                            z = list(arrows=FALSE,cex=0.75,rot =0)),
              panel.aspect = 0.875,
              main = substitute(paste(bolditalic(Theta),' = ( ', rho[1][2], ' = ', bb0, ' , ',
                                      mu[1], ' = ',bb11, ' , ',sigma[1][1], ' = ', bb12,' , ',
                                      mu[2], ' = ',bb21, ' , ',sigma[2][2], ' = ', bb22, ' )',''^T), 
                                list(bb0=input$rho,bb11=input$mu1,bb12=input$sigma1,
                                     bb21=input$mu2,bb22=input$sigma2)),
              xlab = list(label=expression(italic(x[1])), cex=1.30),
              ylab = list(label=expression(italic(x[2])), cex=1.30),
              zlab = list(label=
                            substitute(paste(italic(F[X[1]*", "*X[2]]),'(',italic(x[1]),', ',italic(x[2]),'; ',Theta,')')), cex=1.30,
                          rot = 90),drape = TRUE)
  })
  output$plot5 <- renderPlot({
    xseq   = seq(input$min_x,input$max_x,len=100)
    yseq   = seq(input$min_y,input$max_y,len=100)
    theta1 = c(input$mu1,input$sigma1)
    theta2 = c(input$mu2,input$sigma2)
    theta = c(theta1,theta2,input$rho)
    pdfmat = cdfmat = matrix(NA,length(xseq),length(yseq))
    for (i in 1:length(xseq))
    {
      for (j in 1:length(yseq))
      {
        pdfmat[j,i]=fXY(xseq[i],yseq[j],theta1,theta2,input$rho)
        cdfmat[j,i]=FXY(xseq[i],yseq[j],theta1,theta2,input$rho)
      }
    }
    xy <- expand.grid(easting = xseq, northing = yseq)
    data1=as.data.frame(cbind(x=xy[,1],y=xy[,2],z=c(pdfmat )))
    asp <- diff(range(xy$n)) / diff(range(xy$e))
    wf <- wireframe(data1$z~data1$x*data1$y,screen = list(z=-55,x=-80),
                    shade  = TRUE,aspect = asp,
                    scales = list(arrows=FALSE,cex=0.8,rot = 45,tick.number = 10,
                                  z = list(arrows=FALSE,cex=0.75,rot =0)),
                    panel.aspect = 0.875,
                    xlab = list(label=expression(italic(x[1])), cex=1.30),
                    ylab = list(label=expression(italic(x[2])), cex=1.30),
                    zlab = list(label=
                                  substitute(paste(italic(f[X[1]*", "*X[2]]),'( ',italic(x[1]),', ',italic(x[2]),' ;  ',rho[1][2], ' = ', bb0, ' , ',
                                                   mu[1], ' = ',bb11, ' , ',sigma[1][1], ' = ', bb12,' , ',
                                                   mu[2], ' = ',bb21, ' , ',sigma[2][2], ' = ', bb22, ' )'), 
                                             list(bb0=input$rho,bb11=input$mu1,bb12=input$sigma1,
                                                  bb21=input$mu2,bb22=input$sigma2)), cex=1.30,
                                rot = 90))
    lv <- levelplot(data1$z ~ data1$x * data1$y,
                    aspect = asp,contour = TRUE,screen = list(z =-40, x = -75),
                    scales = list(x=list(at=seq(-max(xseq),max(xseq),1),cex=1.0),
                                  y=list(at=seq(-max(yseq),max(yseq),1),cex=1.0)),
                    xlab = list(label=expression(italic(x[1])), cex=1.30),
                    ylab = list(label=expression(italic(x[2])), cex=1.30))
    print(lv, split = c(1, 1, 2, 1), more = TRUE)
    print(wf, split = c(2, 1, 2, 1))   
  })
  output$plot6 <- renderPlot({
    xseq   = seq(input$min_x,input$max_x,len=100)
    yseq   = seq(input$min_y,input$max_y,len=100)
    theta1 = c(input$mu1,input$sigma1)
    theta2 = c(input$mu2,input$sigma2)
    theta = c(theta1,theta2,input$rho)
    pdfmat = cdfmat = matrix(NA,length(xseq),length(yseq))
    for (i in 1:length(xseq))
    {
      for (j in 1:length(yseq))
      {
        pdfmat[j,i]=fXY(xseq[i],yseq[j],theta1,theta2,input$rho)
        cdfmat[j,i]=FXY(xseq[i],yseq[j],theta1,theta2,input$rho)
      }
    }
    xy <- expand.grid(easting = xseq, northing = yseq)
    data1=as.data.frame(cbind(x=xy[,1],y=xy[,2],z=c(cdfmat )))
    asp <- diff(range(xy$n)) / diff(range(xy$e))
    wf <- wireframe(data1$z~data1$x*data1$y,screen = list(z=-55,x=-80),
                    shade  = TRUE,aspect = asp,
                    scales = list(arrows=FALSE,cex=0.8,rot = 45,tick.number = 10,
                                  z = list(arrows=FALSE,cex=0.75,rot =0)),
                    panel.aspect = 0.875,
                    xlab = list(label=expression(italic(x[1])), cex=1.30),
                    ylab = list(label=expression(italic(x[2])), cex=1.30),
                    zlab = list(label=
                                  substitute(paste(italic(f[X[1]*", "*X[2]]),'( ',italic(x[1]),', ',italic(x[2]),' ;  ',rho[1][2], ' = ', bb0, ' , ',
                                                   mu[1], ' = ',bb11, ' , ',sigma[1][1], ' = ', bb12,' , ',
                                                   mu[2], ' = ',bb21, ' , ',sigma[2][2], ' = ', bb22, ' )'), 
                                             list(bb0=input$rho,bb11=input$mu1,bb12=input$sigma1,
                                                  bb21=input$mu2,bb22=input$sigma2)), cex=1.30,
                                rot = 90))
    lv <- levelplot(data1$z ~ data1$x * data1$y,
                    aspect = asp,contour = TRUE,screen = list(z =-40, x = -75),
                    scales = list(x=list(at=seq(-max(xseq),max(xseq),1),cex=1.0),
                                  y=list(at=seq(-max(yseq),max(yseq),1),cex=1.0)),
                    xlab = list(label=expression(italic(x[1])), cex=1.30),
                    ylab = list(label=expression(italic(x[2])), cex=1.30))
    print(lv, split = c(1, 1, 2, 1), more = TRUE)
    print(wf, split = c(2, 1, 2, 1))   
  })
}
shinyApp(ui, server)