FreddyHernandez commited on
Commit
c262a0c
verified
1 Parent(s): 1883298

Upload 3 files

Browse files
Files changed (3) hide show
  1. include.html +0 -0
  2. server.R +150 -0
  3. ui.R +85 -0
include.html ADDED
The diff for this file is too large to render. See raw diff
 
server.R ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ library(shiny)
2
+ require(gamlss)
3
+
4
+ shinyServer(function(input, output, session){
5
+
6
+ four.hist <- function(k, f, p) {
7
+ par(cex.main=0.95)
8
+ inFile <- input$file1
9
+ if(is.null(inFile))
10
+ dt <- cars
11
+ else dt <- read.csv(inFile$datapath, header=input$header,
12
+ sep=input$sep)
13
+ mod <- fitDist(dt[, p], type=f, k=k)
14
+ par(mfrow=c(2, 2), bg='gray98')
15
+ for(i in 1:4){
16
+ denst <- density(dt[, p])
17
+ res <- histDist(dt[, p], family=names(mod$fits)[i],
18
+ main='',
19
+ ylab='Density',
20
+ xlab=p, las=1,
21
+ line.wd=3,
22
+ line.ty=1,
23
+ line.col='dodgerblue2',
24
+ ylim=c(0, (2 * max(denst$y))))
25
+ gaic <- round(-2 * logLik(res) + k * length(res$parameters), 2)
26
+ title(main=paste(i, ')', names(mod$fits)[i],
27
+ 'distribution with GAIC=', gaic),
28
+ col.main='blue4')
29
+ param <- c('mu', 'sigma', 'nu', 'tau')
30
+ np <- length(res$parameters)
31
+ fun1 <- function(x) eval(parse(text=x))
32
+ hat.param <- sapply(as.list(paste('res$', param[1:np], sep='')), fun1)
33
+ hat.param <- round(hat.param, digits=2)
34
+ txt <- paste('hat(', param[1:np], ')==', hat.param, sep='')
35
+ txt <- paste(txt, collapse=', ')
36
+ legend('topright', bty='n',
37
+ legend=eval(parse(text=paste('expression(', txt, ')'))))
38
+ }
39
+ }
40
+
41
+ four.hist.qqplot <- function(k, f, p) {
42
+ par(cex.main=0.95)
43
+ inFile <- input$file1
44
+ if(is.null(inFile))
45
+ dt <- cars
46
+ else dt <- read.csv(inFile$datapath, header=input$header,
47
+ sep=input$sep)
48
+ mod <- fitDist(dt[, p], type=f, k=k)
49
+ par(mfrow=c(4, 2), bg='gray98')
50
+ for(i in 1:4){
51
+ denst <- density(dt[, p])
52
+ res <- histDist(dt[, p], family=names(mod$fits)[i],
53
+ main='',
54
+ ylab='Density',
55
+ xlab=p, las=1,
56
+ line.wd=3,
57
+ line.ty=1,
58
+ line.col='dodgerblue2',
59
+ ylim=c(0, (2 * max(denst$y))))
60
+ gaic <- round(-2 * logLik(res) + k * length(res$parameters), 2)
61
+ title(main=paste(i, ')', names(mod$fits)[i],
62
+ 'distribution with GAIC=', gaic),
63
+ col.main='blue4')
64
+ param <- c('mu', 'sigma', 'nu', 'tau')
65
+ np <- length(res$parameters)
66
+ fun1 <- function(x) eval(parse(text=x))
67
+ hat.param <- sapply(as.list(paste('res$', param[1:np], sep='')), fun1)
68
+ hat.param <- round(hat.param, digits=2)
69
+ txt <- paste('hat(', param[1:np], ')==', hat.param, sep='')
70
+ txt <- paste(txt, collapse=', ')
71
+ legend('topright', bty='n',
72
+ legend=eval(parse(text=paste('expression(', txt, ')'))))
73
+ #qqnorm(runif(10))
74
+ n <- dim(dt)[1]
75
+ a <- paste('q', names(mod$fits)[i], sep='')
76
+ a <- paste(a, '(ppoints(n),', sep='')
77
+ a <- paste(a, paste(hat.param, collapse=', '), ')')
78
+ par(bty='n', col.axis='blue4', col.lab='blue4')
79
+ qqplot(y=dt[, p], x=eval(parse(text=a)),
80
+ xlab=a, ylab=p, col='blue4')
81
+ axis(side=1, col='blue4')
82
+ axis(side=2, col='blue4')
83
+ }
84
+ }
85
+
86
+ observe({
87
+ inFile <- input$file1
88
+ if(is.null(inFile))
89
+ dt <- cars
90
+ else dt <- read.csv(inFile$datapath, header=input$header,
91
+ sep=input$sep)
92
+ updateSelectInput(session, "response",
93
+ choices = names(dt))
94
+ updateSelectInput(session, "familia")
95
+ })
96
+
97
+ output$data_table <- renderTable({
98
+ inFile <- input$file1
99
+ if(is.null(inFile))
100
+ dt <- cars
101
+ else dt <- read.csv(inFile$datapath, header=input$header,
102
+ sep=input$sep)
103
+ dt
104
+ })
105
+
106
+
107
+ output$distPlot1 <- renderPlot({
108
+ four.hist(input$k, input$familia, input$response)
109
+ })
110
+
111
+ output$distPlot2 <- renderPlot({
112
+ four.hist.qqplot(input$k, input$familia, input$response)
113
+ })
114
+
115
+ output$downplot1 <- downloadHandler(
116
+ filename = function() {
117
+ paste("four_dist", input$type_plot, sep=".")
118
+ },
119
+ # content is a function with argument file. content writes the plot to the device
120
+ content = function(file) {
121
+ if(input$type_plot == "png")
122
+ png(file) # open the png device
123
+ else
124
+ pdf(file) # open the pdf device
125
+ four.hist(input$k, input$familia, input$response) # draw the plot
126
+ dev.off() # turn the device off
127
+
128
+ }
129
+ )
130
+
131
+ output$downplot2 <- downloadHandler(
132
+ filename = function() {
133
+ paste("four_dist", input$type_plot, sep=".")
134
+ },
135
+ # content is a function with argument file. content writes the plot to the device
136
+ content = function(file) {
137
+ if(input$type_plot == "png")
138
+ png(file) # open the png device
139
+ else
140
+ pdf(file) # open the pdf device
141
+ four.hist.qqplot(input$k, input$familia, input$response) # draw the plot
142
+ dev.off() # turn the device off
143
+
144
+ }
145
+ )
146
+
147
+ output$markdown <- renderUI({
148
+ HTML(markdown::markdownToHTML(knit('Teoria.Rmd', quiet = TRUE)))
149
+ })
150
+ })
ui.R ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ library(shiny)
2
+ library(knitr)
3
+
4
+ shinyUI(pageWithSidebar(
5
+ headerPanel( "Explorando la distribuci贸n que mejor se ajusta a una variable"),
6
+ sidebarPanel(
7
+ h6("Esta aplicaci贸n sirve para identificar
8
+ las cuatro distribuciones
9
+ que mejor se ajustan a una variable de
10
+ una base de datos ingresada por el usuario."),
11
+ h6('Como ejemplo se muestran los resultados obtenidos para
12
+ la variable speed de la base de datos cars de R.'),
13
+ h4('Para usar la aplicaci贸n haga lo siguiente:'),
14
+ h6('1. Suba su base,'),
15
+ h6('2. Indique el tipo de separaci贸n de su base,'),
16
+ h6('3. Seleccione la variable de inter茅s.'),
17
+ fileInput(inputId='file1', label='Use el bot贸n siguiente para
18
+ cargar la base de datos.',
19
+ accept = c(
20
+ 'text/csv',
21
+ 'text/comma-separated-values',
22
+ 'text/tab-separated-values',
23
+ 'text/plain',
24
+ '.csv',
25
+ '.tsv'
26
+ )),
27
+ checkboxInput('header', label='驴Tiene encabezado la base
28
+ de datos?', value=TRUE),
29
+ selectInput(inputId="sep", label="驴Cual es la separaci贸n de sus datos?",
30
+ choices=list('tab'='\t', ','=',', ';'=';', 'space'=' '),
31
+ selected=';'),
32
+ selectInput("response", label="Seleccione la variable
33
+ de la base de datos", choices=""),
34
+ selectInput("familia", "Seleccione la familia a la cual pertenece
35
+ la variable de inter茅s:",
36
+ choice=list("Reales positivos" = "realplus",
37
+ "Reales" = "realAll",
38
+ "Reales entre 0 y 1" = "real0to1",
39
+ "Conteos" = "counts",
40
+ "Binomiales" = "binom")),
41
+ sliderInput("k", 'Ingrese una penalizaci贸n \\( k \\) para
42
+ penalizar el exceso de par谩metros
43
+ en el calculo del \\(GAIC=-2 \\times logLik + k \\times df\\)
44
+ donde \\( df \\) es n煤mero de parametros
45
+ del modelo.',
46
+ min=2,
47
+ max=20,
48
+ value=2,
49
+ step=1,
50
+ animate=TRUE),
51
+ radioButtons(inputId = "type_plot",
52
+ label = "Select the file type to download the plot",
53
+ choices = list("pdf", "png")),
54
+ br(),
55
+ p("This app was created by Semillero de R at Universidad Nacional
56
+ de Colombia:"),
57
+ tags$a(href="https://srunal.github.io/", "https://srunal.github.io/")),
58
+ mainPanel(
59
+ tabsetPanel(type ="pills",
60
+ tabPanel("Selected distributions I",
61
+ h4('A continuaci贸n se presenta el histograma para
62
+ la variable de inter茅s
63
+ con la curva de densidad para cada distribuci贸n.'),
64
+ plotOutput("distPlot1", width="700px", height="600px"),
65
+ downloadButton(outputId = "downplot1", label = "Download the plot")
66
+ ),
67
+ tabPanel("Selected distributions II",
68
+ h4('A continuaci贸n se presenta el histograma para
69
+ la variable de inter茅s
70
+ con la curva de densidad para cada distribuci贸n y
71
+ el qqplot asociado.'),
72
+ plotOutput("distPlot2", width="700px", height="800px"),
73
+ downloadButton(outputId = "downplot2", label = "Download the plot")
74
+ ),
75
+ tabPanel("Data",
76
+ "A continuacion la base de datos ingresada por el usuario.",
77
+ uiOutput('data_table')),
78
+ #tabPanel("Goodness of fit test"),
79
+ tabPanel("Theory", includeHTML("include.html"))
80
+ )
81
+ )
82
+ )
83
+ )
84
+
85
+