ph_dif_medias / server.R
FreddyHernandez's picture
Upload 8 files
2cdebcb verified
library(shiny)
source("auxiliar.R")
shinyServer(function(input,output,session){
observe({
inFile <- input$file1
if(is.null(inFile))
dt <- datos
else dt <- read.csv(inFile$datapath, header=input$header, sep=input$sep)
updateSelectInput(session, "variable1",
choices = names(dt),
selected = "Weight")
updateSelectInput(session, "variable2",
choices = names(dt),
selected = "Group")
})
output$summary <- renderTable({
inFile <- input$file1
if(is.null(inFile))
dt <- datos
else dt <- read.csv(inFile$datapath, header=input$header, sep=input$sep)
dt <- na.omit(dt) # Para eliminar obs con NA
dt
})
output$statistic <- renderTable({
inFile <- input$file1
if(is.null(inFile))
dt <- datos
else dt <- read.csv(inFile$datapath, header=input$header, sep=input$sep)
# Para eliminar obs con NA
dt <- na.omit(dt)
# Para obtener x y grupo
x <- dt[, input$variable1]
group <- dt[, input$variable2]
group <- as.factor(group)
xx <- split(x, group) # Lista con variable interes
resumen <- function(x) c(mean(x), var(x), length(x))
res <- sapply(xx, resumen)
rownames(res) <- c('Media', 'Varianza', 'N煤mero obs')
t(res)
},
rownames = TRUE, align='c', bordered = TRUE) # Para obtener tabla con rownames
output$appPlot <- renderPlot({
inFile <- input$file1
if(is.null(inFile))
dt <- datos
else dt <- read.csv(inFile$datapath, header=input$header, sep=input$sep)
# Para eliminar obs con NA
dt <- na.omit(dt)
# Para obtener x y grupo
x <- dt[, input$variable1]
group <- dt[, input$variable2]
group <- as.factor(group)
if (nlevels(group) != 2) {
plot(1, type = "n", xlab = "", ylab = "", axes = FALSE)
mensaje <- "La variable cualitativa \n que eligi贸 debe tener s贸lo \n 2 niveles."
text(x=1, y=1, mensaje, cex=2, col = "blue", pos=3)
}
else {
par(mfrow=c(1, 2), bg="gray98")
# Para dibujar las densidades
xx <- split(x, group)
den <- lapply(xx, density)
plot(den[[1]], lwd=4, col='deepskyblue3',
main='Densidad', las=1,
xlab=as.character(input$variable1),
ylab='Densidad',
xlim=range(range(den[[1]]$x), range(den[[2]]$x)),
ylim=c(0, max(c(den[[1]]$y, den[[2]]$y))))
lines(den[[2]], lwd=4, col='firebrick3')
# Leyenda para distiguir las densidades
legend('topright', bty='n',
lwd=4,
col=c('deepskyblue3', 'firebrick3'),
legend=unique(group))
# Para dibujar los qqplot
qq1 <- qqnorm(xx[[1]], plot.it=FALSE)
qq2 <- qqnorm(xx[[2]], plot.it=FALSE)
plot(qq1, las=1, main='QQplot',
pch=19, col='deepskyblue3',
xlim=range(c(qq1$x, qq2$x)),
ylim=range(c(qq1$y, qq2$y)),
xlab='Cuantiles te贸ricos N(0, 1)',
ylab=as.character(input$variable1))
points(qq2, pch=19, col='firebrick3')
# Para construir los qqplot
qqline(xx[[1]], col='deepskyblue3')
qqline(xx[[2]], col='firebrick3')
# Para incluir el valor P de Shapiro
shapi <- lapply(xx, shapiro.test)
leyenda <- c(paste('Valor P=', round(shapi[[1]]$p.value, 2)),
paste('Valor P=', round(shapi[[2]]$p.value, 2)))
legend('topleft', bty='n',
text.col=c('deepskyblue3', 'firebrick3'),
legend=leyenda)
}
})
output$resul1 <- renderText({
inFile <- input$file1
if(is.null(inFile))
dt <- datos
else dt <- read.csv(inFile$datapath, header=input$header, sep=input$sep)
# Para eliminar obs con NA
dt <- na.omit(dt)
# Para obtener x y grupo
x <- dt[, input$variable1]
group <- dt[, input$variable2]
group <- as.factor(group)
if (nlevels(group) != 2) {
paste0("La variable cualitativa \n que eligi贸 debe tener s贸lo \n 2 niveles.")
}
else {
xx <- split(x, group)
ph <- t.test(x=xx[[1]], y=xx[[2]],
alternative=input$h0,
mu=input$delta0,
conf.level=input$alfa,
var.equal=input$var.equal)
paste0('El estad铆stico de prueba es t0=', round(ph$statistic, 4),
' con un valor-P de ', round(ph$p.value, 4), '.')
}
})
output$resul2 <- renderText({
inFile <- input$file1
if(is.null(inFile))
dt <- datos
else dt <- read.csv(inFile$datapath, header=input$header, sep=input$sep)
dt <- na.omit(dt) # Para eliminar obs con NA
x <- dt[, input$variable1]
group <- dt[, input$variable2]
group <- as.factor(group)
if (nlevels(group) != 2) {
paste0("La variable cualitativa \n que eligi贸 debe tener s贸lo \n 2 niveles.")
}
else {
xx <- split(x, group)
ph <- t.test(x=xx[[1]], y=xx[[2]],
alternative=input$h0,
mu=input$delta0,
conf.level=input$alfa,
var.equal=input$var.equal)
intervalo <- paste("(", round(ph$conf.int[1], digits=4),
", ",
round(ph$conf.int[2], digits=4),
").", sep='')
paste0('El intervalo de confianza del ', 100*input$alfa,
'% para la diferencia de medias poblacionales es ',
intervalo)
}
})
output$miteoria <- renderUI({
HTML(markdown::markdownToHTML(knit(input='teoria.md', quiet = TRUE)))
})
})