-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
123 lines (102 loc) · 3.93 KB
/
server.R
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
120
121
122
123
library(shiny)
function(input, output) {
getMdl <- reactive({
C <- input$codes
# Contrast codes
DT[, feed_NvsP := case_when(feedback == "N" ~ C[1, 1],
feedback == "P" ~ C[1, 2],
feedback == "A" ~ C[1, 3])]
DT[, feed_AvsNP := case_when(feedback == "N" ~ C[2, 1],
feedback == "P" ~ C[2, 2],
feedback == "A" ~ C[2, 3])]
mdl <- DT[, lm(note ~ feed_NvsP + feed_AvsNP)]
DT[, noteHat := predict(mdl)]
return(mdl)
})
output$mainPlot <- renderPlotly({
mdl <- getMdl()
B <- coef(mdl)
b0 <- round(B[1], 2)
b1 <- round(B[2], 2)
b2 <- round(B[3], 2)
b3 <- 2
max.x1.x2 <- max(DT[, .(feed_NvsP, feed_AvsNP)]) + 1
min.x1.x2 <- min(DT[, .(feed_NvsP, feed_AvsNP)]) - 1
GG <- expand.grid(
x1 = seq(-10, 10, length.out = 40),
x2 = seq(-10, 10, length.out = 40)
) %>% data.table()
GG[, y := cbind(1, as.matrix(GG)) %*% B]
z <- spread(GG, key = x2, value = y) %>% .[, 2:ncol(.)] %>% as.matrix %>% t
# Plot model and data
fig <- plot_ly()
fig <- fig %>% add_surface(
x = unique(GG$x1), y = unique(GG$x2), z = z,
colors = c("#006400", "#458B00"),
showscale = FALSE,
opacity = .5,
hoverinfo = "skip",
contours = list(
x = list(show = FALSE,
highlight = input$projectX2, #
highlightcolor = "red",
highlightwidth = 5,
color = "azure"),
y = list(show = FALSE,
highlight = input$projectX1, #
highlightcolor = "red",
highlightwidth = 5,
color = "azure"),
z = list(show = FALSE,
highlight = FALSE)
))
fig <- fig %>% add_markers(
x = DT$feed_NvsP, y = DT$feed_AvsNP, z = DT$note,
hoverinfo = "skip",
marker = list(size = 5,
color = "#1874CD",
opacity = .5))
fig <- fig %>% add_markers(
x = DT$feed_NvsP, y = DT$feed_AvsNP, z = DT$noteHat,
hoverinfo = "skip",
marker = list(size = 5,
color = "green",
opacity = .7))
# fig <- fig %>% add_trace(x = 1:3, y = 1:3, z = 0,
# type = "scatter3d",
# mode = "lines",
# hoverinfo = "skip",
# line = list(color = "black", width = 5))
fig <- fig %>% layout(
title = sprintf(
"note = %s %s %sfeed_NvsP %s %sfeed_AvsNP",
b0, ifelse(b1>=0, "+", "-"), abs(b1), ifelse(b2>=0, "+", "-"), abs(b2)),
scene = list(
xaxis = list(title = "feed_NvsP",
titlefont = list(color = "rgb(153, 0, 0)"),
tickfont = list(color = "grey"),
showspikes = FALSE,
range = c(min.x1.x2, max.x1.x2)),
yaxis = list(title = "feed_AvsNP",
titlefont = list(color = "rgb(153, 0, 0)"),
tickfont = list(color = "grey"),
showspikes = FALSE,
range = c(min.x1.x2, max.x1.x2)),
zaxis = list(title = "note",
titlefont = list(color = "rgb(153, 0, 0)"),
tickfont = list(color = "grey"),
showspikes = FALSE,
range = c(7, 20)),
camera = list(eye = list(x = .5, y = -2, z = 1.25))),
showlegend = FALSE)
fig
})
output$mdlSummary <- renderPrint({
mdl <- getMdl()
summary(mdl)
})
output$descriptives <- renderPrint({
print(DT[, .(M = round(mean(note), 2),
SD = round(sd(note), 2)), .(feedback)])
})
}