FreddyHernandez
commited on
Commit
•
57e83aa
1
Parent(s):
fc9889c
Upload 2 files
Browse files
server.R
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
n <- 20
|
2 |
+
b0 <- isolate(sample(-5:5, size=1))
|
3 |
+
b1 <- isolate(sample(-5:5, size=1))
|
4 |
+
x <- isolate(runif(n, min=-5, max=10))
|
5 |
+
y <- isolate(rnorm(n, mean=b0+b1*x, sd=5))
|
6 |
+
mod <- lm(y ~ x)
|
7 |
+
best_b0 <- round(coef(mod)[1], 2)
|
8 |
+
best_b1 <- round(coef(mod)[2], 2)
|
9 |
+
ssr <- sum((y-(best_b0+best_b1*x))^2)
|
10 |
+
ssr <- round(ssr, digits=2)
|
11 |
+
aux <- sum((y-(0+1*x))^2)
|
12 |
+
|
13 |
+
library(shiny)
|
14 |
+
|
15 |
+
# Define server logic required to draw a histogram
|
16 |
+
shinyServer(function(input, output) {
|
17 |
+
|
18 |
+
#Example
|
19 |
+
values <- reactiveValues(df_data = NULL)
|
20 |
+
values <- reactiveValues(df = data.frame(Intercepto=0,
|
21 |
+
Pendiente=1,
|
22 |
+
SCE=aux))
|
23 |
+
|
24 |
+
newEntry <- observeEvent(input$update,{
|
25 |
+
b0_new <- c(values$df$Intercepto, as.numeric(input$b0))
|
26 |
+
b1_new <- c(values$df$Pendiente, as.numeric(input$b1))
|
27 |
+
rta <- sum((y - (input$b0 + input$b1 * x))^2)
|
28 |
+
rta <- round(rta, digits=2)
|
29 |
+
SCE_new <- c(values$df$SCE, as.numeric(rta))
|
30 |
+
|
31 |
+
values$df <- data.frame(Intercepto = b0_new,
|
32 |
+
Pendiente = b1_new,
|
33 |
+
SCE = SCE_new)
|
34 |
+
})
|
35 |
+
|
36 |
+
output$example <- renderTable({values$df})
|
37 |
+
|
38 |
+
output$my_plot <- renderPlot({
|
39 |
+
par(mfrow=c(1, 2))
|
40 |
+
|
41 |
+
# Figura 1
|
42 |
+
plot(x=x, y=y,
|
43 |
+
#main=c(best_b0, best_b1),
|
44 |
+
main="Diagrama de dispersion y \n modelo ajustado",
|
45 |
+
pch=19, las=1)
|
46 |
+
abline(h=0, v=0, lty="dashed", col=gray(0.8))
|
47 |
+
aux <- function(inter, slope) inter + slope * x
|
48 |
+
y_hat <- aux(inter = tail(values$df$Intercepto, n=1),
|
49 |
+
slope = tail(values$df$Pendiente, n=1))
|
50 |
+
#abline(a=input$b0, b=input$b1, lwd=3, col="deepskyblue3")
|
51 |
+
abline(a=tail(values$df$Intercepto, n=1),
|
52 |
+
b=tail(values$df$Pendiente, n=1),
|
53 |
+
lwd=3, col="deepskyblue3")
|
54 |
+
segments(x0=x, y0=y, x1 =x, y1=y_hat, col="tomato", lty="dotted")
|
55 |
+
|
56 |
+
# If correct
|
57 |
+
if (min(values$df$SCE) == ssr)
|
58 |
+
legend("center","center",
|
59 |
+
paste0(input$nombre, "\n felicitaciones, \n lo hiciste bien!!! "),
|
60 |
+
text.col="darkorchid3", cex=2, bty="n", bg="#ffffff00")
|
61 |
+
|
62 |
+
# Figura 2
|
63 |
+
with(values$df, plot(SCE, las=1, type='b', pch=19,
|
64 |
+
ylab="SCE",
|
65 |
+
xlab="Intento",
|
66 |
+
ylim=c(0, max(SCE)),
|
67 |
+
main="Evolucion de SCE"))
|
68 |
+
text(x=1, y=ssr,
|
69 |
+
paste0("Obj=", ssr),
|
70 |
+
col="green4", pos=4)
|
71 |
+
abline(h=ssr, col="green4", lty="dashed", lwd=2)
|
72 |
+
|
73 |
+
# If correct
|
74 |
+
if (min(values$df$SCE) == ssr)
|
75 |
+
legend("center","center",
|
76 |
+
paste0(input$nombre, "\n felicitaciones, \n lo hiciste bien!!! "),
|
77 |
+
text.col="darkorchid3", cex=2, bty="n", bg="#ffffff00")
|
78 |
+
})
|
79 |
+
|
80 |
+
})
|
ui.R
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
library(shiny)
|
2 |
+
|
3 |
+
# Define UI for application that draws a histogram
|
4 |
+
shinyUI(fluidPage(
|
5 |
+
|
6 |
+
# Application title
|
7 |
+
titlePanel("Juguemos a adivinar"),
|
8 |
+
|
9 |
+
# Sidebar with a slider input for number of bins
|
10 |
+
sidebarLayout(
|
11 |
+
sidebarPanel(
|
12 |
+
h5("En este juego usted debe adivinar el intercepto
|
13 |
+
y la pendiente de la linea recta azul que mejor
|
14 |
+
explica la nube de puntos. El objetivo es MINIMIZAR
|
15 |
+
la suma de cuadrados de los errores (SCE)."),
|
16 |
+
h5("En el diagrama de la izquierda estan los puntos
|
17 |
+
y la linea recta."),
|
18 |
+
h5("En el diagrama de la derecha se muestra la evolucion
|
19 |
+
del SCE para cada intento. La linea de color verde
|
20 |
+
representa el minimo
|
21 |
+
valor de SCE, ese
|
22 |
+
es el valor objetivo que usted debe alcanzar."),
|
23 |
+
br(),
|
24 |
+
textInput("nombre",
|
25 |
+
label="Por favor ingrese su nombre"),
|
26 |
+
numericInput("b0",
|
27 |
+
"Ingrese el valor del intercepto:",
|
28 |
+
min = -50,
|
29 |
+
max = 50,
|
30 |
+
step = 0.01,
|
31 |
+
value = 0),
|
32 |
+
numericInput("b1",
|
33 |
+
"Ingrese el valor de la pendiente:",
|
34 |
+
min = -50,
|
35 |
+
max = 50,
|
36 |
+
step = 0.01,
|
37 |
+
value = 1),
|
38 |
+
#submitButton("Submit"),
|
39 |
+
actionButton("update", "Someter los valores"),
|
40 |
+
br(),
|
41 |
+
img(src="logo.png", height=200, width=200)
|
42 |
+
|
43 |
+
),
|
44 |
+
|
45 |
+
# Show a plot of the generated distribution
|
46 |
+
mainPanel(
|
47 |
+
plotOutput("my_plot"),
|
48 |
+
h4("Tabla con las respuestas sometidas:"),
|
49 |
+
tableOutput("example")
|
50 |
+
)
|
51 |
+
)
|
52 |
+
))
|