-
Notifications
You must be signed in to change notification settings - Fork 0
/
EDA_v3.Rmd
244 lines (200 loc) · 8.73 KB
/
EDA_v3.Rmd
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
---
title: "EDA"
author: "James A. Coppock, Cyrus Tanade, Nicole Zimmer"
date: "9/25/2020"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## BME 590: Fall Detection
```{r cars}
#setwd("~/Desktop/Duke/Class/Fall 2020/Biomedical DS/BME590_Fall_Detection")
#install.packages("Rcpproll")
#install.packages("slider")
library(RcppRoll)
library("tidyverse")
library(slider)
library(tibble)
library(purrr)
library(lubridate, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
library(readxl)
library(evobiR)
library(patchwork)
library(gridExtra)
library(moments)
library(corrplot)
library(corrr)
library(seewave)
```
```{r}
Falls_df1 <- read.csv('Fall_Data1.csv')
Falls_df2 <- read.csv('Fall_Data2.csv')
Falls_df <- rbind(Falls_df1,Falls_df2)
rm(Falls_df1)
rm(Falls_df2)
#Falls_df[, c(2):c(ncol(Falls_df)-1)] <- scale(Falls_df[, c(2):c(ncol(Falls_df)-1)], center=TRUE, scale=TRUE) # standardizing
#Falls_df <- scale(Falls_df) #standardize
#colMeans(Falls_df) # faster version of apply(scaled.dat, 2, mean)
#apply(Falls_df, 2, sd)
Falls_df
```
See structure of dataset
```{r}
summary(Falls_df)
str(Falls_df)
sum(is.na(Falls_df)) # no missing values to impute!
```
## Including Plots
You can also embed plots, for example:
```{r}
p1 = ggplot(Falls_df, aes(x=time, y=accX)) + geom_boxplot()
p2 = ggplot(Falls_df, aes(x=time, y=angX)) + geom_boxplot()
p3 = ggplot(Falls_df, aes(x=time, y=maxX)) + geom_boxplot()
p4 = ggplot(Falls_df, aes(x=time, y=accY)) + geom_boxplot()
p5 = ggplot(Falls_df, aes(x=time, y=angY)) + geom_boxplot()
p6 = ggplot(Falls_df, aes(x=time, y=magY)) + geom_boxplot()
p7 = ggplot(Falls_df, aes(x=time, y=accZ)) + geom_boxplot()
p8 = ggplot(Falls_df, aes(x=time, y=angZ)) + geom_boxplot()
p9 = ggplot(Falls_df, aes(x=time, y=magZ)) + geom_boxplot()
(p1+p2+p3) / (p4+p5+p6) / (p7+p8+p9)
```
```{r , echo=FALSE}
cat('The Number of Falls in this Set of Data is:',(sum(Falls_df$Target)),'\n')
Fs <- 100
win <- 2
stp <- 1
window <-win*Fs
step <- stp*Fs
```
```{r}
test_fun <- function(x) {
max(x)
}
rms <- function(x){
sqrt(sum(x^2)/length(x))
}
q1 <- function(x){
quantile(x, 0.25)
}
q3 <- function(x){
quantile(x, 0.75)
}
rename <- function(a,b,c){
var_name <- paste(c, a, sep="_") # Construct the name
assign(var_name, b, env=.GlobalEnv) # Assign values to variable
}
variable_generate <- function(a){
arg_name <- deparse(substitute(a))
mean_var <- SlidingWindow("mean", a, window, step)
rename(arg_name,mean_var, "mean")
max_var <- SlidingWindow("max", a, window, step)
rename(arg_name,max_var, "max")
min_var <- SlidingWindow("min", a, window, step)
rename(arg_name,min_var, "min")
median_var <- SlidingWindow("median", a, window, step)
rename(arg_name,median_var, "median")
q1_var <- SlidingWindow("q1", a, window, step)
rename(arg_name,q1_var, "q1")
q3_var <- SlidingWindow("q3", a, window, step)
rename(arg_name,q3_var, "q3")
rms_var <- SlidingWindow(rms, a, window, step) # should we normalize everything beforehand???
rename(arg_name,rms_var, "rms")
std_var <- SlidingWindow("sd", a, window, step)
rename(arg_name,std_var, "std")
skew_var <- SlidingWindow("skewness", a, window, step)
rename(arg_name,skew_var, "skew")
kurtosis_var <- SlidingWindow("kurtosis", a, window, step)
rename(arg_name,kurtosis_var, "kurtosis")
}
#mean, standard deviation, RMS, max and min amplitude, Q1 median and Q3, skewness, kurtosis
#test<-roll_mean(Falls_df$accX,n =25,by = 12 )
Targets<-SlidingWindow("max", Falls_df$Target, window, step)
maxAccX<-SlidingWindow(max, abs(Falls_df$accX), window, step)
maxAccXTest<-SlidingWindow(test_fun, abs(Falls_df$accX), window, step)
maxAccY<-SlidingWindow("max", abs(Falls_df$accY), window, step)
maxAccZ<-SlidingWindow("max", abs(Falls_df$accZ), window, step)
#maxAccX[0:20]
#maxAccXTest[0:20]
#mean, standard deviation, RMS, max and min amplitude, Q1 median and Q3, skewness, kurtosis
Features<-data.frame(cbind(maxAccX,maxAccY,maxAccZ, Targets))
Features
#stats_AccX
# Feature Extraction
variable_generate(Falls_df$accX)
variable_generate(Falls_df$accY)
variable_generate(Falls_df$accZ)
variable_generate(Falls_df$angX)
variable_generate(Falls_df$angY)
variable_generate(Falls_df$angZ)
variable_generate(Falls_df$magY)
variable_generate(Falls_df$magZ)
```
```{r}
# Combine all features into a dataframe
AllFeatures <- data.frame(Targets, `mean_Falls_df$accX`, `max_Falls_df$accX`, `min_Falls_df$accX`,`median_Falls_df$accX`,`q1_Falls_df$accX`,
`q3_Falls_df$accX`,`rms_Falls_df$accX`,`std_Falls_df$accX`,`skew_Falls_df$accX`,`kurtosis_Falls_df$accX`,
`mean_Falls_df$accY`, `max_Falls_df$accY`, `min_Falls_df$accY`,`median_Falls_df$accY`,`q1_Falls_df$accY`,
`q3_Falls_df$accY`,`rms_Falls_df$accY`,`std_Falls_df$accY`,`skew_Falls_df$accY`,`kurtosis_Falls_df$accY`,
`mean_Falls_df$accZ`, `max_Falls_df$accZ`, `min_Falls_df$accZ`,`median_Falls_df$accZ`,`q1_Falls_df$accZ`,
`q3_Falls_df$accZ`,`rms_Falls_df$accZ`,`std_Falls_df$accZ`,`skew_Falls_df$accZ`,`kurtosis_Falls_df$accZ`,
`mean_Falls_df$angX`,`max_Falls_df$angX`,`min_Falls_df$angX`,`median_Falls_df$angX`,`q1_Falls_df$angX`,
`q3_Falls_df$angX`,`rms_Falls_df$angX`, `std_Falls_df$angX`,`skew_Falls_df$angX`,`kurtosis_Falls_df$angX`,
`mean_Falls_df$angY`,`max_Falls_df$angY`,`min_Falls_df$angY`,`median_Falls_df$angY`,`q1_Falls_df$angY`,
`q3_Falls_df$angY`,`rms_Falls_df$angY`,`std_Falls_df$angY`,`skew_Falls_df$angY`,`kurtosis_Falls_df$angY`,
`mean_Falls_df$angZ`,`max_Falls_df$angZ`,`min_Falls_df$angZ`,`median_Falls_df$angZ`,`q1_Falls_df$angZ`,
`q3_Falls_df$angZ`,`rms_Falls_df$angZ`,`std_Falls_df$angZ`,`skew_Falls_df$angZ`,`kurtosis_Falls_df$angZ`,
`mean_Falls_df$magY`,`max_Falls_df$magY`,`min_Falls_df$magY`,`median_Falls_df$magY`,`q1_Falls_df$magY`,
`q3_Falls_df$magY`,`rms_Falls_df$magY`,`std_Falls_df$magY`,`skew_Falls_df$magY`,`kurtosis_Falls_df$magY`,
`mean_Falls_df$magZ`,`max_Falls_df$magZ`,`min_Falls_df$magZ`,`median_Falls_df$magZ`,`q1_Falls_df$magZ`,
`q3_Falls_df$magZ`,`rms_Falls_df$magZ`,`std_Falls_df$magZ`,`skew_Falls_df$magZ`,`kurtosis_Falls_df$magZ`)
# lol please tell me there's a better way to do this ^
str(AllFeatures)
# Check that there are still 28 falls
cat('The Number of Falls in this Set of Data is:',(sum(AllFeatures$Targets)),'\n')
```
```{r}
# Correlation plot
colnames(AllFeatures) <- paste0("f", 1:ncol(AllFeatures))
col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
# use corrplot library
cor_df <- cor(AllFeatures)
corrplot(cor_df, method = "color", type = "lower", col=col(200),
diag=FALSE, order="hclust", tl.cex = 0.1)
```
```{r}
x <- AllFeatures %>%
correlate() %>%
focus(f1)
x
x %>%
mutate(rowname = factor(rowname, levels = rowname[order(f1)])) %>% # Order by correlation strength
ggplot(aes(x = rowname, y = f1)) +
geom_bar(stat = "identity") +
ylab("Correlation with Falls") +
xlab("Variable")
```
```{r , echo=FALSE}
time<-seq(from = 1/Fs, to =(length(Falls_df$accX)/Fs) , by = 1/Fs)
wintime<-seq(from = 1/100, to =(length(Features$maxAccX)/2) , by = 1/2)
p2<-ggplot(data=Features, aes(x = wintime, y=maxAccX, group=1)) +
geom_line(linetype = "solid") + labs(title = "Max X Acceleration in (2:0.5) Window")
p2
```
```{r}
yhat<-as.logical(Falls_df$Target)
fX<-Falls_df$accX[yhat]
fY<-Falls_df$accY[yhat]
fZ<-Falls_df$accZ[yhat]
X<-time[yhat]
feat<-data.frame(cbind(X,fX,fY,fZ))
p1<-ggplot(data=Falls_df, aes(x = time, y=accX, group=1)) +
geom_line(linetype = "solid") + labs(title = "X Acceleration") + geom_point(data=feat, aes(x = X, y=fX, group=2,color = 'Falls'))
p2<-ggplot(data=Falls_df, aes(x = time, y=accY, group=1)) +
geom_line(linetype = "solid") + labs(title = "Y Acceleration") + geom_point(data=feat, aes(x = X, y=fY, group=2,color = 'Falls'))
p3<-ggplot(data=Falls_df, aes(x = time, y=accZ, group=1)) +
geom_line(linetype = "solid") + labs(title = "Z Acceleration") + geom_point(data=feat, aes(x = X, y=fZ, group=2,color = 'Falls'))
p1/p2/p3
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.