Spaces:
Running
Running
Nuevos archivos
Browse files- auxiliar.R +14 -0
- server.R +119 -0
- ui.R +45 -0
auxiliar.R
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
pdf <- function(a, b, cuantil, pdf) {
|
| 3 |
+
# To convert pmf to a function
|
| 4 |
+
pdf <- gsub(" ", "", pdf)
|
| 5 |
+
pdf <- substr(pdf, start=6, stop=nchar(pdf))
|
| 6 |
+
pdf <- paste(pdf, ' * x^0')
|
| 7 |
+
fun <- function(x) eval(parse(text=pdf))
|
| 8 |
+
prob <- integrate(fun, lower=a, upper=cuantil)$value
|
| 9 |
+
area <- integrate(fun, lower=a, upper=b)$value
|
| 10 |
+
list(prob=prob, area=area, fun=fun)
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
server.R
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
library(shiny)
|
| 2 |
+
|
| 3 |
+
source("auxiliar.R")
|
| 4 |
+
|
| 5 |
+
shinyServer(function(input, output)
|
| 6 |
+
{
|
| 7 |
+
|
| 8 |
+
output$grafico1 <- renderPlot({
|
| 9 |
+
|
| 10 |
+
res <- pdf(a=input$min,
|
| 11 |
+
b=input$max,
|
| 12 |
+
cuantil=input$cuantil,
|
| 13 |
+
pdf=input$pdf)
|
| 14 |
+
|
| 15 |
+
area <- res$area
|
| 16 |
+
fun <- res$fun
|
| 17 |
+
|
| 18 |
+
if (input$min > input$max | area > 1.01 | area < 0.99) {
|
| 19 |
+
plot(c(-5, 5), c(0, 1), xlab="", ylab="", type='n',
|
| 20 |
+
xaxt='n', yaxt='n', bty='n')
|
| 21 |
+
text(x=0, y=0.7, col='red', cex=2,
|
| 22 |
+
label='Revise los valores que ingres贸.')
|
| 23 |
+
text(x=0, y=0.5, col='orange', cex=1.5,
|
| 24 |
+
label=paste('El 谩rea bajo la curva es', area))
|
| 25 |
+
text(x=0, y=0.3, col='purple', cex=1,
|
| 26 |
+
label='El m铆nimo no puede ser mayor que el m谩ximo.')
|
| 27 |
+
}
|
| 28 |
+
else {
|
| 29 |
+
|
| 30 |
+
par(mfrow=c(1, 2))
|
| 31 |
+
|
| 32 |
+
# Para dibujar f(x)
|
| 33 |
+
secuencia <- seq(input$min, input$max, length.out=1000)
|
| 34 |
+
cord.x <- c(input$min, secuencia, input$max)
|
| 35 |
+
cord.y <- c(0, fun(secuencia), 0)
|
| 36 |
+
curve(fun, from=input$min, to=input$max, lwd=6, las=1,
|
| 37 |
+
col="steelblue",
|
| 38 |
+
ylim=c(0, max(fun(secuencia))),
|
| 39 |
+
xlab="X", ylab="f(x)",
|
| 40 |
+
main="Funci贸n de densidad")
|
| 41 |
+
grid()
|
| 42 |
+
|
| 43 |
+
# Para dibujar F(x)
|
| 44 |
+
Fun <- function(x)
|
| 45 |
+
integrate(fun, lower=input$min, upper=x)$value
|
| 46 |
+
Fun <- Vectorize(Fun)
|
| 47 |
+
curve(Fun, from=input$min, to=input$max, lwd=6, las=1,
|
| 48 |
+
col="steelblue",
|
| 49 |
+
#ylim=c(0, max(fun(secuencia))),
|
| 50 |
+
xlab="X", ylab="F(x)",
|
| 51 |
+
main="Funci贸n acumulada")
|
| 52 |
+
grid()
|
| 53 |
+
|
| 54 |
+
}
|
| 55 |
+
})
|
| 56 |
+
|
| 57 |
+
output$med_var <- renderText({
|
| 58 |
+
|
| 59 |
+
res <- pdf(a=input$min,
|
| 60 |
+
b=input$max,
|
| 61 |
+
cuantil=input$cuantil,
|
| 62 |
+
pdf=input$pdf)
|
| 63 |
+
|
| 64 |
+
area <- res$area
|
| 65 |
+
fun <- res$fun
|
| 66 |
+
|
| 67 |
+
f1 <- function(x) x * fun(x)
|
| 68 |
+
esperanza <- integrate(f1, lower=input$min, upper=input$max)$value
|
| 69 |
+
f2 <- function(x) x^2 * fun(x)
|
| 70 |
+
varianza <- integrate(f2, lower=input$min, upper=input$max)$value
|
| 71 |
+
varianza <- varianza - esperanza^2
|
| 72 |
+
|
| 73 |
+
if (input$min > input$max | area > 1.01 | area < 0.99) {
|
| 74 |
+
paste(c("Hay algo errado!!!"))
|
| 75 |
+
}
|
| 76 |
+
else {
|
| 77 |
+
paste0(c("La v.a. X tiene E(X)=",
|
| 78 |
+
round(esperanza, 4),
|
| 79 |
+
" y Var(X)=",
|
| 80 |
+
round(varianza, 4)
|
| 81 |
+
),
|
| 82 |
+
collapse=""
|
| 83 |
+
)
|
| 84 |
+
}
|
| 85 |
+
})
|
| 86 |
+
|
| 87 |
+
output$prob_hasta_cuantil <- renderText({
|
| 88 |
+
|
| 89 |
+
res <- pdf(a=input$min,
|
| 90 |
+
b=input$max,
|
| 91 |
+
cuantil=input$cuantil,
|
| 92 |
+
pdf=input$pdf)
|
| 93 |
+
|
| 94 |
+
area <- res$area
|
| 95 |
+
fun <- res$fun
|
| 96 |
+
|
| 97 |
+
# Acumul
|
| 98 |
+
Fun <- function(x)
|
| 99 |
+
integrate(fun, lower=input$min, upper=x)$value
|
| 100 |
+
|
| 101 |
+
result_prob <- ifelse(input$cuantil <= input$min,
|
| 102 |
+
0,
|
| 103 |
+
ifelse(input$cuantil > input$max,
|
| 104 |
+
1,
|
| 105 |
+
Fun(input$cuantil)))
|
| 106 |
+
|
| 107 |
+
if (input$min > input$max | area > 1.01 | area < 0.99) {
|
| 108 |
+
paste(c("Hay algo errado!!!"))
|
| 109 |
+
}
|
| 110 |
+
else {
|
| 111 |
+
paste0(c("P(X<=", input$cuantil, ")=",
|
| 112 |
+
result_prob
|
| 113 |
+
),
|
| 114 |
+
collapse=""
|
| 115 |
+
)
|
| 116 |
+
}
|
| 117 |
+
})
|
| 118 |
+
|
| 119 |
+
})
|
ui.R
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
library(shiny)
|
| 2 |
+
|
| 3 |
+
shinyUI(fluidPage(
|
| 4 |
+
|
| 5 |
+
# Application title
|
| 6 |
+
titlePanel("Funci贸n de densidad de probabilidad"),
|
| 7 |
+
|
| 8 |
+
sidebarLayout(
|
| 9 |
+
sidebarPanel(
|
| 10 |
+
HTML("Ingrese la funci贸n de densidad f(x) y los valores
|
| 11 |
+
m铆nimo y m谩ximo que la v.a. X puede tomar."),
|
| 12 |
+
br(),
|
| 13 |
+
br(),
|
| 14 |
+
textInput("pdf",
|
| 15 |
+
"Ingrese la f贸rmula de la densidad en forma apropiada
|
| 16 |
+
usando los operadores +, -, * y /. Vea el ejemplo abajo.",
|
| 17 |
+
"f(x)=(-2*x+3)/2"),
|
| 18 |
+
numericInput(inputId = "min",
|
| 19 |
+
label = HTML("Ingrese el valor m铆nimo de X. Si el
|
| 20 |
+
valor m铆nimo es -∞, escriba un
|
| 21 |
+
n煤mero negativo grande."),
|
| 22 |
+
value = 0, step=0.01),
|
| 23 |
+
numericInput(inputId = "max",
|
| 24 |
+
label = HTML("Ingrese el valor m谩ximo de X. Si el
|
| 25 |
+
valor m谩ximo es ∞, escriba un
|
| 26 |
+
n煤mero positivo grande."),
|
| 27 |
+
value = 1, step=0.01),
|
| 28 |
+
numericInput(inputId = "cuantil",
|
| 29 |
+
label = HTML("Ingrese el valor de x para el cual desea
|
| 30 |
+
calcular P(X ≤ x)"),
|
| 31 |
+
value = 0.75, step=0.01),
|
| 32 |
+
br(),
|
| 33 |
+
p("App creada por el Semillero de R de la Universidad Nacional de Colombia."),
|
| 34 |
+
tags$a(href="https://srunal.github.io", "https://srunal.github.io")
|
| 35 |
+
),
|
| 36 |
+
|
| 37 |
+
# Show a plot
|
| 38 |
+
mainPanel(
|
| 39 |
+
h3("", align = "center"),
|
| 40 |
+
plotOutput("grafico1", width = "100%", height = "300px"),
|
| 41 |
+
verbatimTextOutput('med_var'),
|
| 42 |
+
verbatimTextOutput('prob_hasta_cuantil')
|
| 43 |
+
)
|
| 44 |
+
)
|
| 45 |
+
))
|