Skip to content

Instantly share code, notes, and snippets.

@jknowles
Created January 8, 2013 15:54
Show Gist options
  • Save jknowles/4484886 to your computer and use it in GitHub Desktop.
Save jknowles/4484886 to your computer and use it in GitHub Desktop.
Show how different moments of a distribution can shift it away from a normal distribution.
# Script to demonstrate distributions
library(VGAM)
library(eeptools)
library(shiny)
library(ggplot2)
shinyServer(function(input,output){
mydat <- reactive(function() {
mydat<-rsnorm(input$obs, location = input$mean, scale =input$variance,
shape = input$skew)
if(input$mode==input$mean){
return(mydat)
}
else if(input$mode!=input$mean){
a<-table(as.vector(round(mydat)))
m<-names(a)[a==max(a)]
v<-max(a)
mydat2<-mydat[round(mydat)!=as.numeric(m)]
samp<-sample(mydat[round(mydat)==as.numeric(m)],v*0.8)
mydat2<-c(mydat2,samp)
fill<-rep(input$mode,(v-length(samp)))
mydat<-c(mydat2,fill)
return(mydat)
}
})
output$distPlot<-reactivePlot(function(){
p <-qplot(mydat(), geom = 'blank') +
geom_line(aes(y = ..density.., colour = 'Empirical'), stat = 'density') +
stat_function(fun = dnorm, aes(colour = 'Normal')) +
geom_histogram(aes(y = ..density..), alpha = 0.4,binwidth=0.2) +
scale_colour_manual(name = 'Density', values = c('red', 'blue'))+
theme_dpi()+theme(legend.position = c(0.85, 0.85))
p<-p+xlim(c(-10,10))+labs(x="data",y="density",title="Distribution of Data")
print(p)
})
})
# Script to demonstrate distributions
library(VGAM)
library(eeptools)
library(shiny)
library(ggplot2)
shinyUI(pageWithSidebar(
# Title
headerPanel("Exploring Propoerties of Distributions"),
sidebarPanel(
sliderInput("obs","Number of tries:",
min=200,max=5000,value=500,step=250),
sliderInput("mean","Mean of the Distribution",
min=-10,max=10,value=0,step=1),
sliderInput("mode","Mode of the Distribution",
min=-10,max=10,value=0,step=1),
sliderInput("variance","Variance of the Distribution",
min=1,max=5,value=1,step=1),
sliderInput("skew","skew of the Distribution",
min=-5,max=5,value=0,step=1)
),
# GGPLOT
mainPanel(
plotOutput("distPlot")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment