File size: 3,194 Bytes
490d970
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
library(shiny)

source("auxiliar.R")

shinyServer(function(input, output) 
{
  
  output$grafico1 <- renderPlot({

    res <- pdf(a=input$min, 
               b=input$max, 
               cuantil=input$cuantil, 
               pdf=input$pdf)
    
    area <- res$area
    fun <- res$fun
    
    if (input$min > input$max | area > 1.01 | area < 0.99) {
      plot(c(-5, 5), c(0, 1), xlab="", ylab="", type='n',
           xaxt='n', yaxt='n', bty='n')
      text(x=0, y=0.7, col='red', cex=2,
           label='Revise los valores que ingres贸.')
      text(x=0, y=0.5, col='orange', cex=1.5,
           label=paste('El 谩rea bajo la curva es', area))
      text(x=0, y=0.3, col='purple', cex=1,
           label='El m铆nimo no puede ser mayor que el m谩ximo.')
    }
    else {
      
      par(mfrow=c(1, 2))
      
      # Para dibujar f(x)
      secuencia <- seq(input$min, input$max, length.out=1000)
      cord.x <- c(input$min, secuencia, input$max)
      cord.y <- c(0, fun(secuencia), 0)
      curve(fun, from=input$min, to=input$max, lwd=6, las=1,
            col="steelblue",
            ylim=c(0, max(fun(secuencia))),
            xlab="X", ylab="f(x)",
            main="Funci贸n de densidad")
      grid()
      
      # Para dibujar F(x)
      Fun <- function(x) 
        integrate(fun, lower=input$min, upper=x)$value
      Fun <- Vectorize(Fun)
      curve(Fun, from=input$min, to=input$max, lwd=6, las=1,
            col="steelblue",
            #ylim=c(0, max(fun(secuencia))), 
            xlab="X", ylab="F(x)",
            main="Funci贸n acumulada")
      grid()
      
    }
  })
  
  output$med_var <- renderText({

    res <- pdf(a=input$min, 
               b=input$max, 
               cuantil=input$cuantil, 
               pdf=input$pdf)
    
    area <- res$area
    fun <- res$fun
    
    f1 <- function(x) x * fun(x)
    esperanza <- integrate(f1, lower=input$min, upper=input$max)$value
    f2 <- function(x) x^2 * fun(x)
    varianza <- integrate(f2, lower=input$min, upper=input$max)$value
    varianza <- varianza - esperanza^2
    
    if (input$min > input$max | area > 1.01 | area < 0.99) {
      paste(c("Hay algo errado!!!"))
    }
    else {
      paste0(c("La v.a. X tiene E(X)=",
               round(esperanza, 4),
               " y Var(X)=", 
               round(varianza, 4)
               ),
             collapse=""
            )
    }
  })
  
  output$prob_hasta_cuantil <- renderText({
    
    res <- pdf(a=input$min, 
               b=input$max, 
               cuantil=input$cuantil, 
               pdf=input$pdf)
    
    area <- res$area
    fun <- res$fun
    
    # Acumul
    Fun <- function(x) 
      integrate(fun, lower=input$min, upper=x)$value
    
    result_prob <- ifelse(input$cuantil <= input$min, 
                          0,
                          ifelse(input$cuantil > input$max, 
                                 1, 
                                 Fun(input$cuantil)))
    
    if (input$min > input$max | area > 1.01 | area < 0.99) {
      paste(c("Hay algo errado!!!"))
    }
    else {
      paste0(c("P(X<=", input$cuantil, ")=",
               result_prob
      ),
      collapse=""
      )
    }
  })
  
})