-
-
Save mtaylor-semo/1d55b911d94f9f3232f89d67cf5df91f to your computer and use it in GitHub Desktop.
t-test: Shiny app at http://www.statistics.calpoly.edu/shiny
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
t-test Shiny App | |
Base R code created by Jimmy Wong | |
Shiny app files created by Jimmy Wong | |
Cal Poly Statistics Dept Shiny Series | |
http://statistics.calpoly.edu/shiny |
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
Title: t-test | |
Author: Jimmy Wong | |
AuthorUrl: https://www.linkedin.com/in/jimmywong46/ | |
License: MIT | |
DisplayMode: Normal | |
Tags: t-test | |
Type: Shiny |
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
The MIT License (MIT) | |
Copyright (c) 2015 Jimmy Wong | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. |
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
# ----------------------- | |
# App Title: t-test | |
# Author: Jimmy Wong | |
# ----------------------- | |
##################################################################################################################### | |
##################################################################################################################### | |
## t-distribution shading area function | |
##################################################################################################################### | |
##################################################################################################################### | |
t.dist.area = function(tstat,tail,df) | |
{ | |
x = seq(-5,5,length.out=200) | |
df = round(df, digits=3) | |
if(tail=="right") | |
{ | |
xmin=tstat | |
xmax=5 | |
area = seq(xmin,xmax,length.out=200) | |
dat = data.frame(x=area,ymin=0,ymax=dt(area,df=df)) | |
graph = ggplot() + geom_line(data.frame(x=x, y=dt(x,df=df)), mapping=aes(x=x, y=y)) + | |
geom_ribbon(data=dat, mapping=aes(x=x, ymin=ymin, ymax=ymax), fill="navy") + | |
ggtitle(paste("t-distribution with", df, "degrees of freedom")) + | |
xlab("t-values") + ylab("Relative frequency") + theme_bw() | |
} else if(tail=="left") | |
{ | |
xmin=-5 | |
xmax=tstat | |
area = seq(xmin,xmax,length.out=200) | |
dat = data.frame(x=area,ymin=0,ymax=dt(area,df=df)) | |
graph = ggplot() + geom_line(data.frame(x=x, y=dt(x,df=df)), mapping=aes(x=x, y=y)) + | |
geom_ribbon(data=dat, mapping=aes(x=x, ymin=ymin, ymax=ymax), fill="navy") + | |
ggtitle(paste("t-distribution with", df, "degrees of freedom")) + | |
xlab("t-values") + ylab("Relative frequency") + theme_bw() | |
} else if(tail=="both") | |
{ | |
xmin1=abs(tstat) | |
xmax1=5 | |
area1 = seq(xmin1,xmax1,length.out=200) | |
dat1 = data.frame(x=area1,ymin1=0,ymax1=dt(area1,df=df)) | |
xmin2=-5 | |
xmax2=-abs(tstat) | |
area2 = seq(xmin2,xmax2,length.out=200) | |
dat2 = data.frame(x=area2,ymin2=0,ymax2=dt(area2,df=df)) | |
graph = ggplot() + geom_line(data.frame(x=x, y=dt(x,df=df)), mapping=aes(x=x, y=y)) + | |
geom_ribbon(data=dat1, mapping=aes(x=x, ymin=ymin1, ymax=ymax1),fill="navy") + | |
geom_ribbon(data=dat2, mapping=aes(x=x, ymin=ymin2, ymax=ymax2),fill="navy") + | |
ggtitle(paste("t-distribution with", df, "degrees of freedom")) + | |
xlab("t-values") + ylab("Relative frequency") + theme_bw() | |
} | |
return(graph) | |
} | |
##################################################################################################################### | |
##################################################################################################################### | |
## Library and data sets | |
##################################################################################################################### | |
##################################################################################################################### | |
options(shiny.maxRequestSize=30*1024^2) | |
library(ggplot2) | |
library(shinyBS) | |
data(faithful) | |
data(mtcars) | |
##################################################################################################################### | |
##################################################################################################################### | |
## Shiny server | |
##################################################################################################################### | |
##################################################################################################################### | |
shinyServer(function(input, output) { | |
##################################################################################################################### | |
##################################################################################################################### | |
## Data Exploration Panel | |
##################################################################################################################### | |
##################################################################################################################### | |
data = reactive({ | |
if(is.null(input$file) & !input$usedata) | |
{ | |
return(NULL) | |
} else if(!is.null(input$file) & !input$usedata) | |
{ | |
file = read.csv(input$file$datapath, header=input$header, sep=input$sep, quote=input$quote) | |
return(file) | |
} else if(input$sampdat==1 & input$usedata) | |
{ | |
return(data.frame(eruptions=faithful$eruptions)) | |
} else if(input$sampdat==2 & input$usedata) | |
{ | |
mtcars$amcoded = rep(NA,length(mtcars$hp)) | |
mtcars$amcoded[which(mtcars$am==0)] = "automatic" | |
mtcars$amcoded[which(mtcars$am==1)] = "manual" | |
return(data.frame(transmission=mtcars$amcoded, horsepower=mtcars$hp)) | |
} | |
}) | |
output$data.tab = renderDataTable({ | |
if(!input$usedata) data() | |
}) | |
output$data.tab1 = renderDataTable({ | |
if(input$usedata) data() | |
}) | |
output$datagraph = renderPlot({ | |
if((input$datformat==1 & !input$usedata) | (input$sampdat!=2 & input$usedata)) | |
{ | |
dat=unlist(data()) | |
dat1=data.frame(x=as.numeric(as.character(dat))) | |
if(input$usedata) | |
lab = "Eruption times" | |
else | |
lab = paste(names(data())[1]) | |
ggplot(data=dat1) + geom_histogram(aes(x=x), fill="navy", alpha=.5) + | |
xlab(lab) + ylab("Frequency") + | |
ggtitle(paste("Histogram of",lab)) + theme_bw() | |
} else if((input$datformat==2 & !input$usedata) | (input$sampdat!=1 & input$usedata)) | |
{ | |
dat=data() | |
dat1=data.frame(x=dat[[1]],y=dat[[2]]) | |
if(length(unique(dat1$x))>length(unique(dat1$y))) | |
{ | |
dat1$x = as.numeric(as.character(dat1$x)) | |
ggplot(data=dat1) + geom_boxplot(aes(x=factor(y),y=x,fill=factor(y)),alpha=.5) + | |
xlab(paste(names(dat)[2])) + ylab(paste(names(dat)[1])) + theme_bw() + | |
ggtitle(paste("Boxplots of",paste(names(dat)[1]),"by",paste(names(dat)[2]))) + | |
scale_fill_manual(name=paste(names(dat)[1]),values=c("seagreen2","gold2")) + | |
theme(legend.position="bottom") | |
} else | |
{ | |
dat1$y = as.numeric(as.character(dat1$y)) | |
ggplot(data=dat1) + geom_boxplot(aes(x=factor(x),y=y,fill=factor(x)),alpha=.5) + | |
xlab(paste(names(dat)[1])) + ylab(paste(names(dat)[2])) + theme_bw() + | |
ggtitle(paste("Boxplots of",paste(names(dat)[2]),"by",paste(names(dat)[1]))) + | |
scale_fill_manual(name=paste(names(dat)[2]),values=c("seagreen2","gold2")) + | |
theme(legend.position="bottom") | |
} | |
} else if((input$datformat==3 & !input$usedata) | (input$sampdat!=1 & input$usedata)) | |
{ | |
dat=data() | |
dat1=data.frame(x=c(as.numeric(as.character(dat[[1]])),as.numeric(as.character(dat[[2]]))), | |
y=c(rep(names(dat)[1],length(dat[[1]])),rep(names(dat)[2],length(dat[[2]])))) | |
ggplot(data=dat1) + geom_boxplot(aes(x=factor(y),y=x,fill=factor(y)),alpha=.5) + | |
xlab("Explanatory variable") + ylab("Response variable") + | |
scale_fill_manual(name="",values=c("seagreen2","gold2")) + | |
ggtitle("Boxplots") + theme_bw() + theme(legend.position="bottom") | |
} | |
}) | |
output$summarystats = renderTable({ | |
if(input$displaystats & ((input$datformat==1 & !input$usedata) | (input$sampdat!=2 & input$usedata))) | |
{ | |
vec = as.numeric(as.character(data()[[1]])) | |
table = t(matrix(c((as.matrix(summary(vec)[1:6])), | |
round(sd(vec,na.rm=TRUE))))) | |
if(input$usedata) | |
rownames(table) = "Eruption times" | |
else | |
rownames(table) = names(data())[[1]] | |
colnames(table) = c("Min","Q1","Median","Mean","Q3","Max","SD") | |
return(table) | |
} else if(input$displaystats & ((input$datformat==2 & !input$usedata) | (input$sampdat!=1 & input$usedata))) | |
{ | |
dat=data() | |
dat1=data.frame(x=dat[[1]],y=dat[[2]]) | |
if(length(unique(dat1$x)) > length(unique(dat1$y))) | |
{ | |
dat1$y = factor(dat1$y) | |
dat1$x = as.numeric(as.character(dat1$x)) | |
dat1 = dat1[which(complete.cases(dat1)),] | |
sum = tapply(dat1$x,dat1$y,summary) | |
table = data.frame(matrix(c(sum[[1]],sum[[2]]),nrow=2,ncol=6,byrow=TRUE)) | |
std = tapply(dat1$x,dat1$y,sd,na.rm=TRUE) | |
table$sd[1] = round(std[1],digits=2) | |
table$sd[2] = round(std[2],digits=2) | |
table = as.matrix(table) | |
colnames(table) = c("Min","Q1","Median","Mean","Q3","Max","SD") | |
rownames(table) = c(levels(dat1$y)[1],levels(dat1$y)[2]) | |
return(table) | |
} else if(length(unique(dat1$x)) < length(unique(dat1$y))) | |
{ | |
dat1$x = factor(dat1$x) | |
dat1$y = as.numeric(as.character(dat1$y)) | |
dat1 = dat1[which(complete.cases(dat1)),] | |
sum = tapply(dat1$y,dat1$x,summary) | |
table = data.frame(matrix(c(sum[[1]],sum[[2]]),nrow=2,ncol=6,byrow=TRUE)) | |
std = tapply(dat1$y,dat1$x,sd) | |
table$sd[1] = round(std[1],digits=2) | |
table$sd[2] = round(std[2],digits=2) | |
table = as.matrix(table) | |
colnames(table) = c("Min","Q1","Median","Mean","Q3","Max","SD") | |
rownames(table) = c(levels(dat1$x)[1],levels(dat1$x)[2]) | |
return(table) | |
} | |
} else if(input$displaystats & ((input$datformat==3 & !input$usedata) | (input$sampdat!=1 & input$usedata))) | |
{ | |
dat = data() | |
dat[,1] = as.numeric(as.character(dat[,1])) | |
dat[,2] = as.numeric(as.character(dat[,2])) | |
table = data.frame(t(as.matrix(apply(dat,2,summary)[-7,]))) | |
table$sd[1] = round(sd(dat[,1],na.rm=TRUE),digits=2) | |
table$sd[2] = round(sd(dat[,2],na.rm=TRUE),digits=2) | |
table = as.matrix(table) | |
colnames(table) = c("Min","Q1","Median","Mean","Q3","Max","SD") | |
return(table) | |
} | |
}) | |
##################################################################################################################### | |
##################################################################################################################### | |
## T-test Panel | |
##################################################################################################################### | |
##################################################################################################################### | |
output$info = renderUI({ | |
HTML(as.character(code("Click here for hypothesis test information."))) | |
}) | |
output$onesample = renderUI({ | |
HTML(as.character(code("Click here for one-sample t-test information."))) | |
}) | |
output$twosample = renderUI({ | |
HTML(as.character(code("Click here for two-sample t-test information."))) | |
}) | |
output$hypo1 = renderUI({ | |
if((input$datformat==1 & !input$usedata) | (input$sampdat!=2 & input$usedata)) | |
{ | |
if(input$alt1=="less than") | |
HTML("Ho: μ =", input$null1,"<p> Ha: μ <",input$null1) | |
else if(input$alt1=="greater than") | |
HTML("Ho: μ =", input$null1,"<p> Ha: μ >",input$null1) | |
else | |
HTML("Ho: μ =", input$null1,"<p> Ha: μ ≠",input$null1) | |
} | |
}) | |
output$hypo2 = renderUI({ | |
if((input$datformat!=1 & !input$usedata) | (input$sampdat!=1 & input$usedata)) | |
{ | |
if(input$alt2=="less than") | |
HTML("Ho: μ<sub>1</sub>-μ<sub>2</sub> =",input$null2, | |
"<p> Ha: μ<sub>1</sub>-μ<sub>2</sub> <",input$null2) | |
else if(input$alt2=="greater than") | |
HTML("Ho: μ<sub>1</sub>-μ<sub>2</sub> =",input$null2, | |
"<p> Ha: μ<sub>1</sub>-μ<sub>2</sub> >",input$null2) | |
else | |
HTML("Ho: μ<sub>1</sub>-μ<sub>2</sub> =",input$null2, | |
"<p> Ha: μ<sub>1</sub>-μ<sub>2</sub> ≠",input$null2) | |
} | |
}) | |
mod = reactive({ | |
input$teststart | |
isolate({ | |
if(input$teststart>0) | |
{ | |
if((input$datformat==1 & !input$usedata) | (input$sampdat!=2 & input$usedata)) | |
{ | |
if(input$alt1=="less than") | |
mod = t.test(x=as.numeric(as.character(unlist(data()))),alternative="less",mu=input$null1,conf.level=1-input$alpha) | |
else if(input$alt1=="greater than") | |
mod = t.test(x=as.numeric(as.character(unlist(data()))),alternative="greater",mu=input$null1,conf.level=1-input$alpha) | |
else | |
mod = t.test(x=as.numeric(as.character(unlist(data()))),alternative="two.sided",mu=input$null1,conf.level=1-input$alpha) | |
} else if((input$datformat==2 & !input$usedata) | (input$sampdat!=1 & input$usedata)) | |
{ | |
dat=data() | |
if(length(unique(dat[[1]])) > length(unique(dat[[2]]))) | |
{ | |
if(input$alt2=="less than") | |
mod = t.test(as.numeric(as.character(dat[[1]]))~dat[[2]], | |
alternative="less",mu=input$null2,conf.level=1-input$alpha) | |
else if(input$alt2=="greater than") | |
mod = t.test(as.numeric(as.character(dat[[1]]))~dat[[2]], | |
alternative="greater",mu=input$null2,conf.level=1-input$alpha) | |
else | |
mod = t.test(as.numeric(as.character(dat[[1]]))~dat[[2]], | |
alternative="two.sided",mu=input$null2,conf.level=1-input$alpha) | |
} else | |
{ | |
if(input$alt2=="less than") | |
mod = t.test(as.numeric(as.character(dat[[2]]))~dat[[1]], | |
alternative="less",mu=input$null2,conf.level=1-input$alpha) | |
else if(input$alt2=="greater than") | |
mod = t.test(as.numeric(as.character(dat[[2]]))~dat[[1]], | |
alternative="greater",mu=input$null2,conf.level=1-input$alpha) | |
else | |
mod = t.test(as.numeric(as.character(dat[[2]]))~dat[[1]], | |
alternative="two.sided",mu=input$null2,conf.level=1-input$alpha) | |
} | |
} else if((input$datformat==3 & !input$usedata) | (input$sampdat!=1 & input$usedata)) | |
{ | |
dat=data() | |
if(input$alt2=="less than") | |
mod = t.test(x=as.numeric(as.character(dat[[1]])),y=as.numeric(as.character(dat[[2]])), | |
alternative="less",mu=input$null2,conf.level=1-input$alpha) | |
else if(input$alt2=="greater than") | |
mod = t.test(x=as.numeric(as.character(dat[[1]])),y=as.numeric(as.character(dat[[2]])), | |
alternative="greater",mu=input$null2,conf.level=1-input$alpha) | |
else | |
mod = t.test(x=as.numeric(as.character(dat[[1]])),y=as.numeric(as.character(dat[[2]])), | |
alternative="two.sided",mu=input$null2,conf.level=1-input$alpha) | |
} | |
} | |
}) | |
}) | |
output$est=renderUI({ | |
if(input$teststart>0 & input$showpoint & ((input$datformat==1 & !input$usedata) | (input$sampdat!=2 & input$usedata))) | |
{ | |
HTML("x̅ =",round(mod()$estimate[1],2)) | |
} else if(input$teststart>0 & input$showpoint & ((input$datformat!=1 & !input$usedata) | (input$sampdat!=1 & input$usedata))) | |
{ | |
HTML("x̅<sub>1</sub> =",round(mod()$estimate[1],2),"<p> x̅<sub>2</sub> =",round(mod()$estimate[2],2), | |
"<p> x̅<sub>1</sub> - x̅<sub>2</sub> =",round(mod()$estimate[1]-mod()$estimate[2],2)) | |
} | |
}) | |
output$test = renderTable({ | |
input$teststart | |
isolate({ | |
if(input$teststart>0) | |
{ | |
tab = matrix(c(mod()$parameter,mod()$statistic,mod()$p.value),nrow=1) | |
colnames(tab) = c("df","t-statistic","p-value") | |
rownames(tab) = "Values" | |
tab | |
} | |
}) | |
}) | |
output$tdist = renderPlot({ | |
input$teststart | |
isolate({ | |
if(input$alt1=="less than" | input$alt2=="less than") | |
{ | |
tail="left" | |
} else if(input$alt1=="greater than" | input$alt2=="greater than") | |
{ | |
tail="right" | |
} else if(input$alt1=="two-sided" | input$alt2=="two-sided") | |
{ | |
tail="both" | |
} | |
return(t.dist.area(mod()$statistic,tail=tail,mod()$parameter)) | |
}) | |
}) | |
output$citab = renderTable({ | |
if(input$ci & input$teststart>0) | |
{ | |
tab = matrix(c(mod()$conf.int[1],mod()$conf.int[2]),nrow=1) | |
colnames(tab) = c("Lower bound","Upper bound") | |
rownames(tab) = paste(round(1-input$alpha, digits=3)*100,"% CI",sep="") | |
tab | |
} | |
}) | |
##################################################################################################################### | |
##################################################################################################################### | |
## Diagnostics Panel | |
##################################################################################################################### | |
##################################################################################################################### | |
output$qqplot = renderPlot({ | |
if((input$datformat==1 & !input$usedata) | (input$sampdat==1 & input$usedata)) | |
{ | |
dat=unlist(data()) | |
dat1=data.frame(x=as.numeric(as.character(dat))) | |
ggplot(data=dat1, aes(sample=x)) + stat_qq(geom="point",color="navy",shape=1) + | |
theme_bw() + theme(text=element_text(size=15)) + ggtitle("Q-Q Plot") | |
} else if((input$datformat==2 & !input$usedata) | (input$sampdat!=1 & input$usedata)) | |
{ | |
dat=data() | |
dat1=data.frame(x=dat[[1]],y=dat[[2]]) | |
if(length(unique(dat[[1]])) > length(unique(dat[[2]]))) | |
{ | |
dat1$x=as.numeric(as.character(dat1$x)) | |
ggplot(data=dat1, aes(sample=x)) + stat_qq(aes(color=factor(y)),geom="point",shape=1) + theme_bw() + | |
theme(text=element_text(size=15)) + ggtitle("Q-Q Plot") + facet_wrap(~y) + | |
scale_color_manual(name="", values=c("navy","gold2")) + guides(color=FALSE) | |
} else | |
{ | |
dat1$y=as.numeric(as.character(dat1$y)) | |
ggplot(data=dat1, aes(sample=y)) + stat_qq(aes(color=factor(x)),geom="point",shape=1) + theme_bw() + | |
theme(text=element_text(size=15)) + ggtitle("Q-Q Plot") + facet_wrap(~x) + | |
scale_color_manual(name="", values=c("navy","gold2")) + guides(color=FALSE) | |
} | |
} else if((input$datformat==3 & !input$usedata) | (input$sampdat!=1 & input$usedata)) | |
{ | |
dat=data() | |
dat1=data.frame(x=c(as.numeric(as.character(dat[[1]])),as.numeric(as.character(dat[[2]]))), | |
y=c(rep(names(dat)[1],length(dat[[1]])),rep(names(dat)[2],length(dat[[2]])))) | |
ggplot(data=dat1, aes(sample=x)) + stat_qq(aes(color=factor(y)),geom="point",shape=1) + theme_bw() + | |
theme(text=element_text(size=15)) + ggtitle("Q-Q plot") + facet_wrap(~y) + | |
scale_color_manual(name="", values=c("navy","gold2")) + guides(color=FALSE) | |
} | |
}) | |
output$sw = renderTable({ | |
if((input$datformat==1 & !input$usedata) | (input$sampdat!=2 & input$usedata)) | |
{ | |
dat=unlist(data()) | |
dat1=data.frame(x=as.numeric(as.character(dat))) | |
validate( | |
need(try(shapiro.test(dat1$x)), "Do not need to conduct normality test") | |
) | |
norm = shapiro.test(dat1$x) | |
tab = matrix(c(norm$statistic,norm$p.value),nrow=1) | |
colnames(tab) = c("W statistic","p-value") | |
rownames(tab) = "Data" | |
tab | |
} else if((input$datformat==2 & !input$usedata) | (input$sampdat!=1 & input$usedata)) | |
{ | |
dat=data() | |
dat1=data.frame(x=dat[[1]],y=dat[[2]]) | |
if(length(unique(dat[[1]])) > length(unique(dat[[2]]))) | |
{ | |
dat1$x=as.numeric(as.character(dat1$x)) | |
norm1 = shapiro.test(dat1$x[which(dat1$y==unique(dat1$y)[1])]) | |
norm2 = shapiro.test(dat1$x[which(dat1$y==unique(dat1$y)[2])]) | |
tab = matrix(c(norm1$statistic,norm2$statistic,norm1$p.value,norm2$p.value),ncol=2) | |
colnames(tab) = c("W statistic","p-value") | |
rownames(tab) = c("Data 1","Data 2") | |
tab | |
} else | |
{ | |
dat1$y=as.numeric(as.character(dat1$y)) | |
norm1 = shapiro.test(dat1$y[which(dat1$x==unique(dat1$x)[1])]) | |
norm2 = shapiro.test(dat1$y[which(dat1$x==unique(dat1$x)[2])]) | |
tab = matrix(c(norm1$statistic,norm2$statistic,norm1$p.value,norm2$p.value),ncol=2) | |
colnames(tab) = c("W statistic","p-value") | |
rownames(tab) = c("Data 1","Data 2") | |
tab | |
} | |
} else if((input$datformat==3 & !input$usedata) | (input$sampdat!=1 & input$usedata)) | |
{ | |
dat=data() | |
norm1 = shapiro.test(as.numeric(as.character(dat[[1]]))) | |
norm2 = shapiro.test(as.numeric(as.character(dat[[2]]))) | |
tab = matrix(c(norm1$statistic,norm2$statistic,norm1$p.value,norm2$p.value),ncol=2) | |
colnames(tab) = c("W statistic","p-value") | |
rownames(tab) = c("Data 1","Data 2") | |
tab | |
} | |
}) | |
}) |
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
# ----------------------- | |
# App Title: t-test | |
# Author: Jimmy Wong | |
# ----------------------- | |
if (!require("ggplot2")) | |
install.packages("ggplot2") | |
if (!require("shinyBS")) | |
install.packages("shinyBS") | |
shinyUI(fluidPage( | |
tags$head(tags$link(rel = "icon", type = "image/x-icon", | |
href = "https://webresource.its.calpoly.edu/cpwebtemplate/5.0.1/common/images_html/favicon.ico")), | |
navbarPage(title="t-test", | |
tabPanel("Information of t-test", | |
column(1), | |
column(5,br(),br(),br(), | |
withMathJax(p("This application allows users to perform either a", code("one-sample t-test",style="color:navy"), | |
"or a", code("two-sample t-test",style="color:navy"),". A one-sample t-test focuses on comparing the average of a | |
single quantitative variable to a hypothesized value, while a two-sample t-test | |
focuses on comparing the difference in averages of a quantitative variable between two groups to a hypothesized value. In | |
both scenarios, the purpose of the hypothesis test is to determine how likely are the observed results or any more extreme results, | |
under the assumption that the null hypothesis is true. This is known as a", strong("p-value.")), | |
p("In most data analyses, the population mean(s) along with the population standard deviation(s) are unknown. | |
Therefore, the", strong("t-test"), "is used instead of a z-test. The", strong("t-statistic"), "can be calculated to determine the p-value, | |
by comparing it to the", strong("t-distribution"), "with a", strong("specified degrees of freedom."), "In this scenario, the sample standard deviation(s) replaces the population standard deviation(s) to yield | |
the", strong("standard error"), "(an estimate of the true standard deviation) of the", strong("sampling distribution.")))), | |
column(5,br(),br(),br(), | |
wellPanel( | |
code("One-sample t-test:",style="color:navy"), | |
p(HTML("<ul> <li type=square> the parameter of interest is the population mean, μ<li type=square>"),p(), | |
p("t-statistic = \\(\\frac{\\bar x -\\mu_0}{s_{x}/\\sqrt{n}}\\)"), HTML("</ul>")),br(), | |
code("Two-sample t-test:",style="color:navy"), | |
p(HTML("<ul> <li type=square> the parameter of interest is the difference between the two population means, μ<sub>1</sub>-μ<sub>2</sub> <li type=square>"),p(), | |
p("t-statistic = \\(\\frac{(\\bar x_1 - \\bar x_2) -(\\mu_1-\\mu_2)}{\\sqrt{\\frac{s_{1}^2}{n_1} + \\frac{s_{2}^2}{n_2}}}\\)"), HTML("</ul>")))), | |
column(1)), | |
tabPanel("Data Exploration", | |
tabsetPanel( | |
tabPanel("Sample Data", | |
fluidRow( | |
column(3, | |
wellPanel( | |
selectInput("sampdat", "Choose a data set:", choices=list("One-sample"=1,"Two-sample"=2), selected=1), | |
bsPopover(id="sampdat", title="Data set information", content="The one-sample data set is collected from the Old Faithful geyser in Yellowstone National Park, Wyoming, USA. The variable of interest is the duration of each eruption in minutes. The two-sample data set is associated with the 1974 Motor Trend US magazine. The explanatory variable is the type of transmission and the response variable is horsepower.", | |
trigger="hover",placement="right"), | |
checkboxInput("usedata", "Use sample data", TRUE), | |
bsTooltip("usedata","Remember to uncheck this when not using sample data!","right"), | |
br(),br(),br(), | |
div("Shiny app by", | |
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"), | |
div("Base R code by", | |
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"), | |
div("Shiny source files:", | |
a(href="https://gist.github.com/calpolystat/48dc47f3ff436aba4b19", | |
target="_blank","GitHub Gist"),align="right", style = "font-size: 8pt"), | |
div(a(href="http://www.statistics.calpoly.edu/shiny",target="_blank", | |
"Cal Poly Statistics Dept Shiny Series"),align="right", style = "font-size: 8pt"))), | |
column(9, | |
conditionalPanel( | |
condition="input.usedata", | |
dataTableOutput("data.tab1"))))), | |
tabPanel("Upload Data", | |
fluidRow( | |
column(3, | |
wellPanel( | |
fileInput("file", "",accept=c("text/csv", "text/comma-separated-values,text/plain", ".csv")), | |
bsPopover("file","Note", "Remember to select the correct data format after uploading file! Hover over the Select data format panel for more information.", | |
trigger="hover",placement="right"), | |
tags$hr(), | |
radioButtons("datformat", strong("Select data format:"), choices=c("1-sample"=1,Stacked=2,Unstacked=3), selected=1), | |
bsPopover("datformat","Data format", "Select Stacked for 2-sample with explanatory and response variables in two columns. Select Unstacked with explanatory variable as column names and response variable in two columns", | |
trigger="hover",placement="right"), | |
tags$hr(), | |
strong("Customize file format:"), | |
checkboxInput("header", "Header", TRUE), | |
radioButtons("sep", "Separator:", choices=c(Comma=",",Semicolon=";",Tab="\t"), selected=","), | |
radioButtons("quote", "Quote", choices=c(None="","Double Quote"='"',"Single Quote"="'"),selected=""), | |
br(),br(),br(), | |
div("Shiny app by", | |
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"), | |
div("Base R code by", | |
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"), | |
div("Shiny source files:", | |
a(href="https://gist.github.com/calpolystat/48dc47f3ff436aba4b19", | |
target="_blank","GitHub Gist"),align="right", style = "font-size: 8pt"), | |
div(a(href="http://www.statistics.calpoly.edu/shiny",target="_blank", | |
"Cal Poly Statistics Dept Shiny Series"),align="right", style = "font-size: 8pt"))), | |
column(9, | |
conditionalPanel( | |
condition="input.file!='NULL'", | |
dataTableOutput("data.tab"))))), | |
tabPanel("Visualize Data", | |
fluidRow( | |
column(6, | |
plotOutput("datagraph")), | |
column(6,br(),br(), | |
checkboxInput("displaystats", "Summary statistics", FALSE), | |
tableOutput("summarystats")))))), | |
tabPanel("Hypothesis Test", | |
fluidRow( | |
column(3, | |
wellPanel( | |
conditionalPanel( | |
condition="input.datformat==1 && input.sampdat==1", | |
h4("Hypotheses:"), | |
uiOutput("hypo1"), | |
tags$hr(), | |
numericInput("null1", label="Hypothesized value:", value=0), | |
selectInput("alt1", "Select a direction for Ha:", choices=list("two-sided","less than","greater than"),selected="two-sided")), | |
conditionalPanel( | |
condition="input.datformat!=1 || input.sampdat==2", | |
h4("Hypotheses:"), | |
uiOutput("hypo2"), | |
tags$hr(), | |
numericInput("null2", label="Hypothesized value:", value=0), | |
selectInput("alt2", label="Select a direction for Ha:", choices=list("two-sided","less than","greater than"),selected="two-sided")), | |
sliderInput("alpha", label=HTML("Significance level α:"), value=.05, max=1, min=0, step=.01), | |
tags$hr(), | |
actionButton("teststart", strong("Perform t-test")), | |
bsPopover("teststart","Note","Remember to check the normality condition before performing t-test.",trigger="hover",placement="right"), | |
br(),br(),br(), | |
div("Shiny app by", | |
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"), | |
div("Base R code by", | |
a(href="http://www.linkedin.com/in/jimmywong46/",target="_blank","Jimmy Wong"),align="right", style = "font-size: 8pt"), | |
div("Shiny source files:", | |
a(href="https://gist.github.com/calpolystat/48dc47f3ff436aba4b19", | |
target="_blank","GitHub Gist"),align="right", style = "font-size: 8pt"), | |
div(a(href="http://www.statistics.calpoly.edu/shiny",target="_blank", | |
"Cal Poly Statistics Dept Shiny Series"),align="right", style = "font-size: 8pt"))), | |
column(9, | |
br(),br(), | |
conditionalPanel( | |
condition="input.teststart>0", | |
column(7, | |
plotOutput("tdist"), | |
bsPopover("tdist","p-value","The p-value is the shaded region. A large p-value indicates to fail to reject Ho and no evidence for Ha. A small p-value indicates to reject the Ho and evidence for Ha.", | |
trigger="hover",placement="left"),br()), | |
column(5,br(), | |
strong("Test output:"), | |
tableOutput("test"),br(), | |
checkboxInput("showpoint","Point estimate(s):",FALSE), | |
uiOutput("est"), | |
checkboxInput("ci","Confidence interval:", FALSE), | |
tableOutput("citab"), | |
bsPopover("citab","Confidence interval sample interpretation","We are 95% confident that the true parameter is anywhere between the lower bound to the upper bound.", | |
trigger="hover",placement="bottom")))))), | |
tabPanel("Normality Condition", | |
column(1), | |
column(4,br(), | |
p(strong("Normality test:"),"The sampling distribution of the sample means or differences in the sample means is 1) Normal if the population(s) is Normal or 2) approximately Normal if the | |
sample size(s) is large enough (at least 30). In situations with small sample size(s), the approach to access | |
whether the sample data could have came from a Normal distribution is either through a Q-Q plot or a Normality test."), | |
actionButton("normstart", label=strong("Perform normality test")),br(),br(), | |
conditionalPanel( | |
condition="input.normstart>0", | |
wellPanel( | |
strong("Ho: data is from a Normal distribution"),br(), | |
strong("Ha: data is not from a Normal distribution"),br(),br(), | |
em("Shapiro-Wilk Normality Test:"), | |
tableOutput("sw"), | |
bsPopover("sw","Normality test","In a normality test, a large p-value does not provide evidence that the sample data is not from a Normal distribution.", | |
trigger="hover",placement="bottom")))), | |
column(6, | |
conditionalPanel( | |
condition="input.normstart>0",br(),br(), | |
plotOutput("qqplot"), | |
bsPopover("qqplot","Q-Q plot","In a Q-Q plot, points that resemble a diagonal line with no curvature implies that the sample data could have came from a Normal distribution.", | |
trigger="hover",placement="left"))), | |
column(1)) | |
))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment