Skip to content
Snippets Groups Projects
Commit c860628c authored by Eduardo E. R. Junior's avatar Eduardo E. R. Junior
Browse files

Merge branch 'issue#15' into 'master'

Issue#15

Esse ramo partiu do ramo #14. O conteúdo desse ramo está descrito no #15.

See merge request !14
parents c4eff2c2 7e495bd3
No related branches found
No related tags found
No related merge requests found
Showing
with 285 additions and 26 deletions
......@@ -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/
gWidgets/hist_Button.gif

91.5 KiB

gWidgets/hist_Checkbox.gif

53 KiB

gWidgets/hist_Checkboxgroup.gif

98.3 KiB

gWidgets/hist_Numeric.gif

295 KiB

gWidgets/hist_Radio.gif

106 KiB

gWidgets/hist_Select.gif

223 KiB

gWidgets/hist_Slider.gif

617 KiB

gWidgets/hist_Text.gif

57.1 KiB

rpanel/hist_button.gif

107 KiB

rpanel/hist_checkbox.gif

54.2 KiB

rpanel/hist_checkboxgroup.gif

113 KiB

rpanel/hist_numeric.gif

325 KiB

rpanel/hist_radio.gif

99.9 KiB

rpanel/hist_select.gif

220 KiB

rpanel/hist_select2.gif

191 KiB

rpanel/hist_slider.gif

523 KiB

rpanel/hist_text.gif

48.2 KiB

##-------------------------------------------
## 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")
......@@ -13,14 +33,42 @@ shinyServer(
})
output$ui <- renderUI({
if (is.null(input$dist)){
return()}
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",
......@@ -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,7 +121,6 @@ shinyServer(
label="Probabilidade de sucesso",
min=0.02, max=0.98,
value=0.5, step=0.02)
)
},
......@@ -55,13 +132,51 @@ 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)
)
},
......@@ -73,13 +188,43 @@ shinyServer(
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()
##-------------------------------------------
## 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment