-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtele-assistance-system.Rmd
103 lines (93 loc) · 3.28 KB
/
tele-assistance-system.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
---
title: "Tele Assistance System Script"
author: "Rebekka Wohlrab"
date: ""
output:
pdf_document: default
html_document: default
---
```{r Init, include=FALSE}
library(ggfortify)
library(dplyr)
library(ggforce)
library(ggrepel)
library(dplyr)
library(janitor)
library(tidyr)
library(tidyverse)
library(rpart)
library(rpart.plot)
library(factoextra)
data.frames <- list()
data.cat.frames <- list()
```
```{r ReadFromFiles, include=FALSE}
list_raw <- list.files("/TAS", pattern = "^tas.*.\\.csv$", full.names = TRUE)
data.raw <- lapply(list_raw, read.csv, sep = "\t", header = TRUE, stringsAsFactors = TRUE, row.names = NULL, check.names = FALSE)
frame <- NULL
```
Normalization
```{r normalize, include=FALSE}
index <-0
for (i in data.raw) {
index <- index + 1
frame <- data.raw[[index]]
metricvars <- grep("\\_FR|\\_C|\\_RT",colnames(frame))
betweenness <- grep("^\\[B\\]",colnames(frame))
frame <- frame[,-c(metricvars,betweenness)]
frame_normalized <- frame[,-1]
frame_sums <- (rowSums(frame_normalized))
frame_min <- apply(frame_normalized,2, min)
frame_area_norm <- sweep(frame_normalized, 1, frame_sums, "/")
frame_min <- (apply(frame_area_norm,1, min))
baseline_offset <- sweep(frame_area_norm, 1, frame_min, "-")
data.frames[[index]] <- baseline_offset
cat_frame <- data.raw[[index]]
names(cat_frame)[names(cat_frame) == ""] <- "sample"
timeoutvars <- grep("TIMEOUT",colnames(cat_frame))
metricvars <- grep("\\_FR|\\_C|\\_RT",colnames(cat_frame))
betweenness <- grep("^\\[B\\]",colnames(cat_frame))
bindingsComponents <- grep("^\\[C\\]",colnames(cat_frame)) # yes, no
numVars <- c(2:4, timeoutvars, betweenness)
cat_frame[,numVars] <- mutate_all(cat_frame[,numVars], function(x) as.numeric(as.character(x)))
cat_frame <- cat_frame[,-c(metricvars,betweenness)]
cat_frame[,-numVars][cat_frame[,-numVars] == 1] <- "exists"
cat_frame[,-numVars][cat_frame[,-numVars] == 0] <- "does not exist"
data.cat.frames[[index]] <- cat_frame
}
```
PCA
```{r PCAall, echo = FALSE, fig.dim = c(10,8)}
index <-0
for (i in data.raw) {
index <- index + 1
dat <- data.frames[[index]]
pca <- prcomp(dat, center = TRUE, scale = FALSE)
all_cor <- cor(dat, pca$x)
cor <- all_cor[abs(all_cor[,1])>0.4 | abs(all_cor[,2]) > 0.4,]
options(ggrepel.max.overlaps = Inf)
p1<-ggplot(cor,aes(x=PC1,y=PC2, label=row.names(cor) )) +
geom_ellipse(aes(x0 = 0, y0 = 0, a = 1, b = 1, angle = 0)) +
geom_ellipse(aes(x0 = 0, y0 = 0, a = 0.70710678118655, b = 0.70710678118655, angle = 0)) +
labs(x = paste("PC1 ",round(get_eigenvalue(pca)$variance.percent[1],digits = 2), "%"), y = paste("PC2 ",round(get_eigenvalue(pca)$variance.percent[2], digits=2), "%"))
p1<-p1+geom_point()+
geom_text_repel(aes(x = PC1,
y = PC2,
label = row.names(cor) ))
ggsave(p1,filename=paste("TAS/TAS_PCA_",index,".pdf",sep=""), width=13, height=6)
}
```
```{r DTL, fig.width=16, echo = FALSE}
index <-0
for (i in data.raw) {
index <- index + 1
df <- data.cat.frames[[index]]
model3 <- rpart(
reliability ~ .,
data = df[,-c(1:3)],
control = rpart.control(minsplit = 2))
pdf(file=paste("TAS/TAS_decision_tree_rel_",index,".pdf",sep=""), width = 7, height = 3)
rpart.plot(model3, type = 1, fallen.leaves= TRUE, tweak = 1.1)
dev.off()
}
```