-
Notifications
You must be signed in to change notification settings - Fork 0
/
Normality_test.R
246 lines (198 loc) · 8.5 KB
/
Normality_test.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
# This application allows to evaluate the normality of a variable of a data frame.
# The data frame must be loaded on the first tab "Upload data". After that,
# a variable must be selected from the frame in the "Select column" tab.
# The following tabs help to decide whether the variable is normally
# distributed.
# the application is dependent on the following packages:
if(!require(moments)){
install.packages("moments")
library(moments)
}
if(!require(shiny)){
install.packages("shiny")
library(shiny)
}
# Define UI for application
ui <- navbarPage(
# Application title
"Normality evaluation",
# Application is subset in tabs
# tab #1: load data
tabPanel("1) Upload data",
fileInput("file",
"Select file for upload"),
checkboxInput("header", "Enable if the data has a header", TRUE),
radioButtons("sep", "What is the separator?",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = ","),
radioButtons("quote", "What is the quoting character?",
choices = c(None = "",
"Double Quote" = '"',
"Single Quote" = "'"),
selected = '"'),
),
# tab #2: Choose column
tabPanel("2) Select column",
conditionalPanel(
condition = is.null("input.file"),
textOutput("placeholder"),tags$hr(),),
actionButton("choice", "Please first click here to import column names"),
tags$hr(),
selectInput("columns", "Select a column to evaluate normality in further tabs", choices = NULL),
tableOutput("contents"),
tableOutput("Table_selected.col")),
# tab #3: illustrate data
tabPanel("3) Plot column",
sidebarPanel(
selectInput("trafo", "Data transformation (for this and all further tabs):",
c("None" = "none",
"log10(x)" = "log",
"log10(1+x)" = "log2",
"sqrt(x)" = "sqrt",
"asin(sqrt(x)) {range x from 0 to 1}" = "arcsin"
),
multiple = FALSE),
radioButtons("figure", "Choose figure", c("Histogram", "Boxplot")),
conditionalPanel(
condition = 'input.figure == "Histogram"',
numericInput(inputId="nbins",
label="Number of bins",
value=15))
),
mainPanel(
conditionalPanel(
condition = is.null("input.file"),
textOutput("placeholder2")),
conditionalPanel(
condition = "input.columns == ''",
textOutput("placeholder6")),
plotOutput("plot", width="500px", height = "500px"))),
# tab #4: QQ plot
tabPanel("4) QQ Plot",
conditionalPanel(
condition = is.null("input.file"),
textOutput("placeholder3")),
conditionalPanel(
condition = "input.columns == ''",
textOutput("placeholder7")),
plotOutput("qqplot", width="500px", height = "500px")),
# tab #5: Skewness and Kurtosis
tabPanel("5) Skewness and Kurtosis",
conditionalPanel(
condition = is.null("input.file"),
textOutput("placeholder4")),
conditionalPanel(
condition = "input.columns == ''",
textOutput("placeholder8")),
htmlOutput("text")),
# tab #6: Normality test
tabPanel("6) Shapiro-wilk test",
conditionalPanel(
condition = is.null("input.file"),
textOutput("placeholder5")),
conditionalPanel(
condition = "input.columns == ''",
textOutput("placeholder9")),
htmlOutput("test"))
)
# Define server logic
server <- function(input, output, session) {
# required for loading data from file
data <- reactive({
req(input$file)
userdata <- read.csv(input$file$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
return(userdata)
})
# required for drawing figure
output$plot <- renderPlot({
c <- column()
c <- subset(c, select = input$columns)
if(input$trafo == "none") {}
else if(input$trafo == "log") {c <- log10(c)}
else if(input$trafo == "log2") {c <- log10(1+c)}
else if(input$trafo == "arcsin") {c <- asin(sqrt(c))}
else if(input$trafo == "sqrt") {c <- sqrt(c)}
if(input$figure == "Histogram") { hist(c[[1]], main="", input$nbins, xlab=input$columns)}
if(input$figure == "Boxplot") { boxplot(c[[1]], main="", xlab=input$columns)
}
})
# required for drawing QQ Plot
output$qqplot <- renderPlot({
c <- column()
c <- subset(c, select = input$columns)
if(input$trafo == "none") {}
else if(input$trafo == "log") {c <- log10(c)}
else if(input$trafo == "log2") {c <- log10(1+c)}
else if(input$trafo == "arcsin") {c <- asin(sqrt(c))}
else if(input$trafo == "sqrt") {c <- sqrt(c)}
qqnorm(c[[1]])
qqline(c[[1]], col="blue", lwd=2)
})
# required for table
output$contents <- renderTable({
req(data())
data()
})
# requred for choosing column
column <- eventReactive(input$choice, {
req(input$file)
userdata <- read.csv(input$file$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
vars <- names(userdata)
updateSelectInput(session, "columns", "Select a column to evaluate normality in further tabs", choices = vars)
userdata
})
output$Table_selected.col <- renderTable({
c <- column()
c <- subset(c, select = input$columns)
return(NULL)
})
# required for Skewness and Kurtosis
output$text <- renderText({
c <- column()
c <- subset(c, select = input$columns)
if(input$trafo == "none") {}
else if(input$trafo == "log") {c <- log10(c)}
else if(input$trafo == "log2") {c <- log10(1+c)}
else if(input$trafo == "arcsin") {c <- asin(sqrt(c))}
else if(input$trafo == "sqrt") {c <- sqrt(c)}
HTML("Skewness: ",skewness(c[[1]]),
"<br> Kurtosis: ", kurtosis(c[[1]]))
})
# required for normality test
output$test <- renderText({
c <- column()
c <- subset(c, select = input$columns)
if(input$trafo == "none") {}
else if(input$trafo == "log") {c <- log10(c)}
else if(input$trafo == "log2") {c <- log10(1+c)}
else if(input$trafo == "arcsin") {c <- asin(sqrt(c))}
else if(input$trafo == "sqrt") {c <- sqrt(c)}
s <- shapiro.test(c[[1]])
W <- gsub("W = c\\(", "", s[[1]])
HTML("W = ", W, "<br> p = ", s[[2]])
})
# remind user to upload data
output$placeholder <- output$placeholder2 <- output$placeholder3 <- output$placeholder4 <- output$placeholder5 <-renderText({
ifelse(is.null(input$file), yes = "Please upload data in tab 1", no = "") })
# remind user to choose column
output$placeholder6 <- output$placeholder7 <- output$placeholder8 <- output$placeholder9 <-renderText({
ifelse(input$columns == "", yes = "Please select a column in tab 2", no = "") })
}
# Run the application
shinyApp(ui = ui, server = server)