library(shiny)
require(grDevices)
shinyServer(function(input,output,session){
observe({
inFile <- input$file1
if(is.null(inFile)) dt <- read.table('datos.txt', header=T, sep='\t')
else
dt <- read.csv(inFile$datapath, header=input$header, sep=input$sep)
updateSelectInput(session, "product", choices = names(dt))
})
output$superficie <- renderPlot({
inFile <- input$file1
if(is.null(inFile)) dt <- read.table('datos.txt', header=T, sep='\t')
else
dt <- read.csv(inFile$datapath, header=input$header, sep=input$sep)
x <- as.vector(dt[, input$product])
Dist <- ifelse(input$Distribucion == 'Normal', 'norm',
ifelse(input$Distribucion == 'Weibull', 'weibull', 'gamma'))
n.points <- 30
sigmas <- 3
####
loglik_function <- function(par1, par2) {
eval(parse(text=paste('sum(d', Dist, '(x=x, par1, par2, log=T))', sep='')))
}
loglik_function <- Vectorize(loglik_function)
require(MASS)
if (Dist == 'norm')
fit <- fitdistr(x, densfun='normal')
if (Dist == 'weibull')
fit <- fitdistr(x, densfun='weibull')
if (Dist == 'gamma')
fit <- fitdistr(x, densfun='gamma')
par1 <- seq(from=fit$estimate[1]-sigmas*fit$sd[1],
to=fit$estimate[1]+sigmas*fit$sd[1],
length.out=n.points)
par2 <- seq(from=fit$estimate[2]-sigmas*fit$sd[2],
to=fit$estimate[2]+sigmas*fit$sd[2],
length.out=n.points)
loglik <- outer(par1, par2, loglik_function)
# To create the colors
# ---------------
jet.colors <- colorRampPalette( c("blue", "green") )
nbcol <- 100
color <- jet.colors(nbcol)
ncz <- ncol(loglik)
nrz <- nrow(loglik)
zfacet <- loglik[-1, -1] + loglik[-1, -ncz] + loglik[-nrz, -1] + loglik[-nrz, -ncz]
facetcol <- cut(zfacet, nbcol)
# ---------------
myplot <- persp(par1, par2, loglik, theta=30, phi=30,
col=color[facetcol], ticktype = "detailed", nticks=4)
mypoints <- trans3d(fit$estimate[1], fit$estimate[2], fit$loglik, pmat=myplot)
points(mypoints, pch=19, col="red", cex=2)
lines(trans3d(x=fit$estimate[1], y=par2[1:(n.points/2)],
z=min(loglik), pmat=myplot), col=2, lty=2)
lines(trans3d(x=par1[-(1:(n.points/2))], y=fit$estimate[2],
z=min(loglik), pmat=myplot), col=2, lty=2)
lines(trans3d(x=fit$estimate[1], y=fit$estimate[2],
z=sort(loglik), pmat=myplot), col=2, lty=2)
})
output$lateoria <- renderUI({
HTML(markdown::markdownToHTML(knit('lateoria.txt', quiet = TRUE)))
})
})
library(shiny)
# Define UI for random distribution application
fluidPage(
# Application title
titlePanel("Superficie de log-verosimilitud"),
sidebarLayout(
sidebarPanel(
h5('Esta aplicación sirve para dibujar la superficie de
log-verosimilitud asociada a una distribución de 2 parámetros
dados los valores de una variable ingresados por el usuario.'),
br(),
fileInput('file1', 'Use el siguiente botón para ingresar su base de datos.',
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
),
checkboxInput('header', 'Marque la casilla si su base de datos tiene
ENCABEZADO (header)', TRUE),
radioButtons('sep', '¿Cuál es símbolo de separación dentro de su base datos?',
c(Tab='\t', Comma=',', Semicolon=';' )
),
tags$hr(),
selectInput("product", "Seleccione la variable para la cual quiere
dibujar la superficie de log-verosimilitud",""),
selectInput(inputId="Distribucion",
label="Elija una distribución de dos parámetros:",
choices=c("Normal", "Weibull", "Gamma"),
selected="Normal"),
p("App creada por el Semillero de R de la Universidad Nacional de Colombia."),
tags$a(href="https://srunal.github.io", "https://srunal.github.io")
),
# Show a tabset that includes a plot, summary, and table view
# of the generated distribution
mainPanel(
tabsetPanel(type = "pills",
tabPanel("Superficie", plotOutput("superficie",
width="600px",
height="600px")),
tabPanel("Teoría", includeHTML("include.html"))
)
)
)
)