Correlation demonstration

"Andrea Harnos"
18.08.2020

Goal of the shiny app

  • In teaching correlation in class, to show, how the scatterplot of points generated from bivariate normal distribution look like.
  • Number of data points and
  • the value of Pearson correlation can be given.
  • Scatterplot with calculated correlation is shown.

ui.r

library(shiny)
shinyUI(fluidPage(
  titlePanel("Demonstration of correlation"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("numeric", "How Many Random Numbers Should be generated?", 
                   value = 100, min = 1, max = 1000, step = 1),
      sliderInput("corr", "Set correlation",                            -1, 1, value = 0.5, step=0.1),
      h4("The application generates random numbers from bivariate normal distribution (mean = 0, variance = 1). The number of points and  the value of correlation can be adjusted. Scatterplot of the generated random numbers are shown.")
  ),
  mainPanel(
  plotOutput("plot1")))))

The scatterplot

server.R

library(shiny)
library(MASS)
shinyServer(function(input, output) {
renderPlot({
  number_of_points <-input$numeric
  inpcorr <-input$corr
  mu <- rep(0,2)
  Sigma <- rbind(c(1,inpcorr),c(inpcorr,1))
  rawvars <- mvrnorm(n=number_of_points, mu=mu, Sigma=Sigma)
  plot(rawvars[,1],  rawvars[,2], xlab="X",ylab="Y",
       xlim = c(-4, 4), ylim = c(-4, 4), las=1)
  text(-3,3.5,paste("Number of points =",number_of_points))
  text(2,3.5,paste("Correlation given =",inpcorr))
  calc_cor<-round(cor( rawvars[,1], rawvars[,2]),2)
  text(2,3,paste("Calculated correlation =",calc_cor))
})