#' Student's t-Test Gui
#'
#' Tool to help build understanding about the Student's t-Test.
#'
#' @return True if it successfully builds.
#' @author Trevor Olsen
#' @export
#'
#' @examples
#' ### t_test_gui()
t_test_gui <- function(){
ld_window <- RGtk2::gtkWindow(show=F)
RGtk2::gtkWindowSetTitle(ld_window,"Student's t-Test Gui")
file_name <- tempfile(pattern = "file", tmpdir = tempdir(), fileext = ".png")
png(file_name)
plot(1, type="n", xlab="X", ylab="Iteration", xlim=c(0, 0), ylim=c(0, 1))
dev.off()
main_box <- RGtk2::gtkVBox()
RGtk2::gtkAdd(ld_window, main_box)
top_image <- RGtk2::gtkImageNewFromFile(file_name)
parameter_table <- RGtk2::gtkTableNew(rows = 10, columns = 10)
generate_button <- RGtk2::gtkButton("Generate Plot")
RGtk2::gtkBoxPackStart(main_box, top_image,T,T, padding = 5)
RGtk2::gtkBoxPackStart(main_box, parameter_table,F,F, padding = 5)
RGtk2::gtkBoxPackStart(main_box, generate_button,F,F, padding = 5)
help_button <- RGtk2::gtkButton()
RGtk2::gtkAdd(help_button, RGtk2::gtkImage(stock="gtk-info",size=2L))
RGtk2::gtkButtonSetRelief(help_button,'GTK_RELIEF_NONE')
RGtk2::gtkTableAttach(parameter_table,help_button,0,1,0,1)
distribution_entry <- RGtk2::gtkEntry()
RGtk2::gtkEntrySetText(distribution_entry, "rnorm(n=100, mean = 0, sd = 1)")
RGtk2::gtkTableAttach(parameter_table,RGtk2::gtkLabel(" x:"),1,2,0,1)
RGtk2::gtkTableAttach(parameter_table,distribution_entry,2,5,0,1)
radiogp_two_sided <- RGtk2::gtkRadioButton( label="two.sided" )
radiogp_greater <- RGtk2::gtkRadioButton( RGtk2::gtkRadioButtonGetGroup(radiogp_two_sided) , label="greater" )
radiogp_less <- RGtk2::gtkRadioButton( RGtk2::gtkRadioButtonGetGroup(radiogp_two_sided) , label="less" )
RGtk2::gtkTableAttach(parameter_table,RGtk2::gtkLabel(" alternative:"),1,2,1,2)
RGtk2::gtkTableAttach(parameter_table,radiogp_two_sided,2,3,1,2)
RGtk2::gtkTableAttach(parameter_table,radiogp_greater,3,4,1,2)
RGtk2::gtkTableAttach(parameter_table,radiogp_less,4,5,1,2)
#mu
#conf.level
mu_entry <- RGtk2::gtkEntry()
RGtk2::gtkTableAttach(parameter_table,RGtk2::gtkLabel(" mu:"),1,2,2,3)
RGtk2::gtkTableAttach(parameter_table,mu_entry,2,5,2,3)
RGtk2::gtkEntrySetText(mu_entry, "0")
conf.level_entry <- RGtk2::gtkEntry()
RGtk2::gtkTableAttach(parameter_table,RGtk2::gtkLabel(" conf.level:"),1,2,3,4)
RGtk2::gtkTableAttach(parameter_table,conf.level_entry,2,5,3,4)
RGtk2::gtkEntrySetText(conf.level_entry, "0.95")
sampling_number_entry <- RGtk2::gtkEntry()
RGtk2::gtkTableAttach(parameter_table,RGtk2::gtkLabel(" Number of samples:"),1,2,4,5)
RGtk2::gtkTableAttach(parameter_table,sampling_number_entry,2,5,4,5)
RGtk2::gtkEntrySetText(sampling_number_entry, "100")
RGtk2::gSignalConnect(help_button, "clicked", function(...) {
rstudioapi::sendToConsole("?t.test", execute = TRUE)
return(T)
})
RGtk2::gSignalConnect(generate_button, "clicked", function(...) {
allocation <- RGtk2::gtkWidgetGetAllocation(top_image)$allocation
width_pic <- max(400,min(allocation$width,allocation$height)-20)
distribution <- RGtk2::gtkEntryGetText(distribution_entry)
mu <- as.numeric(RGtk2::gtkEntryGetText(mu_entry))
conf.level <- RGtk2::gtkEntryGetText(conf.level_entry)
sampling_number <- as.numeric(RGtk2::gtkEntryGetText(sampling_number_entry))
alternative <- ifelse(RGtk2::gtkToggleButtonGetActive(radiogp_two_sided),"two.sided",
ifelse(RGtk2::gtkToggleButtonGetActive(radiogp_greater),"greater","less"))
file_name <- tempfile(pattern = "file", tmpdir = tempdir(), fileext = ".png")
png(file_name,
width = width_pic, height = width_pic)
sampling_code <- RGtk2::gtkEntryGetText(distribution_entry)
sampling_file <- tempfile(pattern = "file", tmpdir = tempdir(), fileext = ".png")
code <- paste0( "output <- matrix(NA,nrow=",sampling_number,", ncol=2)\nfor(i in 1:",sampling_number,"){\n", "x <- ",distribution,
"\nxt <- t.test(x,alternative ='",alternative,"',mu =",mu,",conf.level =",conf.level,
")\noutput[i,1] <- xt$conf.int[1]\noutput[i,2] <- xt$conf.int[2]\n}")
sink(sampling_file)
cat(code)
sink()
source(sampling_file, local = T)
m1 <- min(output[,1])
m2 <- max(output[,2])
r1 <- (m2-m1)*.1
bad_count <- sum(output[,1]>mu | output[,2]<mu)
plot(1, type="n", xlab="X", ylab="Iteration",
xlim=c(max(m1-r1,-10000), min(10000,m2+r1)), ylim=c(0, sampling_number+1),
main="Student's t-Test",
sub=paste0(bad_count," of ",sampling_number," (",round(bad_count/sampling_number,3),") have a type 1 error."))
abline(v = mu)
for(i in 1:sampling_number){
if(output[i,1]<mu & output[i,2]>mu){
lines(x=output[i,], y=c(i,i))
}else{
lines(x=output[i,], y=c(i,i), col="red")
}
}
dev.off()
RGtk2::gtkImageSetFromFile(top_image,file_name)
return(T)
})
RGtk2::gtkButtonClicked(generate_button)
RGtk2::gtkShow(ld_window)
return(T)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.