diff --git a/README.md b/README.md index 8af3428931f363c9be448296cefc6d6057f58f70..b2fe1fd2b1266adc149d80c824c28211c35db20d 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,50 @@ Explorando Interfaces Gráficas com o R ====================================== Eduardo E. Ribeiro Jr, PET-Estatística UFPR -Prof. Walmes M. Zeviani, LEG UFPR +[Prof. Walmes M. Zeviani](http://www.leg.ufpr.br/~walmes/), LEG UFPR + +O R é um programa para computação estatística e gráficos ... TODO + +Nesse Curso consideramos pacotes do R para produzir ... TODO + +O pacote [`animation`] serve para gerar animações em diversos formatos, +como *gif* e animações em *javascript* dentro de páginas web. O [`rgl`] +permite explorar o espaço tridimensional .. O pacote [`googleVis`] faz +gráficos da galeria Google Charts a partir de objetos do R. Com os +pacotes [`rpanel`] e [`gWdigets`] é possível fazer interfaces gráficas e +até mini aplicativos baseados em Tcl/Tk ou Gtk+. Por fim, o pacote +[`shiny`] possibilita construir aplicações Web com o R baseadas em +*javascript*. + +O que todos esses pacotes têm em comum é permitir uma visualização +dinâmica e/ou interativas. Com eles podemos produzir gráficos que exibem +uma sequência de estados - como os gifs - com os quais podemos +rotacionar - rgl - TODO + +Tais recursos interativos são usados para ensino de estatística e +exibição de dados ... TODO + +Os arquivos `Rmd` precisam ser compilados com `markdown::render()` ou +`markdown::run()`. + +As aplicações Shiny estão ativas no servidor RStudio/Shiny do LEG/PET: +<http://shiny.leg.ufpr.br/iguir/>. **** ## Organização do repositório +TODO + **** ## Como citar esse material + +TODO + +<!------------------------------------------------------------------ --> + +[`animation`]: http://yihui.name/animation/ +[`rgl`]: http://rgl.neoscientists.org/about.shtml +[`googleVis`]: https://cran.r-project.org/web/packages/googleVis/index.html +[`rpanel`]: https://cran.r-project.org/web/packages/rpanel/index.html +[`gWdigets`]: https://cran.r-project.org/web/packages/gWidgets/index.html +[`shiny`]: http://shiny.rstudio.com/ diff --git a/gWidgets/hist_Button.gif b/gWidgets/hist_Button.gif deleted file mode 100644 index 88b03248430f781e8649b5838f9c5d02d7a77813..0000000000000000000000000000000000000000 Binary files a/gWidgets/hist_Button.gif and /dev/null differ diff --git a/gWidgets/hist_Checkbox.gif b/gWidgets/hist_Checkbox.gif deleted file mode 100644 index 201e00be028d8ec8a24464bda6f3712bf97908c4..0000000000000000000000000000000000000000 Binary files a/gWidgets/hist_Checkbox.gif and /dev/null differ diff --git a/gWidgets/hist_Checkboxgroup.gif b/gWidgets/hist_Checkboxgroup.gif deleted file mode 100644 index cd483ea80773614d3c392ab1b5066cdb09d5fbfb..0000000000000000000000000000000000000000 Binary files a/gWidgets/hist_Checkboxgroup.gif and /dev/null differ diff --git a/gWidgets/hist_Numeric.gif b/gWidgets/hist_Numeric.gif deleted file mode 100644 index 5c067205e75a6fd3d96f54ce5681f18c638e7f35..0000000000000000000000000000000000000000 Binary files a/gWidgets/hist_Numeric.gif and /dev/null differ diff --git a/gWidgets/hist_Radio.gif b/gWidgets/hist_Radio.gif deleted file mode 100644 index 0a78b3e601088c8a301dda0a280e68096f5217ce..0000000000000000000000000000000000000000 Binary files a/gWidgets/hist_Radio.gif and /dev/null differ diff --git a/gWidgets/hist_Select.gif b/gWidgets/hist_Select.gif deleted file mode 100644 index bf2e22ca611442a76fe53e2dc37f23e12128cefd..0000000000000000000000000000000000000000 Binary files a/gWidgets/hist_Select.gif and /dev/null differ diff --git a/gWidgets/hist_Slider.gif b/gWidgets/hist_Slider.gif deleted file mode 100644 index d49873a8d6908d78f101146498184ed0505d6202..0000000000000000000000000000000000000000 Binary files a/gWidgets/hist_Slider.gif and /dev/null differ diff --git a/gWidgets/hist_Text.gif b/gWidgets/hist_Text.gif deleted file mode 100644 index 3f27cbc811ba59055caf3e42b81e6f5ee958755e..0000000000000000000000000000000000000000 Binary files a/gWidgets/hist_Text.gif and /dev/null differ diff --git a/rpanel/hist_button.gif b/rpanel/hist_button.gif deleted file mode 100644 index e4036099cb3b36cf691ec983b24ba6e45cf5aa28..0000000000000000000000000000000000000000 Binary files a/rpanel/hist_button.gif and /dev/null differ diff --git a/rpanel/hist_checkbox.gif b/rpanel/hist_checkbox.gif deleted file mode 100644 index ba98b98cd8781fe9d3480472e8c01d628052851f..0000000000000000000000000000000000000000 Binary files a/rpanel/hist_checkbox.gif and /dev/null differ diff --git a/rpanel/hist_checkboxgroup.gif b/rpanel/hist_checkboxgroup.gif deleted file mode 100644 index b393512494530ede568b632368c8942d53d8a6af..0000000000000000000000000000000000000000 Binary files a/rpanel/hist_checkboxgroup.gif and /dev/null differ diff --git a/rpanel/hist_numeric.gif b/rpanel/hist_numeric.gif deleted file mode 100644 index ea558f25350d526873642f958bbcbc56d8774c67..0000000000000000000000000000000000000000 Binary files a/rpanel/hist_numeric.gif and /dev/null differ diff --git a/rpanel/hist_radio.gif b/rpanel/hist_radio.gif deleted file mode 100644 index 93467479a0d6817348d577f6d4dd0126b19f40f7..0000000000000000000000000000000000000000 Binary files a/rpanel/hist_radio.gif and /dev/null differ diff --git a/rpanel/hist_select.gif b/rpanel/hist_select.gif deleted file mode 100644 index 5a4f841af273a1612252d00df5431c129f8865f1..0000000000000000000000000000000000000000 Binary files a/rpanel/hist_select.gif and /dev/null differ diff --git a/rpanel/hist_select2.gif b/rpanel/hist_select2.gif deleted file mode 100644 index 9cfce5b4a60b5ed9437a399a6f5d96e1e1a9a777..0000000000000000000000000000000000000000 Binary files a/rpanel/hist_select2.gif and /dev/null differ diff --git a/rpanel/hist_slider.gif b/rpanel/hist_slider.gif deleted file mode 100644 index 97b5081dd47cdf8a059172120abd4c69ae65bc45..0000000000000000000000000000000000000000 Binary files a/rpanel/hist_slider.gif and /dev/null differ diff --git a/rpanel/hist_text.gif b/rpanel/hist_text.gif deleted file mode 100644 index 7a0f85fff9fe9e99a9f90f2a335cddca87f2e95c..0000000000000000000000000000000000000000 Binary files a/rpanel/hist_text.gif and /dev/null differ diff --git a/shiny/distProb/server.R b/shiny/distProb/server.R index 27e24c2d348d0e0b240be493bc2a97f3e5f65955..b85889ecbd629d9b6ab83da792f9a73eaef415e8 100644 --- a/shiny/distProb/server.R +++ b/shiny/distProb/server.R @@ -1,7 +1,27 @@ -##------------------------------------------- -## server.R - require(shiny) + +densityView <- function(m, s, sample){ + curve(dnorm(x, mean=m, sd=s), + from=m-4*s, to=m+4*s) + abline(v=m, lty=1, col=2) + lines(density(sample), col=2) +} + +ecdfView <- function(m, s, sample){ + curve(pnorm(x, mean=m, sd=s), + from=m-4*s, to=m+4*s) + abline(v=m, lty=1, col=2) + lines(ecdf(sample), col=2) +} + +qqView <- function(sample){ + qqnorm(sample) + qqline(sample) +} + +## Quantidade de amostras de y. +N <- 500 + ## Carrega template das aplicações elaboradas pelo projeto iguiR2 source("../template.R") @@ -12,20 +32,48 @@ shinyServer( template("TEMA") }) output$ui <- renderUI({ - if(is.null(input$dist)){ - return()} + if (is.null(input$dist)){ + return(NULL) + } switch(input$dist, + "poisson"={ output$plot <- renderPlot({ x <- 0:30 px <- dpois(x, lambda=input$poissonLambda) plot(x, px, type="h", xlab="x", ylab="Pr(x)") }) + SAMPLER <- reactive({ + popMean <- input$poissonLambda + popSd <- sqrt(input$poissonLambda) + meanSd <- popSd/sqrt(input$n) + sampleMean <- + replicate( + N, + mean(rpois( + input$n, + lambda=input$poissonLambda))) + return(list( + m=popMean, + s=meanSd, + sample=sampleMean)) + }) + output$density <- renderPlot({ + with(SAMPLER(), + densityView(m=m, s=s, sample=sample)) + }) + output$ecdf <- renderPlot({ + with(SAMPLER(), + ecdfView(m=m, s=s, sample=sample)) + }) + output$qqnorm <- renderPlot({ + qqView(sample=SAMPLER()$sample) + }) wellPanel( sliderInput(inputId="poissonLambda", label="Média da Poisson", min=0.1, max=20, value=10) - ) + ) }, "binomial"={ @@ -33,7 +81,37 @@ shinyServer( x <- 0:input$binomialSize px <- dbinom(x, size=input$binomialSize, prob=input$binomialProb) - plot(x, px, type="h", xlab="x", ylab="Pr(x)") + plot(x, px, type="h", + xlab="x", ylab="Pr(x)") + }) + SAMPLER <- reactive({ + popMean <- + input$binomialSize*input$binomialProb + popSd <- + sqrt(popMean*(1-input$binomialProb)) + meanSd <- popSd/sqrt(input$n) + sampleMean <- + replicate( + N, + mean(rbinom( + input$n, + size=input$binomialSize, + prob=input$binomialProb))) + return(list( + m=popMean, + s=meanSd, + sample=sampleMean)) + }) + output$density <- renderPlot({ + with(SAMPLER(), + densityView(m=m, s=s, sample=sample)) + }) + output$ecdf <- renderPlot({ + with(SAMPLER(), + ecdfView(m=m, s=s, sample=sample)) + }) + output$qqnorm <- renderPlot({ + qqView(sample=SAMPLER()$sample) }) wellPanel( sliderInput(inputId="binomialSize", @@ -43,10 +121,9 @@ shinyServer( label="Probabilidade de sucesso", min=0.02, max=0.98, value=0.5, step=0.02) - ) }, - + "beta"={ output$plot <- renderPlot({ curve(dbeta(x, @@ -55,31 +132,99 @@ shinyServer( from=0, to=1, xlab="x", ylab="f(x)") }) + SAMPLER <- reactive({ + popMean <- + input$betaShape1/ + (input$betaShape1+input$betaShape2) + popSd <- sqrt( + input$betaShape1*input$betaShape2/( + (input$betaShape1+ + input$betaShape2+1)* + (input$betaShape1+ + input$betaShape2)**2 + ) + ) + meanSd <- popSd/sqrt(input$n) + sampleMean <- + replicate( + N, + mean(rbeta( + input$n, + shape1=input$betaShape1, + shape2=input$betaShape2))) + return(list( + m=popMean, + s=meanSd, + sample=sampleMean)) + }) + output$density <- renderPlot({ + with(SAMPLER(), + densityView(m=m, s=s, sample=sample)) + }) + output$ecdf <- renderPlot({ + with(SAMPLER(), + ecdfView(m=m, s=s, sample=sample)) + }) + output$qqnorm <- renderPlot({ + qqView(sample=SAMPLER()$sample) + }) wellPanel( sliderInput(inputId="betaShape1", label="Parâmetro de forma 1", - min=0.01, max=7, value=1, step=0.1), + min=0.01, max=7, value=1, + step=0.1), sliderInput(inputId="betaShape2", label="Parâmetro de forma 2", - min=0.01, max=7, value=1, step=0.1) + min=0.01, max=7, value=1, + step=0.1) ) }, "gamma"={ output$plot <- renderPlot({ curve(dgamma(x, - shape=input$gammaShape, - rate=input$gammaRate), + shape=input$gammaShape, + rate=input$gammaRate), from=0, to=20, xlab="x", ylab="f(x)") }) + SAMPLER <- reactive({ + popMean <- input$gammaShape/input$gammaRate + popSd <- sqrt( + input$gammaShape/(input$gammaRate**2)) + meanSd <- popSd/sqrt(input$n) + sampleMean <- + replicate( + N, + mean(rgamma( + input$n, + shape=input$gammaShape, + rate=input$gammaRate))) + return(list( + m=popMean, + s=meanSd, + sample=sampleMean)) + }) + output$density <- renderPlot({ + with(SAMPLER(), + densityView(m=m, s=s, sample=sample)) + }) + output$ecdf <- renderPlot({ + with(SAMPLER(), + ecdfView(m=m, s=s, sample=sample)) + }) + output$qqnorm <- renderPlot({ + qqView(sample=SAMPLER()$sample) + }) wellPanel( sliderInput(inputId="gammaShape", label="Parâmetro de forma", - min=0.01, max=7, value=1, step=0.1), + min=0.01, max=7, value=1, + step=0.1), sliderInput(inputId="gammaRate", label="Parâmetro de taxa", - min=0.01, max=7, value=1, step=0.1) + min=0.01, max=7, value=1, + step=0.1) ) }, @@ -91,15 +236,83 @@ shinyServer( from=-3, to=3, xlab="x", ylab="f(x)") }) + SAMPLER <- reactive({ + popMean <- input$normalMean + popSd <- input$normalSd + meanSd <- popSd/sqrt(input$n) + sampleMean <- + replicate( + N, + mean(rnorm( + input$n, + mean=input$normalMean, + sd=input$normalSd))) + return(list( + m=popMean, + s=meanSd, + sample=sampleMean)) + }) + output$density <- renderPlot({ + with(SAMPLER(), + densityView(m=m, s=s, sample=sample)) + }) + output$ecdf <- renderPlot({ + with(SAMPLER(), + ecdfView(m=m, s=s, sample=sample)) + }) + output$qqnorm <- renderPlot({ + qqView(sample=SAMPLER()$sample) + }) wellPanel( sliderInput(inputId="normalMean", label="Média da normal", - min=-3, max=3, value=0, step=0.05), + min=-3, max=3, value=0, + step=0.05), sliderInput(inputId="normalSd", label="Desvio-padrão da normal", - min=0.1, max=3, value=1, step=0.05) - ) + min=0.1, max=3, value=1, + step=0.05) + ) } - ) + + ) ## switch() + }) ## renderUI + + ## Opções para média amostral + output$OptsMedia <- renderUI({ + if (input$tab == "Distribuição") { + return(NULL) + } else { + wellPanel( + numericInput( + inputId="n", + label="Tamanho da amostra:", + min = 2, max = 1000, value=10), + HTML("<label>Tipo de gráfico: </label>"), + tabsetPanel( + id = "graf", + type = "pills", + tabPanel("Density"), + tabPanel("ECDF"), + tabPanel("Q-QNorm") + ), + helpText("Baseados em 500 amostras") + ) + } + }) + + ## Gráficos para a distribuição média amostral + output$grafsMedia <- renderUI({ + if (is.null(input$graf)) { + return(NULL) + } else { + switch( + input$graf, + "Density" = plotOutput("density"), + "ECDF" = plotOutput("ecdf"), + "Q-QNorm" = plotOutput("qqnorm") + ) + } }) - }) + + }) ## shinyServer() diff --git a/shiny/distProb/ui.R b/shiny/distProb/ui.R index fcdabb6f3141404cb478be7975e1bd38bb88cc40..da960cd29405b64c1def9bb10dd212978d641cc5 100644 --- a/shiny/distProb/ui.R +++ b/shiny/distProb/ui.R @@ -1,6 +1,3 @@ -##------------------------------------------- -## ui.R - require(shiny) choi <- c("Poisson"="poisson", @@ -19,10 +16,19 @@ shinyUI( selectInput(inputId="dist", label="Distribuição", choices=choi), - uiOutput("ui") + uiOutput("ui"), + + uiOutput("OptsMedia") ), mainPanel( - plotOutput("plot") + tabsetPanel( + id = "tab", + tabPanel("Distribuição", + plotOutput("plot")), + tabPanel("Distribuição Amostral da Média", + uiOutput("grafsMedia") + ) + ) ) ) ) \ No newline at end of file