Data Ingestion
setwd("/Users/joergheintz/Documents/06_Projects/01_SIAM_Jump")
df<-read.xlsx("df_jump.xlsx", 2)[,c(2, 3)]
mydata<-df[513:1024,]
x<-mydata$x
y<-mydata$v.def
colnames(mydata)<-c("x", "y")
Detect Slope, Curve Turning Points and Add Knots
for (i in 1:length(mydata$y)){
a <- mydata[i,]$y
b <- mydata[i+1,]$y
if (!is.na(b)) {
if (a>b) mydata$mSL[i] = 0
if (a<b) mydata$mSL[i] = 1
}
}
#for (i in 1:length(mydata$y)){
k = length(mydata$y)
i=1
b=1
while ((i < k) & !is.na(b)) {
a <- mydata[i,]$mSL
b <- mydata[i+1,]$mSL
if ( a==1 & !b == a) {
mydata[i+1, ]$mSL = 1
helper<-mydata[i+1, ]
helper$mSL<-0
mydata <- rbind(mydata, helper)
mydata<-mydata[order(mydata$x),]
k = length(mydata$y)
i=i+1
}
if ( a==0 & !b == a) {
mydata[i+1, ]$mSL = 0
helper<-mydata[i+1, ]
helper$mSL<-1
mydata <- rbind(mydata, helper)
mydata<-mydata[order(mydata$x),]
k = length(mydata$y)
i=i+1
}
i=i+1
}
mydata<-mydata[order(mydata$x),]
Determine pos/neg Slope / Group Points to Slopes
# marking positive and negative slope
mydata$gradient <- 1
mydata[mydata$mSL < 1,"gradient"]<- -1
# cluster
n=0
flipflop = 1
for (i in 1:length(mydata$y)){
if (mydata$gradient[i] == 1) {
if (flipflop == 1) {n = n + 1}
mydata$cluster[i]<-n
flipflop=0
}
if (mydata$gradient[i] == -1) {
if (flipflop == 0) {n = n + 1}
mydata$cluster[i]<-n
flipflop=1
}
}
#Calculating the slope
xmin<-as.data.frame(tapply(mydata$x, mydata$cluster, min))
xmax<-as.data.frame(tapply(mydata$x, mydata$cluster,max))
ymin<-as.data.frame(tapply(mydata$y, mydata$cluster, min))
ymax<-as.data.frame(tapply(mydata$y, mydata$cluster, max))
sl_fac<-as.data.frame(tapply(mydata$gradient, mydata$cluster, sum))
myLines<-data.frame()
myLines<-cbind(xmin, xmax, ymin, ymax, sl_fac)
colnames(myLines)<-c("xmin", "xmax","ymin","ymax", "sl_fac")
myLines[myLines$sl_fac < 0, c("ymin", "ymax")] <- myLines[myLines$sl_fac < 0, c("ymax", "ymin")]
myLines$dx<-myLines$xmax - myLines$xmin
myLines$dy<-myLines$ymax - myLines$ymin
myLines$slope<-myLines$dy/myLines$dx*myLines$sl_fac/abs(myLines$sl_fac)
## peaks
peaks<-myLines[order(myLines$dy),][1:10,]
Determine max Segment Changes
# Determining the largest segment jump
mydata$ID<-1:length(mydata$y)
for (i in 1:length(mydata$y)){
a <- mydata[i,]$y
b <- mydata[i+1,]$y
if (!is.na(b)) {
# if (a>b) mydata$mSL[i] = 0
# if (a<b) mydata$mSL[i] = 1
mydata$yi1_minus_yi[i+1] = b-a
}
}
# Top 10 segment jumps
segjumps1<-mydata[order(mydata$yi1_minus_yi), ][1:10,]
segjumps2<-mydata[segjumps1$ID-1 %in% mydata$ID ,]
segjumps2<-segjumps2[ ,c("x", "y", "ID")]
colnames(segjumps1) <-c("xmax", "ymax","mSL", "gradient", "cluster","yi1_minus_yi","ID")
colnames(segjumps2) <-c("xmin", "ymin","ID")
segments<-cbind(segjumps1, segjumps2)
# Linear Regression
slope2of2<-lm(formula = df$v.def[513:1024] ~ df$x[513:1024], data = df)$coeff
Plot
- Curve
- Segments
- Peaks
- Regression Line
p <- ggplot(mydata, aes(x, y))
p = p + geom_segment(aes(x = xmin, y = ymin, xend = xmax, yend = ymax, colour = "line"), colour="lightblue", data = myLines)
p = p + geom_point(colour="blue", size = 0.02)
p

p = p + geom_segment(aes(x = xmin, y = ymin, xend = xmax, yend = ymax, colour = "peaks"), colour="green", data = peaks)
p = p + geom_segment(aes(x = xmin, y = ymin, xend = xmax, yend = ymax, colour = "segments"), colour="red", data = segments)
p

p = p + geom_abline(intercept = slope2of2[1], slope =slope2of2[2], colour = "salmon", linetype = 2)
#p = p + geom_line(colour="black", size = 0.3)
p
