-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
204 lines (179 loc) · 6.91 KB
/
app.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
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
library(shiny)
library(dplyr)
library(faraway)
library(car)
library(leaps)
ui <- fluidPage(
# App title ----
titlePanel("Regression Diagnostics"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select a file ----
fileInput("file", "Dataset (.csv)",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
tags$hr(),
# Input: Checkbox if file has header ----
checkboxInput("header", "Header", TRUE),
# Input: Select separator ----
radioButtons("sep", "Separator",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = ","),
tags$hr(),
uiOutput("responsechoice"),
uiOutput("predictorschoice")
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Tabset w/ plot, summary, and table ----
tabsetPanel(type = "tabs",
tabPanel("Summary",
fluidRow(
column(width = 12, sprintf("If no dataset is provided, the savings dataset from the faraway package is used."))
),
fluidRow(
column(width = 12, verbatimTextOutput("summary"))
)
),
tabPanel("Standard Diagnostics",
fluidRow(
column(width = 12, sprintf("What plot(lm...) yields."))
),
fluidRow(
column(width = 12, plotOutput("standardplot"))
)
),
tabPanel("Outlier Detection",
withMathJax(),
sprintf("Cook's distance measures how the fitted values change when the ith observation is deleted, in essence how much \\(\\hat{\\beta}\\) and \\(\\hat{\\beta}_{[i]}\\) differ.
If observation i has a large \\(C_i\\) it is probably an outlier."),
sprintf("$$C_i = \\frac{\\left(\\hat \\beta - \\hat \\beta_{[i]}\\right)^T\\left(X^TX\\right)\\left(\\hat \\beta - \\hat \\beta_{[i]}\\right)}{\\left(p+1\\right)\\hat{\\sigma}^2} = \\frac{r_i^2}{p+1} \\frac{h_i}{1−h_i}$$"),
fluidRow(
column(width = 12, plotOutput("leverage")),
column(width = 12, plotOutput("cooks"))
)),
tabPanel("Nonlinearity",
fluidRow(
column(width = 12, sprintf("Parital residual plots for 4 random predictors."))
),
fluidRow(
column(width = 12, plotOutput("partialres"))
)
),
tabPanel("Feature Selection",
fluidRow(
column(width = 12, sprintf("Backwards elimination with p-value cutoff of 0.15."))
),
fluidRow(
column(width = 12, verbatimTextOutput("features"))
)),
tabPanel("Table", DT::dataTableOutput("table"))
)
)
)
)
# Define server logic for random distribution app ----
server <- function(input, output) {
#Update table and linear model
df <- reactive({
if(is.null(input$file)) {
savings
} else {
read.csv(input$file$datapath, header = input$header, sep = input$sep)
}
})
linearmodel <- reactive({
resp <- input$response
pred <- input$predictors
columns <- c(resp, pred)
filtered <- df() %>% select(columns)
# If no predictors are specified, regress on the intercept term alone.
if(length(columns) == 1) {
formula <- paste(resp, "~", "1")
model <- lm(data = filtered, formula)
}
# Else regress on every predictor.
else {
vars_concat <- paste(pred, collapse = "+")
formula <- paste(resp, "~", vars_concat)
model <- lm(data = filtered, formula)
}
#This allows me to access the table, or the model via list indexing.
list(table = df(), model = model)
})
output$responsechoice <- renderUI({
columns <- colnames(df())
selectInput("response", "Choose Response variable", columns)
})
output$predictorschoice <- renderUI({
columns <- colnames(df())
resp <- input$response
allowed_columns <- columns[!(columns %in% resp)]
checkboxGroupInput("predictors", "Choose Predictors", columns, selected = allowed_columns)
})
output$summary <- renderPrint({
summary(linearmodel()$model)
})
output$standardplot <- renderPlot({
par(mfrow=c(2,2))
plot(linearmodel()$model)
})
output$leverage <- renderPlot({
leverages <- influence(linearmodel()$model)$hat
rowname <- rownames(linearmodel()$table)
plot(leverages, main = 'Leverage')
abline(h = mean(leverages))
leverages.sorted <- sort(leverages, decreasing=T, index.return=T)
for(i in leverages.sorted$ix[1:2]) {
text(i, leverages[i]-0.02, rowname[i])
}
})
output$cooks <- renderPlot({
cooks <- cooks.distance(linearmodel()$model)
rowname <- rownames(linearmodel()$table)
plot(cooks, main = 'Cooks Distance')
abline(h = mean(cooks))
cooks.sorted <- sort(cooks, decreasing=T, index.return=T)
for(i in cooks.sorted$ix[1:2]) {
text(i, cooks[i]-0.02, rowname[i])
}
})
output$partialres <- renderPlot({
selected_predictors <- sample(input$predictors, 4)
par(mfrow = c(2,2))
#Select random 4 predictors to examine
crPlot(linearmodel()$model, variable = selected_predictors[1])
crPlot(linearmodel()$model, variable = selected_predictors[2])
crPlot(linearmodel()$model, variable = selected_predictors[3])
crPlot(linearmodel()$model, variable = selected_predictors[4])
})
#Backward eliniation feature selection.
output$features <- renderPrint({
best_model <- paste(input$response, "~.")
current <- lm(best_model, data = df())
cutoff <- 0.15
repeat {
max_p <- max(summary(current)$coefficients[,4])
if (max_p < cutoff) {
break
} else {
eliminated <- names(which.max(summary(current)$coefficients[,4]))
best_model <- paste0(best_model, "- ", eliminated)
current <- lm(best_model, data = df())
}
}
best_backward <- formula(current)
summary(current)
})
output$table <- DT::renderDataTable({
DT::datatable(linearmodel()$table)
})
}
# Create Shiny app ----
shinyApp(ui, server)