-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
215 lines (193 loc) · 8.92 KB
/
server.R
File metadata and controls
215 lines (193 loc) · 8.92 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Linear Regression Model Explorer
library(shiny)
shinyServer(function(input, output, session) {
mydata <- reactive({
# input$mydatasetfile will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$mydatasetfile
message("inFile: ", inFile)
if (is.null(inFile)) return(NULL)
mydata <- read.csv(inFile$datapath, header=TRUE, sep=",", quote='"', nrows=-1)
})
ds <- reactive({
message("Selected dataset: ", input$dataset)
validate(need(input$dataset != "", "Please select a data set."))
if (input$dataset == "uploaded data") {
validate(need(nrow(mydata()) > 0, "Data set not available. Please upload your data set first or select a different data set from the list."))
ds <- mydata()
} else {
ds <- get(input$dataset)
}
if (!"data.frame" %in% class(ds)) return(NULL)
ds
})
# Basic data set meta info
output$dsmeta1 <- renderText({
ds <- ds()
s1 <- sprintf("Observations: %d", dim(ds)[1])
s2 <- sprintf("Variables: %d", dim(ds)[2])
s3 <- sprintf("Total NA values: %d\n", sum(is.na(ds)))
HTML(paste(s1, s2, s3, sep="<br/>"))
})
# NA values per variable
output$dsmeta2 <- renderTable({
ds <- ds()
data.frame(NA.values=as.integer(colSums(is.na(ds))))
})
# Data set summary statistics
output$dssummary <- renderTable({
ds <- ds()
summary(ds)
})
# Fitted linear regression model
fit.lm <- reactive({
modelVars <- c(input$predictors, input$interactions)
validate(need(!is.null(ds), "Data set not available. Please select a different data set."))
validate(need(length(modelVars) > 0, "Please select predictors"))
ds <- ds()
# Check if all response and predictor variables are in the model. When switching between data sets
# it may happen that the model does not yet reflect the names from the UI (transient situation).
if (!input$responseVar %in% names(ds)) return(NULL)
message("fit.lm: ", paste(input$responseVar, "~", modelVars))
if (input$intercept) intercept = "" else intercept="-1"
if (length(modelVars) > 0) {
lm(as.formula(paste(input$responseVar, "~", paste(modelVars, collapse="+"), intercept)),
data = ds)
} else return(NULL)
})
# Linear regression model formula
output$formula <- renderText({
modelVars <- c(input$predictors, input$interactions)
validate(
need(length(modelVars) > 0, "Please select predictors")
)
f <- as.character(formula(fit.lm()))
if (paste(f, collapse="") == "") return("Not specified.")
paste("Formula: ", f[2], f[1], f[3])
})
# Watch UI selections and dynamically update selection choices
observe({
message("observe:")
# Exclude response variable from choice of predictors
# Generate interaction combinations
ds <- ds()
choicesResponse <- names(ds)
choicesPredictors <- setdiff(names(ds), input$responseVar)
interactions <- t(combn(choicesPredictors, 2))
choicesInteractions <- paste(interactions[,1], interactions[,2], sep=":")
sel <- input$responseVar
if (!input$responseVar %in% choicesResponse) sel <- choicesResponse[1]
updateSelectInput(session, "responseVar", choices=choicesResponse
,selected=sel
)
updateCheckboxGroupInput(session, "predictors", choices=choicesPredictors)
updateCheckboxGroupInput(session, "interactions", choices=choicesInteractions)
})
# Data set exploratory plots
output$dsplot <- renderPlot({
ds <- ds()
if (!input$responseVar %in% names(ds)) return(NULL)
switch(input$dsplottype,
"1" = {
x <- ds[, input$responseVar]
# histogram of numeric variables, table of non-numeric variables
if (!is.numeric(x)) x <- as.integer(ds[, input$responseVar])
hist(x, xlab=input$responseVar, main=paste("Histogram of", input$responseVar))
},
"2" = {
boxplotVars <- c(input$responseVar, input$predictors)
# boxplot numeric variables only, convert non-numeric to integer
x <- as.data.frame(ds[, boxplotVars])
xnum <- sapply(x, function(y) is.numeric(y))
x1 <- data.frame(x[,xnum, drop=FALSE])
x2 <- sapply(x[,!xnum, drop=FALSE], as.numeric)
if (sum(xnum) > 0)
if (sum(!xnum) > 0) x <- cbind(x1, x2) else x <- x1
else
x <- x2
boxplot(x, plot=TRUE)
if (length(input$predictors) == 0) axis(side = 1, at = 1, labels = input$responseVar)
}
)
})
# Linear regression model diagnostic plots
output$modelplot <- renderPlot({
modelVars <- c(input$predictors, input$interactions)
validate(
need(length(modelVars) > 0, "\nPlease select predictors")
)
# Here we use some third party code to extend the functionality of the pairs plot
# panel extensions for pairs function
# source: R Documentation, pairs {graphics}
panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- abs(cor(x, y))
txt <- format(c(r, 0.123456789), digits = digits)[1]
txt <- paste0(prefix, txt)
if(missing(cex.cor)) cex.cor <- 0.6/strwidth(txt)
text(0.5, 0.5, txt, cex = cex.cor * r)
}
panel.hist <- function(x, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(usr[1:2], 0, 1.5) )
h <- hist(x, plot = FALSE)
breaks <- h$breaks; nB <- length(breaks)
y <- h$counts; y <- y/max(y)
rect(breaks[-nB], 0, breaks[-1], y, col = "cyan", ...)
}
# source: http://stackoverflow.com/questions/15271103/how-to-modify-this-correlation-matrix-plot
# Didzis Elferts
panel.smooth<-function (x, y, col = "blue", bg = NA, pch = 18,
cex = 0.8, col.smooth = "red", span = 2/3, iter = 3, ...)
{
points(x, y, pch = pch, col = col, bg = bg, cex = cex)
ok <- is.finite(x) & is.finite(y)
if (any(ok))
lines(stats::lowess(x[ok], y[ok], f = span, iter = iter),
col = col.smooth, ...)
}
# End of third party code
ds <- ds()
modelVars <- c(input$responseVar, input$predictors,
unlist(strsplit(paste0("", input$interactions), split=":")))
modelVars <- modelVars[!duplicated(modelVars)]
switch(input$plottype,
"3" = pairs(ds[, modelVars], main = "Pairs plot",
lower.panel=panel.smooth, upper.panel=panel.cor, diag.panel=panel.hist),
"4" = {
validate(need(is.numeric(ds[,input$responseVar]),
"\nFor proper linear regression select a numeric variable as response."))
par(mfrow=c(2,2)); plot(fit.lm(), which=c(1:3,5), cex=1.2); par(mfrow=c(1,1))
},
"5" = plot(predict(fit.lm()), residuals (fit.lm()),
xlab="Fitted values", ylab="Residuals", main="Residuals"),
"6" = {
validate(need(is.numeric(ds[, input$responseVar]),
"\nFor proper linear regression select a numeric variable as response."))
plot(hatvalues(fit.lm()), ylab = "Hat values", main="Hat values",
xlab="Observation nr", cex=1.2)
}
)
})
# Linear regression model summary
output$summary <- renderTable({
summary(fit.lm())
})
# Linear regression model confidence interval for coefficients
output$confint <- renderTable({
confint(fit.lm())
})
# Linear regression model statistics
output$stats <- renderTable({
data.frame(Statistic=c("Residual Std Err", "R squared", "Adj. R squared", "F-statistic",
"Numerator DF", " Denominator DF"),
Value=c(summary(fit.lm())$sigma, summary(fit.lm())$r.squared,
summary(fit.lm())$adj.r.squared, summary(fit.lm())$fstatistic))
})
})