Spaces:
Runtime error
Runtime error
FreddyHernandez
commited on
Commit
•
8570062
1
Parent(s):
fdbc8ba
Nuevos archivos
Browse files- auxiliar.R +24 -0
- server.R +96 -0
- ui.R +44 -0
auxiliar.R
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
pmf <- function(a, b, pmf) {
|
3 |
+
# To convert pmf to a function
|
4 |
+
pmf <- gsub(" ", "", pmf)
|
5 |
+
pmf <- substr(pmf, start=6, stop=nchar(pmf))
|
6 |
+
funcion <- function(x) eval(parse(text=pmf))
|
7 |
+
|
8 |
+
# To obtain probs and cumulative
|
9 |
+
x_vals <- a:b
|
10 |
+
n <- length(x_vals)
|
11 |
+
probs <- numeric(n)
|
12 |
+
for (i in 1:n)
|
13 |
+
probs[i] <- funcion(x_vals[i])
|
14 |
+
suma <- sum(probs)
|
15 |
+
cumul_probs <- cumsum(probs)
|
16 |
+
|
17 |
+
list(x_vals=x_vals,
|
18 |
+
cumul_probs=cumul_probs,
|
19 |
+
probs=probs,
|
20 |
+
suma=suma)
|
21 |
+
}
|
22 |
+
|
23 |
+
|
24 |
+
|
server.R
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
library(shiny)
|
2 |
+
|
3 |
+
source("auxiliar.R")
|
4 |
+
|
5 |
+
shinyServer(function(input, output)
|
6 |
+
{
|
7 |
+
|
8 |
+
output$grafico1 <- renderPlot({
|
9 |
+
|
10 |
+
res <- pmf(a=input$min,
|
11 |
+
b=input$max,
|
12 |
+
pmf=input$pmf)
|
13 |
+
|
14 |
+
x_vals <- res$x_vals
|
15 |
+
probs <- res$probs
|
16 |
+
cumul_probs <- res$cumul_probs
|
17 |
+
suma <- res$suma
|
18 |
+
|
19 |
+
if (input$min > input$max | suma > 1.01 | suma < 0.99) {
|
20 |
+
|
21 |
+
plot(c(-5, 5), c(0, 1), xlab="", ylab="", type='n',
|
22 |
+
xaxt='n', yaxt='n', bty='n')
|
23 |
+
text(x=0, y=0.7, col='red', cex=2,
|
24 |
+
label='Revise los valores que ingresó.')
|
25 |
+
text(x=0, y=0.5, col='orange', cex=1.5,
|
26 |
+
label=paste('La suma de las probabilidades es', suma))
|
27 |
+
text(x=0, y=0.3, col='purple', cex=1,
|
28 |
+
label='El mínimo no puede ser mayor que el máximo.')
|
29 |
+
}
|
30 |
+
else {
|
31 |
+
par(mfrow=c(1, 2))
|
32 |
+
|
33 |
+
# Para dibujar f(x)
|
34 |
+
plot(x=x_vals, y=probs, las=1,
|
35 |
+
xlab="X", ylab="f(x)",
|
36 |
+
main="Función de masa",
|
37 |
+
type="h", lwd=3, col="steelblue")
|
38 |
+
grid()
|
39 |
+
|
40 |
+
# Para dibujar F(x)
|
41 |
+
F <- stepfun(x=x_vals, y=c(0, cumul_probs), right=TRUE)
|
42 |
+
plot(F, verticals=FALSE,
|
43 |
+
lwd=3, col="steelblue", las=1,
|
44 |
+
xlab="X", ylab="F(x)",
|
45 |
+
main="Función acumulada")
|
46 |
+
grid()
|
47 |
+
}
|
48 |
+
})
|
49 |
+
|
50 |
+
output$med_var <- renderText({
|
51 |
+
|
52 |
+
res <- pmf(a=input$min,
|
53 |
+
b=input$max,
|
54 |
+
pmf=input$pmf)
|
55 |
+
|
56 |
+
x_vals <- res$x_vals
|
57 |
+
probs <- res$probs
|
58 |
+
cumul_probs <- res$cumul_probs
|
59 |
+
suma <- res$suma
|
60 |
+
|
61 |
+
esperanza <- sum(x_vals * probs)
|
62 |
+
varianza <- sum((x_vals - esperanza)^2 * probs)
|
63 |
+
|
64 |
+
if (input$min > input$max | suma > 1.01 | suma < 0.99) {
|
65 |
+
paste(c("Hay algo errado!!!"))
|
66 |
+
}
|
67 |
+
else {
|
68 |
+
paste0(c("La v.a. X tiene E(X)=",
|
69 |
+
round(esperanza, 4),
|
70 |
+
"y Var(X)=", round(varianza, 4)))
|
71 |
+
}
|
72 |
+
})
|
73 |
+
|
74 |
+
output$tabla_probs <- renderTable({
|
75 |
+
|
76 |
+
res <- pmf(a=input$min,
|
77 |
+
b=input$max,
|
78 |
+
pmf=input$pmf)
|
79 |
+
|
80 |
+
x_vals <- res$x_vals
|
81 |
+
probs <- res$probs
|
82 |
+
cumul_probs <- res$cumul_probs
|
83 |
+
suma <- res$suma
|
84 |
+
|
85 |
+
if (input$min > input$max | suma > 1.01 | suma < 0.99) {
|
86 |
+
paste(c("Hay algo errado!!!"))
|
87 |
+
}
|
88 |
+
else {
|
89 |
+
data.frame("X"=x_vals,
|
90 |
+
"f(x)"=probs,
|
91 |
+
"F(x)"=cumul_probs,
|
92 |
+
check.names = FALSE)
|
93 |
+
}
|
94 |
+
}, digits=6, bordered = TRUE, align = "c")
|
95 |
+
|
96 |
+
})
|
ui.R
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
library(shiny)
|
2 |
+
|
3 |
+
shinyUI(fluidPage(
|
4 |
+
|
5 |
+
# Application title
|
6 |
+
titlePanel("Función de masa de probabilidad"),
|
7 |
+
|
8 |
+
sidebarLayout(
|
9 |
+
sidebarPanel(
|
10 |
+
HTML("Ingrese la función de masa f(x) y los valores
|
11 |
+
mínimo y máximo que la v.a. X puede tomar. Los valores de X
|
12 |
+
deben ser de uno en uno, comenzando en
|
13 |
+
el mínimo y terminando en el máximo."),
|
14 |
+
br(),
|
15 |
+
br(),
|
16 |
+
textInput("pmf",
|
17 |
+
"Ingrese la fórmula de la función de masa en forma apropiada
|
18 |
+
usando los operadores +, -, * y /. Vea el ejemplo abajo.",
|
19 |
+
"f(x)=(2*x+1)/25"),
|
20 |
+
numericInput(inputId = "min",
|
21 |
+
label = HTML("Ingrese el valor mínimo de X. Si el
|
22 |
+
valor mínimo es -∞, escriba un
|
23 |
+
número negativo grande."),
|
24 |
+
value = 0),
|
25 |
+
numericInput(inputId = "max",
|
26 |
+
label = HTML("Ingrese el valor máximo de X. Si el
|
27 |
+
valor máximo es ∞, escriba un
|
28 |
+
número positivo grande."),
|
29 |
+
value = 4),
|
30 |
+
br(),
|
31 |
+
p("App creada por el Semillero de R de la Universidad Nacional de Colombia."),
|
32 |
+
tags$a(href="https://srunal.github.io", "https://srunal.github.io")
|
33 |
+
),
|
34 |
+
|
35 |
+
# Show a plot
|
36 |
+
mainPanel(
|
37 |
+
h3("", align = "center"),
|
38 |
+
plotOutput("grafico1", width = "100%", height = "300px"),
|
39 |
+
verbatimTextOutput("med_var"),
|
40 |
+
h5("Tabla con f(x) y F(x) para la v.a."),
|
41 |
+
tableOutput("tabla_probs")
|
42 |
+
)
|
43 |
+
)
|
44 |
+
))
|