Last active
December 20, 2015 14:39
-
-
Save dgrapov/6148173 to your computer and use it in GitHub Desktop.
compare skewed to normal distrubution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shinyServer(function(input,output){ | |
mydat <- reactive(function() { | |
scale(rsnorm(input$obs, location = input$mean, scale =input$variance, | |
shape = input$skew),center=TRUE,scale=TRUE) | |
}) | |
ndat <- reactive(function() { | |
scale(rnorm(input$obs, mean = input$mean, sd =input$variance),center=TRUE, scale=TRUE) | |
}) | |
output$distPlot<-reactivePlot(function(){ | |
mdata<-mydat() | |
mdens<-density(mdata) | |
mmean<-mdens$x[which.max(abs(mean(mdens$y)-mdens$y))] | |
mmedian<- mdens$x[which.max(abs(median(mdens$y)-mdens$y))] | |
ndata<-ndat() | |
ndens<-density(ndata) | |
nmean<-ndens$x[which.max(abs(mean(ndens$y)-ndens$y))] | |
nmedian<- ndens$x[which.max(abs(median(ndens$y)-ndens$y))] | |
data2<-data.frame(distribution=rep(c("normal","empirical"),each=(input$obs) ),value=c(ndata,mdata)) | |
p <-ggplot(data=data2, aes(x=value, color=distribution, fill=distribution)) +theme_dpi()+theme(legend.position = c(0.85, 0.85)) +xlim(c(-10,10))+labs(x="data",y="density",title="Distribution of Data") | |
pd<-p+geom_density(aes(y=..density..),alpha=.25 ) + | |
geom_vline(aes(xintercept=mmean), linetype=1, color=rainbow(2)[1], alpha=.5) + | |
geom_vline(aes(xintercept=mmedian),linetype=2,color=rainbow(2)[1], alpha=.5)+ | |
geom_vline(aes(xintercept=nmean), linetype=1, color=rainbow(2)[2], alpha=.5) + | |
geom_vline(aes(xintercept=nmedian),linetype=2,color=rainbow(2)[2], alpha=.5) | |
#qq plot | |
ggQQ <- function(dist,ndist) # 2 distributions | |
{ | |
vals<-qqplot(ndist[,1],dist[,1]) | |
data<-data.frame(x=vals$x, y= vals$y) # get same positions | |
#qqline | |
y <- quantile(ndist[,1], c(0.25, 0.75)) | |
x <- quantile(dist[,1], c(0.25, 0.75)) | |
slope <- diff(y)/diff(x) | |
int <- y[1L] - slope * x[1L] | |
p <- ggplot(data, aes(x=y,y=x)) + | |
geom_point()+ | |
#stat_qq(alpha = 0.5) + | |
geom_abline(slope = slope, intercept = int, color="blue") + theme_dpi() + labs(x="normal quantile",y="actual quantile",title="Quantile-Quantile Plot") | |
return(p) | |
} | |
qq<-ggQQ(data.frame(value=mdata),data.frame(value=ndata)) | |
grid.arrange(pd,qq, nrow=2) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script to demonstrate distributions | |
library(VGAM) | |
library(eeptools) | |
library(shiny) | |
library(ggplot2) | |
shinyUI(pageWithSidebar( | |
# Title | |
headerPanel("Comparing Properties 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",width = 600, height = 900) | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment