public
Created

Demonstrating bi-variate correlations using simulation.

  • Download Gist
server.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Script to demonstrate distributions
library(eeptools)
library(shiny)
library(ggplot2)
 
 
rnormcor <- function(x,rho) rnorm(1,rho*x,sqrt(1-rho^2))
 
shinyServer(function(input,output){
output$distPlot<-reactivePlot(function(){
a<-rnorm(input$obs)
b<-sapply(a,rnormcor,rho=input$rho)
p<-qplot(a,b,alpha=0.85)+geom_smooth(method="lm",se=FALSE,size=1.1)+theme_dpi()
p<-p+labs(x="",y="",title="Demonstrating Correlations")
p<-p+geom_text(aes(x=-2.5,y=3,label=paste("Corr. =",input$rho,sep=" ")),size=8)
print(p)
})
})
ui.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# Script to demonstrate distributions
library(eeptools)
library(shiny)
library(ggplot2)
shinyUI(pageWithSidebar(
# Title
headerPanel("Simulating Data with Correlation"),
sidebarPanel(
sliderInput("obs","Number of observations:",
min=200,max=5000,value=500,step=250),
sliderInput("rho","Correlation Coefficient",
min=-1,max=1,value=0,step=0.1)
),
# GGPLOT
mainPanel(
plotOutput("distPlot")
)
))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.