├── an_igbt.R ├── an_fft.R ├── an_bearing_test.R ├── fig_resilience.R ├── startup.R ├── an_loationtest.R ├── fig_bearing.R ├── an_igbt_preprocess.R ├── an_igbt_density.R ├── an_turbofan_multistate.R ├── fig_common.R ├── fig_igbt_detrend_procedure.R ├── fig_igbt_detrend_procedure2.R ├── share_x_plot.R ├── fig_turbofan_phase.R ├── an_femto.R ├── an_window2.R ├── an_ge_main.R ├── fig_igbt_phase3.R ├── an_turbofan.R ├── an_ge_multistate.R ├── fig_igbt_phase2.R ├── fig_bearing_phase.R ├── fig_turbofan.R ├── fig_igbt_phase_variable.R ├── an_bearing_multistate.R ├── fig_igbt_phase.R ├── fig_igbt_detrend.R ├── an_ge_preprocess.R ├── an_indicators.R ├── an_bearing.R ├── fig_ge.R ├── fig_igbt.R ├── an_igbt_detrend_main.R ├── an_igbt_detrend.R ├── an_cmapss.R ├── an_igbt_multistate.R ├── an_ge.R ├── an_common.R └── an_sensitivity.R /an_igbt.R: -------------------------------------------------------------------------------- 1 | ## analysis with the original data set 2 | 3 | 4 | igbt_ = read.table("IIGBTAgingData.txt") 5 | collecter_current = igbt_[,2] 6 | work_series = collecter_current[collecter_current<=2] 7 | nearest_detrended = work_series[c(118980:length(work_series))] # just keep 30000 records 8 | nearest_original = collecter_current[c((length(collecter_current)-60000+1):length(collecter_current))] # coresponding,60000 records to compare 9 | alind2fitted = nearest_detrended[c(43:126)] 10 | 11 | -------------------------------------------------------------------------------- /an_fft.R: -------------------------------------------------------------------------------- 1 | convert.fft <- function(cs, sample.rate=1) { 2 | cs <- cs / length(cs) # normalize 3 | 4 | distance.center <- function(c)signif( Mod(c), 4) 5 | angle <- function(c)signif( 180*Arg(c)/pi, 3) 6 | 7 | df <- data.frame(cycle = 0:(length(cs)-1), 8 | freq = 0:(length(cs)-1) * sample.rate / length(cs), 9 | strength = sapply(cs, distance.center), 10 | delay = sapply(cs, angle)) 11 | return(df) 12 | } 13 | convert.fft(fft(1:4)) -------------------------------------------------------------------------------- /an_bearing_test.R: -------------------------------------------------------------------------------- 1 | # sect_1 = bearing_1_abs[1:19500] 2 | # sect_2 = bearing_1[19201:length(bearing_1)] 3 | # 4 | # win.graph() 5 | # plot(sect_1[1:(length(sect_1)-1)],sect_1[2:length(sect_1)]) 6 | # win.graph() 7 | # plot(sect_1[1:(length(sect_1)-1)],sect_1[2:length(sect_1)],type='b') 8 | 9 | ## linear smooth 10 | y = bearing_1_abs 11 | x = c(1:length(y)) 12 | lm.model = lm( y ~ x + I(x^2) + I(x^3) + I(x^4) + I(x^5)) 13 | win.graph() 14 | plot(lm.model$fitted.values) 15 | 16 | ## gaussian smooth 17 | bearing_gaus = smth.gaussian(bearing_1_abs,window = 5000) 18 | -------------------------------------------------------------------------------- /fig_resilience.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ## bearing 5 | bearing = read.csv("data/final_dataset/IMSBearingNo2_mean_1024.csv",header = TRUE) 6 | bearing_v1 = bearing$V1[1000:nrow(bearing)] # v1 failure without the noise data in the beginning 7 | 8 | win.graph(800,210) 9 | par(mar = c(3, 4, 3, 1),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 10 | tcl = 0.5) 11 | plot(bearing_v1,type='b', 12 | xlim=c(1,(length(bearing_v1)+round(length(bearing_v1)/3))), 13 | xlab='',ylab='') 14 | 15 | ## igbt collector_current detrended 16 | win.graph(800,210) 17 | par(mar = c(3, 4, 3, 1),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 18 | tcl = 0.5) 19 | plot(smoothed_series,type='b', 20 | xlim=c(1,(length(smoothed_series)+round(length(smoothed_series)/3))), 21 | xlab='',ylab='') 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /startup.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | library(snowfall) 4 | library(parallel) 5 | library(ggplot2) 6 | library(reshape2) # we need the function “melt” 7 | library(devtools) # not necessary 8 | library(plyr) # we need the ddply function in bootstrap_trend() operation 9 | library(deSolve) # we need the function “lsoda” 10 | library(psych) # we need the function “skew”, which has notified in the source file. 11 | library(forecast) 12 | library(Kendall) 13 | library(smoother) # Gaussian smoot 14 | 15 | 16 | ## If you want to source() a bunch of files, something like 17 | ## the following may be useful: 18 | sourceDir <- function(path, trace = TRUE, ...) { 19 | for (nm in list.files(path, pattern = "[.][RrSsQq]$")) { 20 | if(trace) cat(nm,":") 21 | source(file.path(path, nm), ...) 22 | if(trace) cat("\n") 23 | } 24 | } 25 | 26 | 27 | #sourceDir("earlywarning_toolbox/R") 28 | 29 | -------------------------------------------------------------------------------- /an_loationtest.R: -------------------------------------------------------------------------------- 1 | ## bearing 2 | # series = bearing_v1 3 | # section3 = bearing_v1[18001:length(bearing_v1)] 4 | # 5 | # len = length(section3) 6 | # section1 = bearing_v1[1:len] 7 | # section2 = bearing_v1[7001:(7001+len-1)] 8 | # 9 | # re1 = wilcox.test(section1,section2,paired = TRUE) 10 | # re2 = wilcox.test(section1,section3,paired = TRUE) 11 | # re3 = wilcox.test(section2,section3,paired = TRUE) 12 | # 13 | # print(paste(re1$p.value," ",re2$p.value," ",re3$p.value)) 14 | 15 | 16 | ## igbt 17 | series = igbt_COLLECTOR_CURRENT 18 | section3 = series[10001:length(series)] 19 | 20 | len = length(section3) 21 | section1 = series[1:len] 22 | section2 = series[5001:(5001+len-1)] 23 | 24 | re1 = wilcox.test(section1,section2,paired = TRUE) 25 | re2 = wilcox.test(section1,section3,paired = TRUE) 26 | re3 = wilcox.test(section2,section3,paired = TRUE) 27 | 28 | print(paste(re1$p.value," ",re2$p.value," ",re3$p.value)) 29 | -------------------------------------------------------------------------------- /fig_bearing.R: -------------------------------------------------------------------------------- 1 | bearing = read.csv("data/final_dataset/IMSBearingNo2_mean_1024.csv",header = TRUE) 2 | 3 | # v1 failure 4 | #bearing_v1 = bearing$V1 # v1 failure 5 | bearing_v1 = bearing$V1[1000:nrow(bearing)] # v1 failure 6 | #bearing_v1 = bearing$V1[1000:(nrow(bearing)-150)] # v1 failure 7 | #bearing_v1 = bearing$V1[18000:(nrow(bearing)-150)] # v1 failure 8 | windowsize = 0.5*length(bearing_v1) 9 | bearing_v1_indicators = get_indicators(index(bearing_v1)[windowsize:length(bearing_v1)], 10 | bearing_v1, 11 | windowsize = windowsize) 12 | plot_statistic_indicators(bearing_v1, 13 | bearing_v1_indicators, 14 | main = 'The Vibration Signal in the Bearing Failure Test [NASA PCoE Datasets]', 15 | xlab='Time', 16 | ylab='Measured Value') 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /an_igbt_preprocess.R: -------------------------------------------------------------------------------- 1 | 2 | seq_mean <- function(series,seq_list){ 3 | ret_series = c() 4 | for(i in c(2:length(seq_list))){ 5 | section = series[seq_list[i-1]:(seq_list[i]-1)] 6 | ret_series = append(ret_series,mean(section)) 7 | } 8 | section = series[seq_list[length(seq_list)]:(length(series))] 9 | ret_series = append(ret_series,mean(section)) 10 | } 11 | 12 | igbt_ = read.table("IIGBTAgingData.txt") 13 | sq_size = 10 14 | seq_list = seq(1,nrow(igbt_),sq_size) 15 | COLLECTOR_CURRENT = seq_mean(igbt_[,2],seq_list = seq_list) 16 | COLLECTOR_VOLTAGE = seq_mean(igbt_[,3],seq_list = seq_list) 17 | GATE_CURRENT = seq_mean(igbt_[,4],seq_list = seq_list) 18 | GATE_VOLTAGE = seq_mean(igbt_[,5],seq_list = seq_list) 19 | HEAT_SINK_TEMP = seq_mean(igbt_[,6],seq_list = seq_list) 20 | PACKAGE_TEMP = seq_mean(igbt_[,7],seq_list = seq_list) 21 | igbt_resample = data.frame(COLLECTOR_CURRENT,COLLECTOR_VOLTAGE,GATE_CURRENT, 22 | GATE_VOLTAGE,HEAT_SINK_TEMP,PACKAGE_TEMP) 23 | 24 | #write.csv(igbt_resample,"final_dataset/igbt_resample_mean_10.csv",row.names = FALSE) -------------------------------------------------------------------------------- /an_igbt_density.R: -------------------------------------------------------------------------------- 1 | series = timeseries 2 | section1 = series[1:2000] 3 | section2 = series[2001:4000] 4 | section3 = series[4001:length(series)] 5 | 6 | ## density plot 7 | 8 | density_sec1 = density(section1) 9 | density_sec2 = density(section2) 10 | density_sec3 = density(section3) 11 | 12 | color = c('black','blue','red') 13 | legend_text = c(sprintf("index from 1 to %g",length(section1)), 14 | sprintf("index from %g to %g",(length(section1)+1),(length(section1)+length(section2))), 15 | sprintf("index from %g to %g",(length(section1)+length(section2)+1),length(series))) 16 | 17 | dev.new() 18 | par(mar = c(3, 4, 3, 1),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 19 | tcl = 0.5) 20 | plot(density_sec1, 21 | xlim = c( min(series)-sd(series),max(series)+sd(series) ), 22 | ylim = c( 0, max(max(density_sec1$y),max(density_sec2$y),max(density_sec3$y))), 23 | xlab='', 24 | main='', 25 | col=color[1], 26 | lwd = 3) 27 | lines(density_sec2,col=color[2],lwd = 3) 28 | lines(density_sec3,col=color[3],lwd = 3) 29 | legend('topright',"(x,y)",legend_text, lty = c(1,1), lwd = c(2.5,2.5), col = color,bty='n',cex=1.5) -------------------------------------------------------------------------------- /an_turbofan_multistate.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ###### Fit Arima model 7 | # data [1:170] are used as the learning data set 8 | # base on the model we want to predict the points at [171:192] 9 | turbofan_arima_forecast <- function(series) 10 | { 11 | h = 22 12 | level = 95 13 | len = length(series) 14 | arima.model = arima(series[1:(len-h)]) 15 | predic.result = forecast.Arima(arima.model,h=h,level = level) 16 | win.graph(18,6) 17 | par(mar = c(3, 4, 3, 1),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 18 | tcl = 0.5) 19 | plot(series,type='b',xlab='Time',ylab='Measured Value',lwd=2) 20 | points(c((len-h+1):len),predic.result$mean,col='red',lwd=2) 21 | lines(c((len-h+1):len),predic.result$lower,col='black',lwd=2) 22 | lines(c((len-h+1):len),predic.result$upper,col='black',lwd=2) 23 | segments((len-h+1),predic.result$lower[1],(len-h+1),predic.result$upper[1],col='black',lwd=2) 24 | segments(len,predic.result$lower[h],len,predic.result$upper[h],col='black',lwd=2) 25 | } 26 | 27 | 28 | 29 | # load data 30 | #turbofan = read.csv("data/final_dataset/TurbofanEngine_trainfd001_uni1.csv",header = TRUE) 31 | 32 | 33 | #################### 34 | # fit arima model for variables: V19, V16, V17, V18, V25, V26 35 | # turbofan_arima_forecast(turbofan$v19) 36 | -------------------------------------------------------------------------------- /fig_common.R: -------------------------------------------------------------------------------- 1 | plot_statistic_indicators <- function(timeseries, 2 | indicators, 3 | main='original time series', 4 | xlab='time', 5 | ylab='measured value'){ 6 | 7 | win.graph(13,6) 8 | layout(matrix(c(1,1,1,2,3,4), 2, 3, byrow = TRUE)) 9 | par(mar = c(3, 4, 3, 2),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 10 | tcl = 0.5) 11 | plot(timeseries,type='b',main=main,xlab=xlab,ylab=ylab) 12 | 13 | # variance 14 | plot(indicators$time,indicators$variance,main='Variance',type='b',xlab='',ylab='') 15 | var_kendall = MannKendall(na.omit(indicators$variance)) 16 | mtext(sprintf("kendall = %.2f",var_kendall$tau[1]),side = 3,line = -5, adj = 0.1,cex = 1.5) 17 | 18 | # autocorrelation 19 | plot(indicators$time,indicators$autocorrelation,main='Autocorrelation(1)',type='b',xlab='',ylab='') 20 | auto_kendall = MannKendall(na.omit(indicators$autocorrelation)) 21 | mtext(sprintf("kendall = %.2f",auto_kendall$tau[1]),side = 3,line = -5, adj = 0.1,cex = 1.5) 22 | 23 | # skewness 24 | sk_kendall = MannKendall(na.omit(indicators$skewness)) 25 | plot(indicators$time,scale(indicators$skewness),main='Skewness ',type='b',xlab='',ylab='') 26 | mtext(sprintf("kendall = %.2f",sk_kendall$tau[1]),side =3,line = -5, adj = 0.1,cex = 1.5) 27 | 28 | } 29 | 30 | -------------------------------------------------------------------------------- /fig_igbt_detrend_procedure.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # the first two interval 5 | win.graph(80,20) 6 | par(mar = c(3, 4, 0.5, 0.5),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 0, 0, 0),mgp = c(2, 0.6, 0), 7 | tcl = 0.5) 8 | #plot(collecter_current[16:360],type='b') 9 | plot(collecter_current[76:450],type='b',xlab='Time',ylab='Current(A)') 10 | 11 | # corresponding series 12 | win.graph(80,20) 13 | par(mar = c(2.2, 2.2, 0.5, 0.5),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 0, 0, 0),mgp = c(2, 0.6, 0), 14 | tcl = 0.5) 15 | y1 = current_split_list[[2]] 16 | y2 = current_split_list[[3]] 17 | plot(append(y1,y2),xlab='',ylab='') 18 | 19 | t1 = index(y1) 20 | t2 = index(y2) 21 | size = 55 22 | start_i = 1 23 | end_i = start_i + size - 1 24 | y1_sub = y1[c(start_i:end_i)] 25 | y2_sub = y2[c(start_i:end_i)] 26 | t = c(1:size) 27 | 28 | coef_intercep =model_coef[1] 29 | coef_x = model_coef[2] 30 | 31 | y_fitted = exp(coef_x*t + coef_intercep) 32 | 33 | lines(t,y_fitted,col='red',lwd=2) 34 | lines(t+length(y1),y_fitted,col='blue',lwd=2) 35 | 36 | y1_residuals = y1_sub - y_fitted 37 | y2_residuals = y2_sub - y_fitted 38 | 39 | 40 | win.graph(80,20) 41 | # par(mar = c(2.2, 2.2, 0.5, 0.5),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 0, 0, 0),mgp = c(2, 0.6, 0), 42 | # tcl = 0.5) 43 | # plot(t1,y1_residuals,type='l',xlab='',ylab='',lwd=2,col='red',xlim=c(0,length(t))) 44 | # lines(t2,y2_residuals,type='l',xlab='',ylab='',lwd=2,col='blue') 45 | plot(append(y1_residuals,y2_residuals),type='b',xlab='',ylab='') 46 | abline(h=0,lty=2,lwd=2,col = "grey60") 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /fig_igbt_detrend_procedure2.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # the first two interval 5 | win.graph(24,6) 6 | par(mar = c(3, 4, 0.5, 0.5),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 0, 0, 0),mgp = c(2, 0.6, 0), 7 | tcl = 0.5) 8 | #plot(collecter_current[16:360],type='b') 9 | plot(collecter_current[76:450],type='b',xlab='Time',ylab='Current(A)') 10 | 11 | # corresponding series 12 | win.graph(24,6) 13 | par(mar = c(2.2, 2.2, 0.5, 0.5),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 0, 0, 0),mgp = c(2, 0.6, 0), 14 | tcl = 0.5) 15 | y1 = current_split_list[[2]] 16 | y2 = current_split_list[[3]] 17 | plot(append(y1,y2),xlab='',ylab='') 18 | 19 | t1 = index(y1) 20 | t2 = index(y2) 21 | 22 | coef_intercep =model_coef[1] 23 | coef_x = model_coef[2] 24 | 25 | y1_fitted = exp(coef_x*t1 + coef_intercep) 26 | y2_fitted = exp(coef_x*t2 + coef_intercep) 27 | 28 | lines(t1,y1_fitted,col='red',lwd=2) 29 | lines(t2+length(y1),y2_fitted,col='blue',lwd=2) 30 | 31 | y1_residuals = y1 - y1_fitted 32 | y2_residuals = y2 - y2_fitted 33 | 34 | 35 | win.graph(24,6) 36 | par(mar = c(2.2, 2.2, 2, 2),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 0, 0, 0),mgp = c(2, 0.6, 0), 37 | tcl = 0.5) 38 | plot(append(y1_residuals,y2_residuals),type='b',xlab='',ylab='') 39 | abline(h=0,lty=2,lwd=2,col = "grey60") 40 | 41 | win.graph(24,6) 42 | par(mar = c(2.2, 2.2, 2, 2),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 0, 0, 0),mgp = c(2, 0.6, 0), 43 | tcl = 0.5) 44 | len_noise = 5 45 | plot(append(y1_residuals[len_noise:length(y1_residuals)],y2_residuals[len_noise:length(y2_residuals)]), 46 | type='b',xlab='',ylab='') 47 | abline(h=0,lty=2,lwd=2,col = "grey60") 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /share_x_plot.R: -------------------------------------------------------------------------------- 1 | 2 | igbt_old = read.table("data/final_dataset/IIGBTAgingData.txt") 3 | 4 | 5 | igbt_COLLECTOR_CURRENT = igbt_old[,2] 6 | igbt_COLLECTOR_VOLTAGE = igbt_old[,3] 7 | igbt_GATE_CURRENT = igbt_old[,4] 8 | 9 | 10 | colnames = c('COLLECTOR_CURRENT','COLLECTOR_VOLTAGE','GATE_CURRENT','GATE_VOLTAGE','HEAT_SINK_TEMP','PACKAGE_TEMP') 11 | 12 | 13 | ## plot stable state 1 14 | win.graph(10,8) 15 | par(mfrow=c(3,1),mar = c(3, 3.5, 1, 2),cex.axis = 1.8, cex.lab = 1.8,cex.main=1.8,oma = c(0, 0, 0, 0),mgp = c(2, 0.6, 0), 16 | tcl = 0.5) 17 | rows = c(10000:15400) 18 | # plot(rows,igbt_COLLECTOR_CURRENT[rows],type='b',axes = FALSE,ylab='Collector Current',xlab='') 19 | # axis(2, col = "black", col.axis = "black") 20 | # box(col = "black") 21 | # 22 | # plot(rows,igbt_COLLECTOR_VOLTAGE[rows],type='b',axes = FALSE,ylab='Collector voltage',xlab='') 23 | # axis(2, col = "black", col.axis = "black") 24 | # box(col = "black") 25 | # 26 | # plot(rows,igbt_GATE_CURRENT[rows],type='b',axes = FALSE,ylab='Gate Current',xlab='Time') 27 | # axis(1, col = "black", col.axis = "black") 28 | # axis(2, col = "black", col.axis = "black") 29 | # box(col = "black") 30 | 31 | 32 | # time = rows 33 | # collecter_current = igbt_COLLECTOR_CURRENT[rows] 34 | # collecter_voltage = igbt_COLLECTOR_VOLTAGE[rows] 35 | # gate_current = igbt_GATE_CURRENT[rows] 36 | # dt = data.frame(time,collecter_current,collecter_voltage,gate_current) 37 | # library(reshape2) 38 | # mm <- melt(subset(dt, select=c(time,collecter_current,collecter_voltage,gate_current)),id.var="time") 39 | # library(lattice) 40 | # xyplot(value~time|variable,data=mm,type="l", 41 | # scales=list(y=list(relation="free")), 42 | # layout=c(1,3)) 43 | 44 | -------------------------------------------------------------------------------- /fig_turbofan_phase.R: -------------------------------------------------------------------------------- 1 | 2 | turbofan = read.csv("data/final_dataset/TurbofanEngine_trainfd001_uni1.csv",header = TRUE) 3 | turbofan_v19 = turbofan$V19 4 | turbofan_v19 = scale(turbofan_v19) 5 | win.graph(11,7) 6 | par(mar = c(3, 3, 1, 1),cex.axis = 2, cex.lab = 2,cex.main=2, 7 | oma = c(0, 0, 0, 0),mgp = c(1.5, 0.5, 0),tcl = 0.5) 8 | plot(turbofan_v19,type='b',xlab='',ylab='',lwd=2) 9 | abline(v=100,lty=2,col='grey60',lwd=7) 10 | 11 | subset1 = turbofan_v19[1:100] 12 | subset2 = turbofan_v19[101:length(turbofan_v19)] 13 | 14 | x1 = seq(min(subset1),max(subset1),0.05) 15 | x2 = seq(min(subset2),max(subset2),0.05) 16 | 17 | dx1 = 0.0092-0.0451*x1 18 | dx2 = 0.0131-0.0261*x2-0.0424*x2*x2 19 | 20 | win.graph(8,8) 21 | par(mar = c(3, 3, 1, 1),cex.axis = 2, cex.lab = 2,cex.main=2, 22 | oma = c(0, 0, 0, 0),mgp = c(1.5, 0.5, 0),tcl = 0.5) 23 | cols = c('blue','red') 24 | names = c('0.0092-0.0451*x','0.0131-0.0261*x-0.0424*x^2') 25 | plot(x1,dx1,xlim=c(-3.5,2.5),ylim = c(-0.4,0.1),col=cols[1],type='l',xlab='x',ylab='dx/dt',lwd=4) 26 | lines(x2,dx2,col=cols[2],lwd=4,type='l') 27 | points(0.2039911308203991130820399113082,0,col='blue', pch = 16,cex=1.5,lwd=2) 28 | points( -0.9431511411831667354914164365958,0,col='red',pch = 16, cex=1.5,lwd=2) 29 | points(0.32758510344731767888764285169014,0,col='red',pch=16,cex=1.5,lwd=2) 30 | abline(h=0,col='grey60',lty=2,lwd=2) 31 | abline(v=0,col='grey60',lty=2,lwd=2) 32 | legend("bottomright",names,pch = 1,lty=1,lwd=2,cex=2,col = cols) 33 | 34 | arrows(x1[40],dx1[40],x1[39],dx1[39],col=cols[1],lwd=2,length = 0.2,code = 2) 35 | arrows(x1[3],dx1[3],x1[4],dx1[4],col=cols[1],lwd=2,length = 0.2,code = 2) 36 | 37 | arrows(x2[40],dx2[40],x2[39],dx2[39],col=cols[2],lwd=2,length = 0.2,code = 2) 38 | arrows(x2[52],dx2[52],x2[53],dx2[53],col=cols[2],lwd=2,length = 0.2,code = 2) 39 | arrows(x2[83],dx2[83],x2[82],dx2[82],col=cols[2],lwd=2,length = 0.2,code = 2) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /an_femto.R: -------------------------------------------------------------------------------- 1 | 2 | ### read acc file 3 | root_dir = "data/FEMTOBearingDataSet/Learning_set/Bearing1_1" 4 | 5 | list.file.femto <- function (path){ 6 | filelist = list.files(path = path,pattern = ".csv") 7 | filelist_acc = filelist[grepl("acc_",filelist)] 8 | filelist_temp = filelist[grepl("temp_",filelist)] 9 | out = list(acc = filelist_acc,temp = filelist_temp) 10 | return(out) 11 | } 12 | 13 | readAccelByIntervl.femto <- function(path, from, to){ 14 | filelist = list.file.femto(path) 15 | filelist = filelist$acc 16 | if( from <=0 | to > length(filelist)){ 17 | print("error: the index is error") 18 | return() 19 | } 20 | horiz_accel = c() 21 | vert_accel = c() 22 | for (i in c(from:to)) { 23 | fname = filelist[i] 24 | fpath = sprintf("%s/%s",path,fname) 25 | data = read.csv(fpath,header = FALSE) 26 | horiz_accel = append(horiz_accel,data[,5]) 27 | vert_accel = append(vert_accel,data[,6]) 28 | } 29 | out = data.frame(horiz_accel,vert_accel) 30 | return(out) 31 | } 32 | 33 | resample.femto <- function(series,interval_size=64){ 34 | start = 1 35 | end = interval_size 36 | new.series = c() 37 | while(end<=length(series)){ 38 | new.series = append(new.series,mean(series[start:end])) 39 | start = start + interval_size 40 | end = end + interval_size 41 | } 42 | return(new.series) 43 | } 44 | 45 | denoise.femto <- function(series,winsize = 5000){ 46 | start=1 47 | end = winsize 48 | len = length(series) 49 | newseries = c() 50 | while(end<=len){ 51 | sub.set = series[start:end] 52 | sub.sd = var(sub.set) 53 | sub.mean = mean(sub.set) 54 | I = sub.set>(sub.mean+sub.sd) 55 | sub.set[I] = sub.mean + sub.sd 56 | I = sub.set<(sub.mean-sub.sd) 57 | sub.set[I] = sub.mean - sub.sd 58 | newseries = append(newseries,sub.set) 59 | start = start + winsize 60 | end = end + winsize 61 | } 62 | return(newseries) 63 | } 64 | -------------------------------------------------------------------------------- /an_window2.R: -------------------------------------------------------------------------------- 1 | 2 | #' resample data 3 | 4 | # ge_data_05_resample_10s = concate_data (root_dir ="GeDataNew", 5 | # pattern ="290-05", 6 | # resample = TRUE, 7 | # resample_size = 10) 8 | 9 | #' plot segment 10 | # segment_plot_series(ge_data_02_resample_10s$BPRate, 11 | # ge_data_02_resample_10s$Date_Time, 12 | # segsize=1000, 13 | # out_dir="GeDataNew/M02_BPRate_segment_plot_10s", 14 | # add_gaussian_line = FALSE) 15 | 16 | 17 | #' rolling analysis 18 | 19 | 20 | # ge_rolling_analysis(ge_data_05_resample_10s$BP, 21 | # ge_data_05_resample_10s$Date_Time, 22 | # windowsize = 25000, 23 | # methods=c('var','cv','mean','skew','kurtosi','autocorr'), 24 | # #methods=c('var','mean','skew','kurtosi','autocorr'), 25 | # output_dir = "GeDataNew/StatisticIndicators/M05", 26 | # segsize = 1000) 27 | # 28 | # # standardize 29 | # 30 | # BP = ge_data_03_resample_10s$BP 31 | # #BP = (BP - mean(BP))/sd(BP) 32 | # emd_result = emd(BP) 33 | 34 | # for( col in c(1:ncol(emd_result$imf))){ 35 | # data = emd_result$imf[,col] 36 | # win.graph() 37 | # plot(data,type='b') 38 | # abline(h=0,col="red") 39 | # win.graph() 40 | # sliding_window(data) 41 | # } 42 | 43 | sq_size = 60 44 | seq_mean <- function(series,seq_list){ 45 | ret_series = c() 46 | for(i in c(2:length(seq_list))){ 47 | section = series[seq_list[i-1]:(seq_list[i]-1)] 48 | ret_series = append(ret_series,mean(section)) 49 | } 50 | section = series[seq_list[length(seq_list)]:(length(series))] 51 | ret_series = append(ret_series,mean(section)) 52 | } 53 | 54 | # BP_05 = ge_data_05$BP 55 | # BP_05_mean = seq_mean(BP_05,seq(1,length(BP_05),sq_size)) 56 | series = ge_m345$BP 57 | series_mean = seq_mean(series,seq(1,length(series),sq_size)) 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /an_ge_main.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | # ge_move_1314 = read.csv("GEData_2state/move/290-0513-0514_move.csv",header = TRUE) 4 | # ge_move_1415 = read.csv("GEData_2state/move/290-0514-0515_move.csv",header = TRUE) 5 | # ge_move_1516 = read.csv("GEData_2state/move/290-0515-0516_move.csv",header = TRUE) 6 | # ge_move_1617 = read.csv("GEData_2state/move/290-0516-0517_move.csv",header = TRUE) 7 | # 8 | # ge_move_BPRate_13_17 = append(ge_move_1314$BPRate,ge_move_1415$BPRate) 9 | # ge_move_BPRate_13_17 = append(ge_move_BPRate_13_17,ge_move_1516$BPRate) 10 | # ge_move_BPRate_13_17 = append(ge_move_BPRate_13_17,ge_move_1617$BPRate) 11 | 12 | 13 | # ge_static_1314 = read.csv("GEData_2state/static/290-0513-0514_static.csv",header = TRUE) 14 | # ge_static_1415 = read.csv("GEData_2state/static/290-0514-0515_static.csv",header = TRUE) 15 | # ge_static_1516 = read.csv("GEData_2state/static/290-0515-0516_static.csv",header = TRUE) 16 | # ge_static_1617 = read.csv("GEData_2state/static/290-0516-0517_static.csv",header = TRUE) 17 | # 18 | # ge_static_BPRate_ = append(ge_static_1314$BPRate,ge_static_1415$BPRate) 19 | # ge_static_BPRate_ = append(ge_static_BPRate_,ge_static_1516$BPRate) 20 | # ge_static_BPRate_ = append(ge_static_BPRate_,ge_static_1617$BPRate) 21 | 22 | #' read 05 month data 23 | ge_move_m5 = c() 24 | root_dir ="data/GEData/2state_identification/5month/move" 25 | filelist = list.files(path = root_dir,pattern = ".csv") 26 | filelist = filelist[grepl("290-05",filelist)] 27 | for (fname in filelist){ 28 | ge_data = read.csv(sprintf("%s/%s",root_dir,fname),header = TRUE) 29 | print(sprintf("process %s and data size is %g",fname,nrow(ge_data))) 30 | ge_move_m5 = rbind(ge_move_m5,ge_data) 31 | } 32 | 33 | 34 | # ## read original data 35 | # ge_data_con_ = c() 36 | # root_dir ="GeDataNew" 37 | # filelist = list.files(path = root_dir,pattern = ".csv") 38 | # filelist = filelist[grepl("290-05",filelist)] 39 | # for (fname in filelist){ 40 | # ge_data = read.csv(sprintf("%s/%s",root_dir,fname),header = TRUE) 41 | # print(sprintf("process %s and data size is %g",fname,nrow(ge_data))) 42 | # ge_data_con_ = rbind(ge_data_con_,ge_data) 43 | # } 44 | # -------------------------------------------------------------------------------- /fig_igbt_phase3.R: -------------------------------------------------------------------------------- 1 | 2 | igbt_old = read.table("final_dataset/IIGBTAgingData.txt") 3 | 4 | igbt_COLLECTOR_CURRENT = igbt_old[,2] 5 | igbt_COLLECTOR_VOLTAGE = igbt_old[,3] 6 | igbt_GATE_CURRENT = igbt_old[,4] 7 | 8 | 9 | colnames = c('COLLECTOR_CURRENT','COLLECTOR_VOLTAGE','GATE_CURRENT','GATE_VOLTAGE','HEAT_SINK_TEMP','PACKAGE_TEMP') 10 | 11 | x = igbt_GATE_CURRENT[300000:301680] 12 | y = igbt_COLLECTOR_CURRENT[300000:301680] 13 | z = igbt_COLLECTOR_VOLTAGE[300000:301680] 14 | 15 | interval_size = 600 16 | total_len = length(x) 17 | split_num = as.integer(total_len/interval_size) 18 | coltype = c() 19 | for( i in c(1:split_num)){ 20 | sub_col = rep(i,interval_size) 21 | coltype = append(coltype,sub_col) 22 | } 23 | if(split_num*interval_size < total_len){ 24 | sub_col = rep(split_num+1,(total_len - (split_num*interval_size))) 25 | coltype = append(coltype,sub_col) 26 | } 27 | 28 | data = data.frame(x,y,z,coltype) 29 | 30 | win.graph(800,800) 31 | par(mar = c(5, 5, 5, 5),cex = 1,cex.axis = 1.5, cex.lab = 1.5,cex.main=1.5,oma = c(0, 0, 0, 0), 32 | mgp = c(1, 1.8, 0),tcl = 0.5) 33 | split_list = seq(1,total_len,interval_size) 34 | cols = c('black','blue','red','brown') 35 | col_i = 2 36 | sub_ = data[c(split_list[1]:(split_list[2]-1)),] 37 | lines3D(sub_$x,sub_$y,sub_$z,phi = 0,ticktype="detailed", 38 | xlab='GATE_CURRENT',ylab='COLLECTOR_CURRENT',zlab='COLLECTOR_VOLTAGE', 39 | col=cols[2], colvar=NULL,lwd=1.5) 40 | for( i in c(3:length(split_list)) ){ 41 | start_i = split_list[i-1] 42 | end_i = split_list[i] - 1 43 | sub_ = data[c(start_i:end_i),] 44 | 45 | lines3D(sub_$x,sub_$y,sub_$z,phi = 0,ticktype="detailed", 46 | xlab='GATE_CURRENT',ylab='COLLECTOR_CURRENT',zlab='COLLECTOR_VOLTAGE', 47 | col=cols[2], colvar=NULL,lwd=1.5,add=TRUE) 48 | 49 | col_i = (col_i+1)%%length(cols)+1 50 | 51 | } 52 | if(split_list[length(split_list)] < total_len){ 53 | start_i = split_list[length(split_list)] 54 | end_i = total_len 55 | sub_ = data[c(start_i:end_i),] 56 | 57 | lines3D(sub_$x,sub_$y,sub_$z,phi = 0,ticktype="detailed", 58 | xlab='GATE_CURRENT',ylab='COLLECTOR_CURRENT',zlab='COLLECTOR_VOLTAGE', 59 | col=cols[2], colvar=NULL,lwd=1.5,add=TRUE) 60 | } 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /an_turbofan.R: -------------------------------------------------------------------------------- 1 | 2 | work_dir = 'data/CMAPSSData' 3 | 4 | ## load data 5 | data = read.table(sprintf("%s/train_FD001.txt",work_dir)) 6 | unit = nasa[nasa$V1==1,] 7 | 8 | # constant_variables = c(5,6,10,11,15,21,23,24) 9 | # unit = unit[,-constant_variables] 10 | # var.name = colnames(unit) 11 | 12 | variables_leave = c(19,16,17,18,25,26) 13 | unit = unit[,variables_leave] 14 | var.name = colnames(unit) 15 | 16 | ########## plot gaussian density 17 | 18 | # for(i in c(1:length(unit))){ 19 | # out_dir = 'density_unit1' 20 | # win.graph(9,7) 21 | # # png_path = sprintf("%s/%s/%s.png",work_dir,out_dir,var.name[i]) 22 | # # png(filename = png_path,width = 2048,height = 768) 23 | # par(mar = c(3, 4, 3, 1),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 24 | # tcl = 0.5) 25 | # 26 | # key = 110 27 | # kernel = 'gaussian' 28 | # first.density = density(unit[1:key,i],kernel = kernel) 29 | # second.density = density(unit[(key+1):nrow(unit),i],kernel = kernel) 30 | # 31 | # color = c('blue','red') 32 | # legend_text = c(sprintf("index from 1 to %g",key), 33 | # sprintf("index from %g to the %g",(key+1),nrow(unit))) 34 | # plot( first.density, 35 | # xlim = c( (min(unit[,i])-sd(unit[,i])), (max(unit[,i])+sd(unit[,i])) ), 36 | # ylim = c( 0, max(max(first.density$y),max(second.density$y)) ), 37 | # main='', 38 | # col=color[1], 39 | # lwd=3, 40 | # xlab='') 41 | # lines(second.density,col=color[2],lwd=3) 42 | # # polygon(first.density,col = color[1], border = color[1]) 43 | # # polygon(second.density, col = color[2], border = color[2]) 44 | # legend("topleft","(x,y)",legend_text, lty = c(1,1), lwd = c(2.5,2.5), col = color,bty='n',cex=1.5) 45 | # #dev.off() 46 | # 47 | # } 48 | 49 | 50 | ### location test 51 | for(i in c(1:length(unit))){ 52 | series = unit[,i] 53 | section1 = series[1:30] 54 | section2 = series[31:80] 55 | section3 = series[81:length(series)] 56 | 57 | # section1 = series[1:50] 58 | # section2 = series[51:100] 59 | # section3 = series[101:length(series)] 60 | 61 | re1 = t.test(section1,section2) 62 | re2 = t.test(section1,section3) 63 | re3 = t.test(section2,section3) 64 | 65 | print(paste(re1$p.value," ",re2$p.value," ",re3$p.value)) 66 | } 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /an_ge_multistate.R: -------------------------------------------------------------------------------- 1 | # ge_move = ge_move_m5[ge_move_m5$BP>=350,] # just single point less than 350 2 | # bp_series_move_10 = ge_move$BP[ge_move$Speed>10] 3 | 4 | ## BP 5 | # series = bp_series_move_10 6 | # section1 = series[1:10000] 7 | # section2 = series[13500:15000] 8 | # section3 = series[19000:length(series)] 9 | 10 | ## BC 11 | # ge_move_10 = ge_move[ge_move$Speed>10,] 12 | # series = ge_move_10$BC 13 | # section1 = series[1:8000] 14 | # section2 = series[8001:16000] 15 | # section3 = series[16001:length(series)] 16 | 17 | ##### density plot 18 | 19 | # kernel = 'gaussian' 20 | # density_sec1 = density(section1,kernel = kernel) 21 | # density_sec2 = density(section2,kernel = kernel) 22 | # density_sec3 = density(section3,kernel = kernel) 23 | # 24 | # color = c('black','blue','red') 25 | # legend_text = c(sprintf("index from 1 to 8000"), 26 | # sprintf("index from 8001 to 16000"), 27 | # sprintf("index from 16001 to %g",length(series))) 28 | # win.graph(7,7) 29 | # par(mar = c(3, 4, 3, 1),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 30 | # tcl = 0.5) 31 | # plot(density_sec1, 32 | # xlim = c( min(series)-sd(series), max(series)+sd(series)), 33 | # ylim = c(0, max(max(density_sec1$y),max(density_sec2$y),max(density_sec3$y))), 34 | # main='', 35 | # col = color[1], 36 | # lwd = 3, 37 | # xlab='' 38 | # ) 39 | # lines(density_sec2,col=color[2],lwd=3) 40 | # lines(density_sec3,col=color[3],lwd=3) 41 | # legend("topleft","(x,y)",legend_text, lty = c(1,1), lwd = c(2.5,2.5), col = color,bty='n',cex=2) 42 | 43 | ###### arima fitted 44 | 45 | # tail_len = length(section3) 46 | # train_series = series[1:(length(series)-tail_len)] 47 | # arima.model = auto.arima(train_series) 48 | # print(summary(arima.model)) 49 | # predic.result = forecast.Arima(arima.model,h=tail_len,level = 95) 50 | # 51 | # dev.new() 52 | # par(mar = c(3, 4, 3, 1),cex.axis = 1.5, cex.lab = 1.5,cex.main=1.5,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 53 | # tcl = 0.5) 54 | # plot(series,xlab='Time',ylab='Measured Value',ylim=c(-50,max(series))) 55 | # points(c((length(train_series)+1):length(series)),predic.result$mean,col='red',lwd=4) 56 | # points( c((length(train_series)+1):length(series)), predic.result$lower, col='blue', lwd=4) 57 | # points(c((length(train_series)+1):length(series)), predic.result$upper, col='blue', lwd=4) -------------------------------------------------------------------------------- /fig_igbt_phase2.R: -------------------------------------------------------------------------------- 1 | 2 | igbt_old = read.table("final_dataset/IIGBTAgingData.txt") 3 | 4 | igbt_COLLECTOR_CURRENT = igbt_old[,2] 5 | igbt_COLLECTOR_VOLTAGE = igbt_old[,3] 6 | igbt_GATE_CURRENT = igbt_old[,4] 7 | 8 | 9 | colnames = c('COLLECTOR_CURRENT','COLLECTOR_VOLTAGE','GATE_CURRENT','GATE_VOLTAGE','HEAT_SINK_TEMP','PACKAGE_TEMP') 10 | 11 | x = igbt_COLLECTOR_CURRENT[10000:14800] 12 | y = igbt_GATE_CURRENT[10000:14800] 13 | z = igbt_COLLECTOR_VOLTAGE[10000:14800] 14 | 15 | interval_size = 600 16 | total_len = length(x) 17 | split_num = as.integer(total_len/interval_size) 18 | coltype = c() 19 | for( i in c(1:split_num)){ 20 | sub_col = rep(i,interval_size) 21 | coltype = append(coltype,sub_col) 22 | } 23 | if(split_num*interval_size < total_len){ 24 | sub_col = rep(split_num+1,(total_len - (split_num*interval_size))) 25 | coltype = append(coltype,sub_col) 26 | } 27 | 28 | data = data.frame(x,y,z,coltype) 29 | 30 | win.graph(800,800) 31 | lines3D(data$x,data$y,data$z,phi = 0,ticktype="detailed", 32 | xlab='GATE_CURRENT',ylab='COLLECTOR_CURRENT',zlab='COLLECTOR_VOLTAGE', 33 | col='black', colvar=NULL,lwd=1.5,theta=340) 34 | 35 | x2 = igbt_COLLECTOR_CURRENT[300000:301680] 36 | y2 = igbt_GATE_CURRENT[300000:301680] 37 | z2 = igbt_COLLECTOR_VOLTAGE[300000:301680] 38 | 39 | win.graph(800,800) 40 | lines3D(x2,y2,z2,phi = 0,ticktype="detailed", 41 | xlab='COLLECTOR_CURRENT',ylab='GATE_CURRENT',zlab='COLLECTOR_VOLTAGE', 42 | col='black', colvar=NULL,lwd=1.5,theta=340) 43 | 44 | 45 | # win.graph(800,800) 46 | # par(mar = c(5, 5, 5, 5),cex = 1,cex.axis = 1.5, cex.lab = 1.5,cex.main=1.5,oma = c(0, 0, 0, 0), 47 | # mgp = c(1, 1.8, 0),tcl = 0.5) 48 | # split_list = seq(1,total_len,interval_size) 49 | # cols = c('black','blue','red','brown') 50 | # col_i = 2 51 | # sub_ = data[c(split_list[1]:(split_list[2]-1)),] 52 | # lines3D(sub_$x,sub_$y,sub_$z,phi = 0,ticktype="detailed", 53 | # xlab='GATE_CURRENT',ylab='COLLECTOR_CURRENT',zlab='COLLECTOR_VOLTAGE', 54 | # col=cols[2], colvar=NULL,lwd=1.5) 55 | # for( i in c(3:length(split_list)) ){ 56 | # start_i = split_list[i-1] 57 | # end_i = split_list[i] - 1 58 | # sub_ = data[c(start_i:end_i),] 59 | # 60 | # lines3D(sub_$x,sub_$y,sub_$z,phi = 0,ticktype="detailed", 61 | # xlab='GATE_CURRENT',ylab='COLLECTOR_CURRENT',zlab='COLLECTOR_VOLTAGE', 62 | # col=cols[2], colvar=NULL,lwd=1.5,add=TRUE) 63 | # 64 | # 65 | # col_i = (col_i+1)%%length(cols)+1 66 | # 67 | # } 68 | 69 | 70 | -------------------------------------------------------------------------------- /fig_bearing_phase.R: -------------------------------------------------------------------------------- 1 | bearing = read.csv("data/final_dataset/IMSBearingNo2_mean_1024.csv",header = TRUE) 2 | 3 | # v1 failure 4 | bearing_v1 = bearing$V1[1000:nrow(bearing)] # v1 failure 5 | 6 | xt1 = bearing_v1[1:(length(bearing_v1)-1)] 7 | xt2 = bearing_v1[2:length(bearing_v1)] 8 | 9 | # original 10 | win.graph(13,4) 11 | par(mar = c(3, 3, 0.5, 0.5),cex = 1,cex.axis = 1.5, cex.lab = 1.5,cex.main=1.5,oma = c(0, 0, 0, 0),mgp = c(1.5, 0.5, 0),tcl = 0.5) 12 | plot(bearing_v1,type='b',xlab='',ylab='') 13 | abline(v=18000,col='grey60',lty=2,lwd=7) 14 | abline(v=7000,col='grey60',lty=2,lwd=7) 15 | 16 | # # divide into stable and unstable part 17 | # stable_part = bearing_v1[1:1500] 18 | # unstable_part = bearing_v1[15001:length(bearing_v1)] 19 | # xlim = c(min(unstable_part),max(unstable_part)) 20 | # 21 | # win.graph(8,8) 22 | # par(mar = c(3, 4, 0.5, 0.5),oma = c(0, 0, 0, 0),mgp = c(2, 0.4, 0),tcl = 0.5,cex.axis = 2, cex.lab = 2,cex.main=2) 23 | # stable_xt1 = stable_part[1:(length(stable_part)-1)] 24 | # stable_xt2 = stable_part[2:length(stable_part)] 25 | # plot(stable_xt1,stable_xt2,pch=16,xlim = xlim,ylim = xlim,xlab = 'x(t)',ylab = 'x(t+1)',cex=1.5) 26 | # 27 | # win.graph(8,8) 28 | # par(mar = c(3, 4, 0.5, 0.5),oma = c(0, 0, 0, 0),mgp = c(2, 0.4, 0),tcl = 0.5,cex.axis = 2, cex.lab = 2,cex.main=2) 29 | # unstable_xt1 = unstable_part[1:(length(unstable_part)-1)] 30 | # unstable_xt2 = unstable_part[2:(length(unstable_part))] 31 | # plot(unstable_xt1,unstable_xt2,pch=16,xlim = xlim,ylim = xlim,xlab = 'x(t)',ylab = 'x(t+1)',cex=1.5) 32 | 33 | 34 | ### divide into 2 part, same picture but different colour 35 | # win.graph(8,8) 36 | # names = c('Stable Motion Phase','Approaching Failure Motion Phase') 37 | # cols = c('blue','red') 38 | # par(mar = c(3, 4, 0.5, 0.5),oma = c(0, 0, 0, 0),mgp = c(2, 0.4, 0),tcl = 0.5,cex.axis = 2, cex.lab = 2,cex.main=2) 39 | # plot(xt1[(length(xt1)-200):(length(xt1)-40)],xt2[(length(xt2)-200):(length(xt2)-40)],pch=1,col=cols[1],lwd=2,cex=2, 40 | # xlab='x(t)',ylab='x(t+1)') 41 | # points(xt1[1:200],xt2[1:200],type='p',col=cols[2],pch=2,lwd=2,cex=2) 42 | # legend('topleft',names,lty=1,lwd=2,col = cols) 43 | 44 | # # plot 3d 45 | # xt1_3d = bearing_v1[1:(length(bearing_v1)-2)] 46 | # xt2_3d = bearing_v1[2:(length(bearing_v1)-1)] 47 | # xt3_3d = bearing_v1[3:length(bearing_v1)] 48 | # 49 | # win.graph(800,800) 50 | # par(mar = c(5, 5, 5, 5),cex = 1,cex.axis = 1.5, cex.lab = 1.5,cex.main=1.5,oma = c(0, 0, 0, 0),mgp = c(1, 1.8, 0),tcl = 0.5) 51 | # scatter3D(xt1, xt2, xt3,phi = 0,ticktype="detailed",bty = "g",xlab='x(t)',ylab='x(t+1)',zlab='x(t+2)',cex=2,lwd=2) 52 | 53 | -------------------------------------------------------------------------------- /fig_turbofan.R: -------------------------------------------------------------------------------- 1 | 2 | # # plot the fig1 for Unit1-variable 19 3 | #Train_FD001_Uni1_V19 4 | # turbofan = read.csv("data/final_dataset/TurbofanEngine_trainfd001_uni1.csv",header = TRUE) 5 | # turbofan_v19 = turbofan$V19 6 | # windowsize = 0.5*length(turbofan_v19) 7 | # turbofan_v19_indicators = get_indicators(index(turbofan_v19)[windowsize:length(turbofan_v19)], 8 | # turbofan_v19,windowsize = windowsize) 9 | # plot_statistic_indicators(turbofan_v19, 10 | # turbofan_v19_indicators, 11 | # main = 'A Measured Signal in the Turbofan Engine Degradation Simulation [NASA PCoE Datasets]', 12 | # xlab='Time', 13 | # ylab='Measured Value') 14 | 15 | 16 | 17 | # # plot for the supplement,which are the other variable plot 18 | # 19 | turbofan = read.csv("data/final_dataset/TurbofanEngine_trainfd001_uni1.csv",header = TRUE) 20 | turbofan_v = turbofan$V26 21 | windowsize = 0.5*length(turbofan_v) 22 | turbofan_v_indicators = get_indicators(index(turbofan_v)[windowsize:length(turbofan_v)], 23 | turbofan_v,windowsize = windowsize) 24 | plot_statistic_indicators(turbofan_v, 25 | turbofan_v_indicators, 26 | main = 'A Measured Signal in the Turbofan Engine Degradation Simulation [NASA PCoE Datasets]', 27 | xlab='Time', 28 | ylab='Measured Value') 29 | 30 | 31 | # plot for the supplement, the leading indicators with the detrending value 32 | # timeseries = turbofan$V14 33 | # dev.new() 34 | # layout(matrix(c(1,1,1,2,2,2,3,4,5), 3, 3, byrow = TRUE)) 35 | # par(mar = c(3, 4, 3, 1),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 36 | # tcl = 0.5) 37 | # 38 | # plot(timeseries,type='b') 39 | # band.rate = 0.4 40 | # method = 'gaussian' 41 | # smy = detrend(timeseries,method = method,band.rate = band.rate) 42 | # plot(smy,type='h') 43 | # 44 | # df.indicators = lead.indicators(timeseries,indicators = c("var","acf1","skew"), 45 | # win.rate = 0.5,detrending = 'gaussian',band.rate = band.rate) 46 | # plot(df.indicators$timeindex,df.indicators$var,type='b',xlab='time',ylab='variance') 47 | # ken_var = MannKendall(df.indicators$var)$tau[1] 48 | # legend('topleft',sprintf("Kendall tau = %.2f",ken_var),bty = 'n') 49 | # plot(df.indicators$timeindex,df.indicators$acf1,type='b',xlab='time',ylab='acf1') 50 | # ken_acf1 = MannKendall(df.indicators$acf1)$tau[1] 51 | # legend("topleft",sprintf("Kendall tau = %.2f",ken_acf1),bty = 'n') 52 | # plot(df.indicators$timeindex,df.indicators$skew,type='b',xlab='time',ylab='skew') 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /fig_igbt_phase_variable.R: -------------------------------------------------------------------------------- 1 | igbt_old = read.table("data/final_dataset/IIGBTAgingData.txt") 2 | 3 | 4 | igbt_COLLECTOR_CURRENT = igbt_old[,2] 5 | igbt_COLLECTOR_VOLTAGE = igbt_old[,3] 6 | igbt_GATE_CURRENT = igbt_old[,4] 7 | 8 | 9 | colnames = c('COLLECTOR_CURRENT','COLLECTOR_VOLTAGE','GATE_CURRENT','GATE_VOLTAGE','HEAT_SINK_TEMP','PACKAGE_TEMP') 10 | 11 | 12 | ## plot stable state 1 13 | win.graph(7,7) 14 | par(mfrow=c(3,1),mar = c(4, 5, 2.2, 0),cex.axis = 2.5, cex.lab = 2.5,cex.main=2.5,oma = c(0, 0, 0, 0),mgp = c(3, 1, 0), 15 | tcl = 0.5) 16 | # par(mfrow = c(3, 1)) 17 | # #par(cex = 0.6) 18 | # par(mar = c(0, 0, 0, 0), oma = c(4, 10, 3, 0.5)) 19 | # par(tcl = -0.25) 20 | # par(mgp = c(2, 0.6, 0)) 21 | # par(cex.axis = 2.5, cex.lab = 2.5,cex.main=2.5) 22 | 23 | rows = c(10000:15400) 24 | plot(rows,igbt_COLLECTOR_CURRENT[rows],type='b',axes = FALSE,ylab='Collector Current',xlab='') 25 | axis(2, col = "black", col.axis = "black") 26 | box(col = "black") 27 | 28 | plot(rows,igbt_COLLECTOR_VOLTAGE[rows],type='b',axes = FALSE,ylab='Collector voltage',xlab='') 29 | axis(2, col = "black", col.axis = "black") 30 | box(col = "black") 31 | 32 | plot(rows,igbt_GATE_CURRENT[rows],type='b',axes = FALSE,ylab='Gate Current',xlab='Time') 33 | axis(1, col = "black", col.axis = "black") 34 | axis(2, col = "black", col.axis = "black") 35 | box(col = "black") 36 | 37 | 38 | # ## plot stable state 2 39 | # win.graph(10,8) 40 | # par(mfrow=c(3,1),mar = c(3, 3.5, 1, 2),cex.axis = 1.8, cex.lab = 1.8,cex.main=1.8,oma = c(0, 0, 0, 0),mgp = c(2, 0.6, 0), 41 | # tcl = 0.5) 42 | # rows = c(200000:205400) 43 | # plot(rows,igbt_COLLECTOR_CURRENT[rows],type='b',axes = FALSE,ylab='Collector Current',xlab='') 44 | # axis(2, col = "black", col.axis = "black") 45 | # box(col = "black") 46 | # 47 | # 48 | # plot(rows,igbt_COLLECTOR_VOLTAGE[rows],type='b',axes = FALSE,ylab='Collector Voltage',xlab='') 49 | # axis(2, col = "black", col.axis = "black") 50 | # box(col = "black") 51 | # 52 | # plot(rows,igbt_GATE_CURRENT[rows],type='b',axes = FALSE,ylab='Gate Current',xlab='Time') 53 | # axis(1, col = "black", col.axis = "black") 54 | # axis(2, col = "black", col.axis = "black") 55 | # box(col = "black") 56 | # 57 | # 58 | # ## plot the last state 59 | # win.graph(10,8) 60 | # par(mfrow=c(3,1),mar = c(3, 3.5, 1, 2),cex.axis = 1.8, cex.lab = 1.8,cex.main=1.8,oma = c(0, 0, 0, 0),mgp = c(2, 0.6, 0), 61 | # tcl = 0.5) 62 | # rows = c(296280:301680) 63 | # plot(rows,igbt_COLLECTOR_CURRENT[rows],type='b',axes = FALSE,ylab='Collector Current',xlab='') 64 | # axis(2, col = "black", col.axis = "black") 65 | # box(col = "black") 66 | # 67 | # 68 | # plot(rows,igbt_COLLECTOR_VOLTAGE[rows],type='b',axes = FALSE,ylab='Collector Voltage',xlab='') 69 | # axis(2, col = "black", col.axis = "black") 70 | # box(col = "black") 71 | # 72 | # plot(rows,igbt_GATE_CURRENT[rows],type='b',axes = FALSE,ylab='Gate Current',xlab='Time') 73 | # axis(1, col = "black", col.axis = "black") 74 | # axis(2, col = "black", col.axis = "black") 75 | # box(col = "black") 76 | -------------------------------------------------------------------------------- /an_bearing_multistate.R: -------------------------------------------------------------------------------- 1 | 2 | bearing = read.csv("data/final_dataset/IMSBearingNo2_mean_1024.csv",header = TRUE) 3 | 4 | # v1 failure 5 | #bearing_v1 = bearing$V1 # v1 failure 6 | bearing_v1 = bearing$V1[1000:nrow(bearing)] # v1 failure without the noise data in the beginning 7 | 8 | 9 | ###### density fitted 10 | section1 = bearing_v1[1:7000] 11 | section2 = bearing_v1[7001:18000] 12 | section3 = bearing_v1[18001:length(bearing_v1)] 13 | 14 | ##### density plot 15 | 16 | kernel = 'gaussian' 17 | density_sec1 = density(section1,kernel = kernel) 18 | density_sec2 = density(section2,kernel = kernel) 19 | density_sec3 = density(section3,kernel = kernel) 20 | 21 | color = c('black','blue','red') 22 | legend_text = c(sprintf("index from 1 to 7000"), 23 | sprintf("index from 7001 to 18000"), 24 | sprintf("index from 18001 to %g",length(bearing_v1))) 25 | win.graph(8,7) 26 | par(mar = c(3, 4, 3, 1),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 27 | tcl = 0.5) 28 | plot(density_sec1, 29 | xlim = c( min(bearing_v1)-sd(bearing_v1), max(bearing_v1)+sd(bearing_v1)), 30 | ylim = c(0, max(max(density_sec1$y),max(density_sec2$y),max(density_sec3$y))), 31 | main='', 32 | col = color[1], 33 | lwd = 4, 34 | xlab='' 35 | ) 36 | lines(density_sec2,col=color[2],lwd=4) 37 | lines(density_sec3,col=color[3],lwd=4) 38 | legend("topleft","(x,y)",legend_text, lty = c(1,1), lwd = c(4,4), col = color,bty='n',cex=1.8) 39 | 40 | ##### location test 41 | 42 | # print("------------t.test-------------------") 43 | # print("section1 vs section2") 44 | # print(t.test(section1,section2)) 45 | # print("section1 vs section3") 46 | # print(t.test(section1,section3)) 47 | # print("section2 vs section3") 48 | # print(t.test(section2,section3)) 49 | # 50 | # print("------------wilcox.test--------------") 51 | # print("section1 vs section2") 52 | # print(wilcox.test(section1,section2)) 53 | # print("section1 vs section3") 54 | # print(wilcox.test(section1,section3)) 55 | # print("section2 vs section3") 56 | # print(wilcox.test(section2,section3)) 57 | 58 | 59 | ###### arima fitted 60 | 61 | # tail_len = length(section3) 62 | # tail_len = length(section3) 63 | # train_series = bearing_v1[1:(length(bearing_v1)-tail_len)] 64 | # arima.model = auto.arima(train_series) 65 | # print(summary(arima.model)) 66 | # predic.result = forecast.Arima(arima.model,h=tail_len,level = 95) 67 | # 68 | # win.graph(8,9) 69 | # par(mar = c(3, 4, 3, 2),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 70 | # tcl = 0.5) 71 | # #plot(bearing_v1,xlab='Time',ylab='Measured Value',type='b') 72 | # plot(c(15000:length(bearing_v1)),bearing_v1[15000:length(bearing_v1)],xlab='Time',ylab='Measured Value', 73 | # type='b',xlim=c(15000,(length(bearing_v1)+10))) 74 | # points(c((length(train_series)+1):length(bearing_v1)),predic.result$mean,col='red',lwd=4) 75 | # points( c((length(train_series)+1):length(bearing_v1)), predic.result$lower, col='red', lwd=4) 76 | # points(c((length(train_series)+1):length(bearing_v1)), predic.result$upper, col='red', lwd=4) 77 | 78 | 79 | -------------------------------------------------------------------------------- /fig_igbt_phase.R: -------------------------------------------------------------------------------- 1 | # igbt = read.csv('final_dataset/igbt_resample_mean_10.csv',header = TRUE) # resample by step 10 2 | # igbt_old = read.table("final_dataset/IIGBTAgingData.txt") 3 | # 4 | # igbt_COLLECTOR_CURRENT = igbt_old[,2] 5 | # igbt_COLLECTOR_VOLTAGE = igbt_old[,3] 6 | # igbt_GATE_CURRENT = igbt_old[,4] 7 | # igbt_GATE_VOLTAGE = igbt_old[,5] 8 | # igbt_HEAT_SINK_TEMP = igbt_old[,6] # bad, exclude 9 | # igbt_PACKAGE_TEMP = igbt_old[,7] 10 | 11 | colnames = c('COLLECTOR_CURRENT','COLLECTOR_VOLTAGE','GATE_CURRENT','GATE_VOLTAGE','HEAT_SINK_TEMP','PACKAGE_TEMP') 12 | 13 | x = igbt_GATE_CURRENT[10000:100000] 14 | y = igbt_COLLECTOR_CURRENT[10000:100000] 15 | z = igbt_COLLECTOR_VOLTAGE[10000:100000] 16 | 17 | interval_size = 600 18 | total_len = length(x) 19 | split_num = as.integer(total_len/interval_size) 20 | coltype = c() 21 | for( i in c(1:split_num)){ 22 | sub_col = rep(i,interval_size) 23 | coltype = append(coltype,sub_col) 24 | } 25 | if(split_num*interval_size < total_len){ 26 | sub_col = rep(split_num+1,(total_len - (split_num*interval_size))) 27 | coltype = append(coltype,sub_col) 28 | } 29 | 30 | data = data.frame(x,y,z,coltype) 31 | # 32 | # win.graph(800,800) 33 | # par(mar = c(5, 5, 5, 5),cex = 1,cex.axis = 1.5, cex.lab = 1.5,cex.main=1.5,oma = c(0, 0, 0, 0), 34 | # mgp = c(1, 1.8, 0),tcl = 0.5) 35 | # 36 | # lines3D(data$x,data$y,data$z,phi = 0,ticktype="detailed", 37 | # xlab='GATE_CURRENT',ylab='COLLECTOR_CURRENT',zlab='COLLECTOR_VOLTAGE', 38 | # col=data$coltype) 39 | 40 | win.graph(800,800) 41 | par(mar = c(5, 5, 5, 5),cex = 1,cex.axis = 1.5, cex.lab = 1.5,cex.main=1.5,oma = c(0, 0, 0, 0), 42 | mgp = c(1, 1.8, 0),tcl = 0.5) 43 | split_list = seq(1,total_len,interval_size) 44 | cols = c('black','blue','red','brown') 45 | col_i = 2 46 | sub_ = data[c(split_list[1]:(split_list[2]-1)),] 47 | lines3D(sub_$x,sub_$y,sub_$z,phi = 0,ticktype="detailed", 48 | xlab='GATE_CURRENT',ylab='COLLECTOR_CURRENT',zlab='COLLECTOR_VOLTAGE', 49 | col=cols[2], colvar=NULL,lwd=1.5) 50 | for( i in c(3:length(split_list)) ){ 51 | start_i = split_list[i-1] 52 | end_i = split_list[i] - 1 53 | sub_ = data[c(start_i:end_i),] 54 | 55 | lines3D(sub_$x,sub_$y,sub_$z,phi = 0,ticktype="detailed", 56 | xlab='GATE_CURRENT',ylab='COLLECTOR_CURRENT',zlab='COLLECTOR_VOLTAGE', 57 | col=cols[2], colvar=NULL,lwd=1.5,add=TRUE) 58 | 59 | 60 | col_i = (col_i+1)%%length(cols)+1 61 | 62 | } 63 | 64 | 65 | # x = data$x[1:300] 66 | # y = data$y[1:300] 67 | # z = data$z[1:300] 68 | # win.graph(800,800) 69 | # par(mar = c(5, 5, 5, 5),cex = 1,cex.axis = 1.5, cex.lab = 1.5,cex.main=1.5,oma = c(0, 0, 0, 0), 70 | # mgp = c(1, 1.8, 0),tcl = 0.5) 71 | # 72 | # lines3D(x,y,z,phi = 0,ticktype="detailed", 73 | # xlab='GATE_CURRENT',ylab='COLLECTOR_CURRENT',zlab='COLLECTOR_VOLTAGE', 74 | # col=NULL, colvar=NULL) 75 | # 76 | # x = data$x[301:600] 77 | # y = data$y[301:600] 78 | # z = data$z[301:600] 79 | # lines3D(x,y,z,phi = 0,ticktype="detailed", 80 | # xlab='GATE_CURRENT',ylab='COLLECTOR_CURRENT',zlab='COLLECTOR_VOLTAGE', 81 | # col='red', colvar=NULL,add=TRUE) 82 | 83 | 84 | #arrows3D(x[150],y[150],z[150],x[152],y[152],z[152],lwd=2,length = 0.2,code = 2) 85 | #arrows2D(x[150],y[150],x[151],y[151]) 86 | 87 | -------------------------------------------------------------------------------- /fig_igbt_detrend.R: -------------------------------------------------------------------------------- 1 | igbt_detrend_plot <- function(originalSeries, 2 | detrendedSeries, 3 | indicators){ 4 | 5 | win.graph(800,400) 6 | layout(matrix(c(1,1,1,2,2,2,3,4,5), 3, 3, byrow = TRUE)) 7 | par(mar = c(3, 4, 3, 1),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 8 | tcl = 0.5) 9 | 10 | plot(originalSeries,type='b',main='The Collector Current Signal in IGBT Accelerated Aging Test [NASA PCoE Datasets]', 11 | xlab='Time',ylab='Current(A)') 12 | 13 | plot(detrendedSeries ,type='b',main='detrended time series',xlab='time',ylab='Amps') 14 | 15 | # variance 16 | plot(indicators$time,indicators$variance,main='Variance',type='b',xlab='',ylab='') 17 | var_kendall = MannKendall(na.omit(indicators$variance)) 18 | mtext(sprintf("kendall = %g",var_kendall$tau[1]),side = 3,line = -5, adj = 0.1,cex = 1) 19 | 20 | # autocorrelation 21 | plot(indicators$time,indicators$autocorrelation,main='Autocorrelation(1)',type='b',xlab='',ylab='') 22 | auto_kendall = MannKendall(na.omit(indicators$autocorrelation)) 23 | mtext(sprintf("kendall = %.2f",auto_kendall$tau[1]),side = 3,line = -5, adj = 0.1,cex = 1) 24 | 25 | # skewness 26 | plot(indicators$time,scale(indicators$skewness),main='Skewness ',type='b',xlab='',ylab='') 27 | auto_sk = MannKendall(na.omit(indicators$skewness)) 28 | mtext(sprintf("kendall = %.2f",auto_sk$tau[1]),side = 3,line = -5, adj = 0.1,cex = 1) 29 | } 30 | 31 | igbt_detrend_plot_split <- function(originalSeries, 32 | detrendedSeries, 33 | indicators, 34 | orig_ylab){ 35 | 36 | win.graph(10,3) 37 | par(mar = c(3, 4, 2, 2),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 38 | tcl = 0.5) 39 | plot(originalSeries,type='b',xlab='Time',ylab=orig_ylab) 40 | 41 | 42 | win.graph(10,3) 43 | par(mar = c(3, 2, 1, 2),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 44 | tcl = 0.5) 45 | plot(detrendedSeries ,type='b',xlab='',ylab='') 46 | #plot(detrendedSeries ,type='h',xlab='',ylab='',axes=FALSE) 47 | 48 | 49 | win.graph(10,3) 50 | # par(mfrow=c(1,3),cex.axis = 2, cex.lab = 2,cex.main=2, 51 | # tcl = 0.5) 52 | par(mfrow=c(1,3),mar = c(3, 2, 3, 2),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 53 | tcl = 0.5) 54 | # variance 55 | plot(indicators$time,indicators$variance,main='Variance',type='b',xlab='',ylab='') 56 | 57 | var_kendall = MannKendall(na.omit(indicators$variance)) 58 | mtext(sprintf("kendall = %.2f",var_kendall$tau[1]),side = 3,line = -5, adj = 0.1,cex = 1.5) 59 | 60 | # autocorrelation 61 | plot(indicators$time,indicators$autocorrelation,main='Autocorrelation(1)',type='b',xlab='',ylab='') 62 | 63 | auto_kendall = MannKendall(na.omit(indicators$autocorrelation)) 64 | mtext(sprintf("kendall = %.2f",auto_kendall$tau[1]),side = 3,line = -5, adj = 0.1,cex = 1.5) 65 | 66 | # skewness 67 | plot(indicators$time,scale(indicators$skewness),main='Skewness ',type='b',xlab='',ylab='') 68 | auto_sk = MannKendall(na.omit(indicators$skewness)) 69 | mtext(sprintf("kendall = %.2f",auto_sk$tau[1]),side = 3,line = -5, adj = 0.1,cex = 1.5) 70 | 71 | } 72 | 73 | 74 | -------------------------------------------------------------------------------- /an_ge_preprocess.R: -------------------------------------------------------------------------------- 1 | #' @description The GE data marked by '2009' in Date segment are reboot noise, based on 2 | #' which this function realize to divide original dataset into several subset which exclude 3 | #' the reboot nose. 4 | #' @param root_dir where save the original data file 5 | #' @param out_dir 6 | ge_seg_original_file <- function(root_dir = "data/GEData/m1_3/original", 7 | out_dir = "data/GEData/m1_3/parsed"){ 8 | 9 | dir.create(out_dir) 10 | file_list = list.files(path = root_dir,pattern = ".csv") 11 | for(file_name in file_list){ 12 | print("-------------------------------------------------------------------------------------------") 13 | data = read.csv(sprintf("%s/%s",root_dir,file_name),header = TRUE) 14 | #columname = c('Date','Time','KM','Meters','Speed','Trac.Effort','BP','BC','ER','Fuel','BP.Flow') 15 | #data = data[,columname] 16 | indexs = c(1:nrow(data)) 17 | split_points = indexs[grepl('2009',data$Date)] 18 | print("split points: ") 19 | print(split_points) 20 | 21 | select_points = c() 22 | if(length(split_points)!=0){ 23 | select_points = c(1,(split_points[1]-1)) 24 | flag = TRUE 25 | for( i in c( 2:length(split_points) )){ 26 | if( split_points[i] != (split_points[i-1]+1) ){ 27 | select_points = append(select_points,c((split_points[i-1]+1),(split_points[i]-1))) 28 | } 29 | } 30 | if(split_points[length(split_points)]0] 23 | # bp_series_move_0 = bp_series_move_0[bp_series_move_0>350] 24 | # bp_series_move_10 = ge_data_m5_sample$BP[ge_data_m5_sample$Speed>10] 25 | # bp_series_move_20 = ge_data_m5_sample$BP[ge_data_m5_sample$Speed>20] 26 | 27 | # ge_move = ge_move_m5[ge_move_m5$BP>=350,] # just single point less than 350 28 | # bp_series_move_0 = ge_move$BP # just single point less than 350 29 | # bp_series_move_10 = ge_move$BP[ge_move$Speed>10] 30 | # bp_series_move_20 = ge_move$BP[ge_move$Speed>20] 31 | # 32 | # 33 | # windowsize = 10000 34 | # indicators_10 = get_indicators(index(bp_series_move_10)[windowsize:length(bp_series_move_10)], 35 | # bp_series_move_10,windowsize = windowsize) 36 | # plot_statistic_indicators(bp_series_move_10, 37 | # indicators_10, 38 | # main = 'Pressure Signal in a Locomotive Brake Pipe Before It Fails', 39 | # xlab = 'Time', 40 | # ylab = 'Pressure(KPa)') 41 | 42 | ### analysis other variable for supplement 43 | #ge_move_10 = ge_move_m5[ge_move_m5$Speed>10,] 44 | ge_move_10 = ge_move[ge_move$Speed>10,] 45 | time_series = ge_move_10$BC 46 | indicators = get_indicators(index(time_series)[windowsize:length(time_series)], 47 | time_series, 48 | windowsize = windowsize) 49 | plot_statistic_indicators(time_series, 50 | indicators, 51 | main = 'Pressure Signal in a Locomotive Brake Cylinder Before It Fails', 52 | xlab = 'Time', 53 | ylab = 'Pressure(KPa)') 54 | 55 | # time_series = ge_move_10$ER 56 | # indicators = get_indicators(index(time_series)[windowsize:length(time_series)], 57 | # time_series, 58 | # windowsize = windowsize) 59 | # plot_statistic_indicators(time_series, 60 | # indicators, 61 | # main = 'Pressure Signal in a Locomotive Eq Res Pressure Before It Fails', 62 | # xlab = 'Time', 63 | # ylab = 'Pressure(KPa)') 64 | 65 | 66 | 67 | ############################### the other test for choosing the speed key point ################## 68 | # indicators_20 = get_indicators(index(bp_series_move_20)[windowsize:length(bp_series_move_20)], 69 | # bp_series_move_20,windowsize = windowsize) 70 | # ge_plot_indicator(bp_series_move_20,indicators_20) 71 | 72 | 73 | ############# ajust the largest BP value to the same level (comprimise) 74 | 75 | # bp_series_move_0_aline = bp_series_move_0 76 | # bp_series_move_0_aline[bp_series_move_0_aline==503] = 496 77 | # bp_series_move_0_aline[bp_series_move_0_aline==489] = 496 78 | # 79 | # bp_series_move_10_aline = bp_series_move_10 80 | # bp_series_move_10_aline[bp_series_move_10_aline==503] = 496 81 | # bp_series_move_10_aline[bp_series_move_10_aline==489] = 496 82 | # 83 | # bp_series_move_20_aline = bp_series_move_20 84 | # bp_series_move_20_aline[bp_series_move_20_aline==503] = 496 85 | # bp_series_move_20_aline[bp_series_move_20_aline==489] = 496 86 | # 87 | # 88 | # 89 | # indicators_0_aline = get_indicators(index(bp_series_move_0_aline)[windowsize:length(bp_series_move_0_aline)], 90 | # bp_series_move_0_aline,windowsize = windowsize) 91 | # ge_plot_indicator(bp_series_move_0_aline,indicators_0_aline) 92 | # 93 | # indicators_10_aline = get_indicators(index(bp_series_move_10_aline)[windowsize:length(bp_series_move_10_aline)], 94 | # bp_series_move_10_aline,windowsize = windowsize) 95 | # ge_plot_indicator(bp_series_move_10_aline,indicators_10_aline) 96 | # 97 | # indicators_20_aline = get_indicators(index(bp_series_move_20_aline)[windowsize:length(bp_series_move_20_aline)], 98 | # bp_series_move_20_aline,windowsize = windowsize) 99 | # ge_plot_indicator(bp_series_move_20_aline,indicators_20_aline) 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /fig_igbt.R: -------------------------------------------------------------------------------- 1 | 2 | igbt = read.csv('data/final_dataset/igbt_resample_mean_10.csv',header = TRUE) # resample by step 10 3 | igbt_old = read.table("data/final_dataset/IIGBTAgingData.txt") 4 | 5 | igbt_COLLECTOR_CURRENT = igbt_old[290000:nrow(igbt_old),2] 6 | igbt_COLLECTOR_VOLTAGE = igbt_old[290000:nrow(igbt_old),3] 7 | igbt_GATE_CURRENT = igbt_old[290000:nrow(igbt_old),4] 8 | igbt_GATE_VOLTAGE = igbt_old[290000:nrow(igbt_old),5] 9 | igbt_HEAT_SINK_TEMP = igbt_old[290000:nrow(igbt_old),6] 10 | igbt_PACKAGE_TEMP = igbt_old[290000:nrow(igbt_old),7] 11 | 12 | # # fig 1 13 | # time_series = igbt_COLLECTOR_CURRENT 14 | # windowsize = 0.5*length(time_series) 15 | # igbt_indicators = get_indicators(index(time_series)[windowsize:length(time_series)], 16 | # time_series, 17 | # windowsize = windowsize) 18 | # plot_statistic_indicators(time_series, 19 | # igbt_indicators, 20 | # main = 'The Collector Current Signal in IGBT Accelerated Aging Test [NASA PCoE Datasets]', 21 | # xlab = 'Time', 22 | # ylab = 'Current(A)') 23 | 24 | 25 | ############################## Supplement ################################### 26 | 27 | ########################## COLLECTOR_VOLTAGE 28 | time_series = igbt_COLLECTOR_VOLTAGE 29 | windowsize = 0.5*length(time_series) 30 | igbt_indicators = get_indicators(index(time_series)[windowsize:length(time_series)], 31 | time_series, 32 | windowsize = windowsize) 33 | plot_statistic_indicators(time_series, 34 | igbt_indicators, 35 | main = 'The Collector Voltage Signal in IGBT Accelerated Aging Test [NASA PCoE Datasets]', 36 | xlab = 'Time', 37 | ylab = 'Voltage(V)') 38 | 39 | # ######################### GATE_CURRENT 40 | # time_series = igbt_GATE_CURRENT 41 | # windowsize = 0.5*length(time_series) 42 | # igbt_indicators = get_indicators(index(time_series)[windowsize:length(time_series)], 43 | # time_series, 44 | # windowsize = windowsize) 45 | # plot_statistic_indicators(time_series, 46 | # igbt_indicators, 47 | # main = 'The Gate Current Signal in IGBT Accelerated Aging Test [NASA PCoE Datasets]', 48 | # xlab = 'Time', 49 | # ylab = 'Current(A)') 50 | # 51 | # ######################### GATE_CURRENT 52 | # time_series = igbt_GATE_VOLTAGE 53 | # windowsize = 0.5*length(time_series) 54 | # igbt_indicators = get_indicators(index(time_series)[windowsize:length(time_series)], 55 | # time_series, 56 | # windowsize = windowsize) 57 | # plot_statistic_indicators(time_series, 58 | # igbt_indicators, 59 | # main = 'The Gate Voltage Signal in IGBT Accelerated Aging Test [NASA PCoE Datasets]', 60 | # xlab = 'Time', 61 | # ylab = 'Voltage(V)') 62 | # 63 | # ######################### PACKAGE_TEMP 64 | # 65 | # time_series = igbt_PACKAGE_TEMP 66 | # windowsize = 0.5*length(time_series) 67 | # igbt_indicators = get_indicators(index(time_series)[windowsize:length(time_series)], 68 | # time_series, 69 | # windowsize = windowsize) 70 | # plot_statistic_indicators(time_series, 71 | # igbt_indicators, 72 | # main = 'The Package Temperature Signal in IGBT Accelerated Aging Test [NASA PCoE Datasets]', 73 | # xlab = 'Time', 74 | # ylab = 'Temperature(Celsius)') 75 | 76 | 77 | # ######### density plot 78 | # time_series = igbt_PACKAGE_TEMP 79 | # out_dir = "figure/supplement/igbt_density" 80 | # png_path = sprintf("%s/%s.png",out_dir,'Package_Temp') 81 | # png(filename = png_path,width = 2048,height = 768) 82 | # par(mar = c(3, 4, 3, 1),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 83 | # tcl = 0.5) 84 | # 85 | # key = 9500 86 | # kernel = 'gaussian' 87 | # seg_1 = time_series[1:key] 88 | # seg_2 = time_series[(key+1):length(time_series)] 89 | # density_1 = density(seg_1,kernel = kernel) 90 | # density_2 = density(seg_2,kernel = kernel) 91 | # 92 | # color = c('blue','red') 93 | # legend_text = c(sprintf("index from 1 to %g",key), 94 | # sprintf("index from %g to the %g",(key+1),length(time_series))) 95 | # plot( density_1, 96 | # xlim = c( (min(time_series)-var(time_series)), (max(time_series)+var(time_series)) ), 97 | # ylim = c( 0, max(max(density_1$y),max(density_2$y)) ), 98 | # main='The Package Temperature Signal in IGBT Accelerated Aging Test [NASA PCoE Datasets]') 99 | # polygon(density_1,col = color[1], border = color[1]) 100 | # polygon(density_2, col = color[2], border = color[2]) 101 | # legend("topleft","(x,y)",legend_text, lty = c(1,1), lwd = c(2.5,2.5), col = color) 102 | # dev.off() -------------------------------------------------------------------------------- /an_igbt_detrend_main.R: -------------------------------------------------------------------------------- 1 | igbt_ = read.table("data/IIGBTAgingData.txt") 2 | 3 | ############################## 4 | # fluctuation mining 5 | ############################# 6 | 7 | ################################################################################# 8 | ##figure for the paper main body —— collector current 9 | # 10 | # collecter_current = igbt_[282000:nrow(igbt_),2] 11 | # collecter_current_residuals = collecter_current[collecter_current<=2] 12 | # 13 | # # these two threshold are not different for the current variable 14 | # series = collecter_current_residuals 15 | # threshold = (max(series[1:round(0.5*length(series))]) - mean(series[1:round(0.5*length(series))]))/2 16 | # #threshold = (max(series[1:round(0.5*length(series))]) - mean(series[1:round(0.5*length(series))])) 17 | # 18 | # current_split_list = split_by_cycle(collecter_current_residuals,threshold) 19 | # model_coef = model_learing(current_split_list,len = as.integer(0.5*length(current_split_list))) 20 | # smoothed_series = smooth_exp(current_split_list,coef_intercep = model_coef[1],coef_x = model_coef[2], 21 | # diff_start = 5,diff_end = 0) 22 | # 23 | # # smooth_size = 55 24 | # # smoothed_series = smooth_debug(current_split_list,size = smooth_size, 25 | # # coef_intercep = model_coef[1],coef_x = model_coef[2]) 26 | # 27 | # timeseries = smoothed_series 28 | # windowsize = 0.5*length(timeseries) 29 | # indicators = get_indicators(index(timeseries)[windowsize:length(timeseries)], 30 | # timeseries, 31 | # windowsize = windowsize) 32 | # 33 | # igbt_detrend_plot_split(collecter_current, 34 | # timeseries, 35 | # indicators, 36 | # 'Current(A)') 37 | 38 | ################################################################################## 39 | ### figure for the supplement 40 | 41 | ######################################### 42 | #### COLLECTOR_VOLTAGE 43 | 44 | # signal = igbt_[282000:nrow(igbt_),3] 45 | # signal_residuals = signal[signal>=9.5] 46 | # threshold = (max(signal_residuals[1:round(0.5*length(signal_residuals))]) - mean(signal_residuals[1:round(0.5*length(signal_residuals))])) 47 | # #threshold = 0.1233584 the result of above 48 | # #threshold = 0.1233584 49 | # split_list = split_by_cycle(signal_residuals,threshold) 50 | # 51 | # model_coef = model_learing(split_list,len = as.integer(0.5*length(split_list))) 52 | # smoothed_series = smooth_exp(split_list,coef_intercep = model_coef[1],coef_x = model_coef[2], 53 | # diff_start = 5,diff_end = 0) 54 | # #smoothed_series = smooth_exp_unique(split_list) 55 | # # smooth_size = 55 56 | # # smoothed_series = smooth_debug(split_list,size = smooth_size, 57 | # # coef_intercep = model_coef[1],coef_x = model_coef[2]) 58 | # 59 | # timeseries = smoothed_series 60 | # windowsize = 0.5*length(timeseries) 61 | # indicators = get_indicators(index(timeseries)[windowsize:length(timeseries)], 62 | # timeseries, 63 | # windowsize = windowsize) 64 | # 65 | # igbt_detrend_plot_split(signal, 66 | # timeseries, 67 | # indicators, 68 | # 'Voltage(V)') 69 | 70 | 71 | ######################################### 72 | #### GATE_CURRENT 73 | 74 | signal = igbt_[282000:nrow(igbt_),4] 75 | signal_residuals = signal[signal<=0.002] 76 | #threshold = (max(signal_residuals[1:round(0.5*length(signal_residuals))]) - mean(signal_residuals[1:round(0.5*length(signal_residuals))])) 77 | threshold = 0.00019 # the result above equation 78 | #threshold = 0.00035 79 | split_list = split_by_cycle(signal_residuals,threshold) 80 | 81 | new_list = list() 82 | cycle_list = split_list 83 | threshold = 10 # when the number of point in per period less than 10, it is the noise. 84 | k = 1 85 | i = 1 86 | while( i <= length(cycle_list)){ 87 | if(length(cycle_list[[i]])>1){ 88 | new_list[[k]] = cycle_list[[i]] 89 | k = k+1 90 | }else{ 91 | new_list[[k]] = append(cycle_list[[i]],cycle_list[[i+1]]) 92 | i = i+1 93 | } 94 | i = i+1 95 | } 96 | split_list = new_list 97 | 98 | # split_list = eliminate_noise(split_list) 99 | model_coef = model_learing(split_list,len = as.integer(0.5*length(split_list))) 100 | smoothed_series = smooth_exp(split_list,coef_intercep = model_coef[1],coef_x = model_coef[2], 101 | diff_start = 18,diff_end = 7) ## 18,7 102 | 103 | # smooth_size = 57 104 | # smoothed_series = smooth_debug(split_list,size = smooth_size, 105 | # coef_intercep = model_coef[1],coef_x = model_coef[2]) 106 | # 107 | timeseries = smoothed_series 108 | windowsize = 0.5*length(timeseries) 109 | indicators = get_indicators(index(timeseries)[windowsize:length(timeseries)], 110 | timeseries, 111 | windowsize = windowsize) 112 | 113 | igbt_detrend_plot_split(signal, 114 | timeseries, 115 | indicators, 116 | 'Current(A)') 117 | 118 | -------------------------------------------------------------------------------- /an_igbt_detrend.R: -------------------------------------------------------------------------------- 1 | split_by_cycle_2mode <- function(series){ 2 | split_list = list() 3 | one_cycle = c() 4 | one_cycle = append(one_cycle,series[1]) 5 | cur_index = 1 6 | #threshold = max(series[1:round(0.5*length(series))]) - mean(series[1:round(0.5*length(series))]) 7 | threshold = mean(series)/2 8 | for (i in c(2:length(series))) { 9 | cur = series[i] 10 | pre = series[i-1] 11 | if( abs(cur-pre) <=threshold ){ 12 | one_cycle = append(one_cycle,cur) 13 | }else{ 14 | split_list[[cur_index]] = one_cycle 15 | cur_index = cur_index + 1 16 | one_cycle = c() 17 | one_cycle = append(one_cycle,cur) 18 | } 19 | } 20 | 21 | return(split_list) 22 | } 23 | 24 | split_by_cycle_continue <- function(series){ 25 | split_list = list() 26 | one_cycle = c() 27 | one_cycle = append(one_cycle,series[1]) 28 | cur_index = 1 29 | flag = 0 30 | for (i in c(2:length(series))) { 31 | if(flag == 0 & series[i]>series[i-1] ){ 32 | one_cycle = append(one_cycle,series[i]) 33 | } 34 | if(flag == 0 & series[i]<=series[i-1]){ 35 | flag = 1 36 | } 37 | if(flag == 1 & series[i]<=series[i-1]){ 38 | one_cycle = append(one_cycle,series[i]) 39 | } 40 | if(flag == 1 & series[i]>series[i-1]){ 41 | split_list[[cur_index]] = one_cycle 42 | cur_index = cur_index+1 43 | one_cycle = c() 44 | one_cycle = append(one_cycle,series[i]) 45 | flag=0 46 | } 47 | } 48 | return(split_list) 49 | } 50 | 51 | split_by_cycle <- function(series,threshold){ 52 | split_list = list() 53 | one_cycle = c() 54 | one_cycle = append(one_cycle,series[1]) 55 | cur_index = 1 56 | for (i in c(2:length(series))) { 57 | cur = series[i] 58 | pre = series[i-1] 59 | if( abs(cur-pre) <=threshold ){ 60 | one_cycle = append(one_cycle,cur) 61 | }else{ 62 | split_list[[cur_index]] = one_cycle 63 | cur_index = cur_index + 1 64 | one_cycle = c() 65 | one_cycle = append(one_cycle,cur) 66 | } 67 | } 68 | 69 | return(split_list) 70 | } 71 | eliminate_noise <- function(cycle_list) 72 | { 73 | new_list = list() 74 | threshold = 10 # when the number of point in per period less than 10, it is the noise. 75 | k = 1 76 | for( i in c(1:length(cycle_list))){ 77 | if(length(cycle_list[[i]])>threshold){ 78 | new_list[[k]] = cycle_list[[i]] 79 | k = k+1 80 | } 81 | } 82 | return(new_list) 83 | } 84 | 85 | eliminate_noise2 <- function(cycle_list) 86 | { 87 | new_list = list() 88 | threshold = 10 # when the number of point in per period less than 10, it is the noise. 89 | k = 1 90 | for( i in c(1:length(cycle_list))){ 91 | if(length(cycle_list[[i]])>threshold){ 92 | cur_ = cycle_list[[i]] 93 | cur_min = mean(cur_) - sd(cur_) 94 | cur_max = mean(cur_) + sd(cur_) 95 | cur_ = cur_[cur_>=cur_min & cur_<=cur_max] 96 | if(length(cur_)>threshold){ 97 | new_list[[k]] = cur_ 98 | k = k+1 99 | } 100 | } 101 | } 102 | return(new_list) 103 | } 104 | 105 | concate_list <- function(cycle_list){ 106 | series = c() 107 | for(i in c(1:length(cycle_list))){ 108 | series = append(series,cycle_list[[i]]) 109 | } 110 | return(series) 111 | } 112 | 113 | smooth_exp <- function(series_list,coef_intercep,coef_x,diff_start,diff_end){ 114 | smoothed_series = c() 115 | for(i in c(2:(length(series_list)-1))){ # the first and last section no enough a cycle, ignore 116 | y = series_list[[i]] 117 | t = index(y) 118 | ynew = exp(coef_x*t + coef_intercep) 119 | residual = y - ynew 120 | smoothed_series = append(smoothed_series,residual[diff_start:(length(residual)-diff_end)]) 121 | #smoothed_series = append(smoothed_series,residual) 122 | } 123 | return(smoothed_series) 124 | } 125 | 126 | smooth_exp_same_len <- function(series_list,size = 50, coef_intercep,coef_x){ 127 | smoothed_series = c() 128 | for(i in c(2:(length(series_list)-1))){ # the first and last section no enough a cycle, ignore 129 | y = series_list[[i]] 130 | t = index(y) 131 | start_i = 1 132 | end_i = start_i + size - 1 133 | y = y[c(start_i:end_i)] 134 | t = t[c(start_i:end_i)] 135 | ynew = exp(coef_x*t+coef_intercep) 136 | residual = y - ynew 137 | smoothed_series = append(smoothed_series,residual) 138 | } 139 | return(smoothed_series) 140 | } 141 | smooth_debug <- function(series_list,size = 50, coef_intercep,coef_x){ 142 | smoothed_series = c() 143 | for(i in c(2:(length(series_list)-1))){ # the first and last section no enough a cycle, ignore 144 | y = series_list[[i]] 145 | t = index(y) 146 | start_i = 1 147 | end_i = start_i + size - 1 148 | y = y[c(start_i:end_i)] 149 | t = t[c(start_i:end_i)] 150 | ynew = exp(coef_x*t+coef_intercep) 151 | residual = y - ynew 152 | smoothed_series = append(smoothed_series,residual[3:(length(residual))]) 153 | } 154 | return(smoothed_series) 155 | } 156 | 157 | # bad 158 | smooth_exp_unique <- function(series_list){ 159 | smoothed_series = c() 160 | for(i in c(1:(length(series_list)))){ # the first and last section no enough a cycle, ignore 161 | y = series_list[[i]] 162 | y = y[y>=(mean(y)-sd(y)) & y<=(mean(y)+sd(y))] 163 | t = index(y) 164 | exp_model = lm(log(y) ~ 0+t) 165 | residual = exp_model$residuals 166 | smoothed_series = append(smoothed_series,residual) 167 | } 168 | return(smoothed_series) 169 | } 170 | 171 | model_learing <- function(series_list,len=20){ 172 | sum_coef_inter = 0 173 | sum_x = 0 174 | for(i in c(1:(1+len-1))){ 175 | y = series_list[[i]] 176 | x = index(y) 177 | exp.model = lm(log(y)~ x) 178 | sum_coef_inter = sum_coef_inter + exp.model$coefficients[1] 179 | sum_x = sum_x + exp.model$coefficients[2] 180 | } 181 | return(c(sum_coef_inter/len, sum_x/len)) 182 | } -------------------------------------------------------------------------------- /an_cmapss.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | nasa_plot_original_data <- function(data_path, 4 | save_path, 5 | unit=1, 6 | type=c('b','o'), 7 | add_gaussian_line = TRUE, 8 | windowsize_rate = 0.1){ 9 | nasa = read.table(data_path) 10 | nasa_unit1 = nasa[nasa$V1==unit,] 11 | for( col in c(3:length(nasa_unit1))){ 12 | dir.create(save_path) 13 | png_path = sprintf("%s/v%g.png",save_path,col) 14 | png(filename = png_path,width = 2048,height = 768) 15 | 16 | plot(nasa_unit1[,col],type=type[1]) 17 | if(add_gaussian_line){ 18 | data_smt_gaussian = smth.gaussian(nasa_unit1[,col],window = length(data)*windowsize_rate) 19 | lines(data_smt_gaussian,type='b',col="red") 20 | } 21 | 22 | dev.off() 23 | } 24 | } 25 | 26 | nasa_analysis <- function(index,time_series){ 27 | 28 | win.graph(200,80) 29 | plot(index,time_series,type='b') 30 | arima_model = auto.arima(time_series) 31 | 32 | print("arima result: ") 33 | print(arima_model) 34 | print("Box testing result: ") 35 | box_testing = Box.test(arima_model$residuals,lag = 20, type = "Ljung-Box") 36 | 37 | print("box test result: ") 38 | print(box_testing) 39 | win.graph(200,80) 40 | plot.forecast(forecast.Arima(arima_model,h=10)) 41 | 42 | sliding_window(time_series) 43 | 44 | } 45 | 46 | 47 | nasa_analysis_plot <- function(indexs,values,number){ 48 | sink("sink-examp.txt") 49 | print("----------------------------------------------") 50 | print(sprintf("the analysis for value v%g",number)) 51 | 52 | win.graph(400, 150) 53 | plot(indexs,values,type='b',main=sprintf("original vale of v%g",number)) 54 | 55 | arima_model = auto.arima(values) 56 | print("arima model fitting result: ") 57 | print(arima_model) 58 | 59 | win.graph(400, 150) 60 | plot.forecast(forecast.Arima(arima_model,h=10)) 61 | 62 | residuls = arima_model$residuals 63 | win.graph(200, 100) 64 | acf(residuls,lag.max = 20) 65 | boxtest_result = Box.test(residuls,lag = 20,type = "Ljung-Box") 66 | print("Box test with Ljung-Box result: ") 67 | print(boxtest_result) 68 | 69 | win.graph(400, 150) 70 | plot(indexs,residuls,type='b',main=sprintf("the residuals vale of v%g",number)) 71 | 72 | sliding_window(residuls) 73 | 74 | gaussian_density(values, length(values)/2) 75 | 76 | print("-------------------------------------------------") 77 | sink() 78 | 79 | } 80 | nasa_analysis_unit <- function(indexs,values,number){ 81 | sink("sink-examp.txt") 82 | print("----------------------------------------------") 83 | print(sprintf("the analysis for value v%g",number)) 84 | 85 | win.graph(400, 150) 86 | plot(indexs,values,type='b',main=sprintf("original vale of v%g",number)) 87 | 88 | arima_model = auto.arima(values) 89 | print("arima model fitting result: ") 90 | print(arima_model) 91 | 92 | win.graph(400, 150) 93 | plot.forecast(forecast.Arima(arima_model,h=10)) 94 | 95 | residuls = arima_model$residuals 96 | win.graph(200, 100) 97 | acf(residuls,lag.max = 20) 98 | boxtest_result = Box.test(residuls,lag = 20,type = "Ljung-Box") 99 | print("Box test with Ljung-Box result: ") 100 | print(boxtest_result) 101 | 102 | win.graph(400, 150) 103 | plot(indexs,residuls,type='b',main=sprintf("the residuals vale of v%g",number)) 104 | 105 | sliding_window(residuls) 106 | 107 | gaussian_density(values, length(values)/2) 108 | 109 | print("-------------------------------------------------") 110 | sink() 111 | 112 | } 113 | 114 | ## 115 | ## analysis a nasa data file 116 | ## 117 | nasa_analysis_arima <- function(file_name,dir_output,length_rate=0.5,constant_cols=NULL) 118 | { 119 | sink(sprintf("%s_result.txt",file_name)) 120 | dir.create(sprintf("%s/",dir_output)) 121 | nasa = read.table(file_name) 122 | units = unique(nasa$V1) 123 | culmulate = rep(0,length(nasa)) 124 | for( u in units){ 125 | u_section = nasa[nasa$V1==u,] 126 | u_arima_result = c() 127 | u_boxtest_result = c() 128 | for( col in c(3:(length(u_section)))){ 129 | series = u_section[,col] 130 | arima_model = auto.arima(series) 131 | arima_residuals = arima_model$residuals 132 | boxtest = Box.test(arima_residuals,lag = 20,type = "Ljung-Box") 133 | u_arima_result = append(u_arima_result, 134 | sprintf("(%g %g %g)", 135 | arima_model$arma[1], 136 | arima_model$arma[length(arima_model$arma)-1], 137 | arima_model$arma[2])) 138 | #u_boxtest_result = append(u_boxtest_result,sprintf("%f",boxtest$p.value)) 139 | u_boxtest_result = append(u_boxtest_result,boxtest$p.value) 140 | 141 | } 142 | print(sprintf("------the %g unit-------",u)) 143 | print(colnames(u_section)[-c(1:2)]) 144 | print(u_arima_result) 145 | print(u_boxtest_result) 146 | print("----------------------------------") 147 | 148 | ## plot the smallest 3 picture 149 | tmp_pvalue = na.omit(u_boxtest_result) ## just omit the NAN value, 150 | if(!is.null(constant_cols)){ 151 | tmp_pvalue = na.omit(u_boxtest_result[-constant_cols]) ## omit the NAN and constant columns 152 | } 153 | min_pvalues = c() 154 | min_indexs = c() 155 | for (i in c(1:3)) { 156 | min_pvalue = min(tmp_pvalue) 157 | cur_col = match(min_pvalue,u_boxtest_result) 158 | cur_col_original = cur_col+2; # add shift 159 | culmulate[cur_col_original] = culmulate[cur_col_original] + 1 160 | series = u_section[,cur_col_original] 161 | 162 | png_path = sprintf("%s/uinit%g_r%g_v%g.png",dir_output,u,i,(cur_col_original)) 163 | png(filename = png_path,width = 2048,height = 768) 164 | sliding_window(series,length(series)*length_rate) 165 | dev.off() 166 | 167 | min_pvalues = append(min_pvalues,u_boxtest_result[cur_col]) 168 | min_indexs = append(min_indexs,(cur_col_original)) 169 | tmp_pvalue = tmp_pvalue[-c(which.min(tmp_pvalue))] 170 | } 171 | 172 | print("----------------------------------") 173 | print("the 3 smallest param:") 174 | print(min_pvalues) 175 | print(min_indexs) 176 | print("----------------------------------") 177 | cat("\n\n\n") 178 | 179 | } 180 | print("-----------------the times of each parameter become the indicator -----------------------") 181 | print( names(nasa) ) 182 | print( culmulate ) 183 | print("-----------------------------------------------------------------------------------------") 184 | unlink(sprintf("%s_result.txt",file_name)) 185 | sink() 186 | 187 | } -------------------------------------------------------------------------------- /an_igbt_multistate.R: -------------------------------------------------------------------------------- 1 | igbt = read.csv('data/final_dataset/igbt_resample_mean_10.csv',header = TRUE) # resample by step 10 2 | igbt_old = read.table("data/final_dataset/IIGBTAgingData.txt") 3 | 4 | igbt_COLLECTOR_CURRENT = igbt_old[290000:nrow(igbt_old),2] 5 | igbt_COLLECTOR_VOLTAGE = igbt_old[290000:nrow(igbt_old),3] 6 | igbt_GATE_CURRENT = igbt_old[290000:nrow(igbt_old),4] 7 | igbt_GATE_VOLTAGE = igbt_old[290000:nrow(igbt_old),5] 8 | igbt_HEAT_SINK_TEMP = igbt_old[290000:nrow(igbt_old),6] 9 | igbt_PACKAGE_TEMP = igbt_old[290000:nrow(igbt_old),7] 10 | 11 | 12 | igbt_density_plot <- function(series){ 13 | section1 = series[1:5000] 14 | section2 = series[5001:10000] 15 | section3 = series[10001:length(series)] 16 | 17 | ## density plot 18 | 19 | density_sec1 = density(section1) 20 | density_sec2 = density(section2) 21 | density_sec3 = density(section3) 22 | 23 | color = c('black','blue','red') 24 | legend_text = c(sprintf("index from 1 to 5000"), 25 | sprintf("index from 5001 to 10000"), 26 | sprintf("index from 10001 to %g",length(series))) 27 | 28 | dev.new() 29 | par(mar = c(3, 4, 3, 1),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 30 | tcl = 0.5) 31 | plot(density_sec1, 32 | xlim = c( min(series)-sd(series),max(series)+sd(series) ), 33 | ylim = c( 0, max(max(density_sec1$y),max(density_sec2$y),max(density_sec3$y))), 34 | xlab='', 35 | main='', 36 | col=color[1], 37 | lwd = 3) 38 | lines(density_sec2,col=color[2],lwd = 3) 39 | lines(density_sec3,col=color[3],lwd = 3) 40 | legend('topleft',"(x,y)",legend_text, lty = c(1,1), lwd = c(2.5,2.5), col = color,bty='n',cex=1.5) 41 | 42 | } 43 | 44 | igbt_density_plot_period <- function(period_list){ 45 | series = concate_list(period_list) 46 | tail_len = 6 47 | unit_size = round((length(period_list)-tail_len)/2) 48 | section1 = concate_list(period_list[1:unit_size]) 49 | section2 = concate_list(period_list[(unit_size+1):(length(period_list)-tail_len)]) 50 | section3 = concate_list(period_list[(length(period_list)-tail_len+1):length(period_list)]) 51 | 52 | ## density plot 53 | 54 | density_sec1 = density(section1) 55 | density_sec2 = density(section2) 56 | density_sec3 = density(section3) 57 | 58 | color = c('black','blue','red') 59 | legend_text = c(sprintf("index from 1 to %g",length(section1)), 60 | sprintf("index from %g to %g",(length(section1)+1),(length(section1)+length(section2))), 61 | sprintf("index from %g to %g",(length(section1)+length(section2)+1),length(series))) 62 | 63 | dev.new() 64 | par(mar = c(3, 4, 3, 1),cex.axis = 1.5, cex.lab = 1.5,cex.main=1.5,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 65 | tcl = 0.5) 66 | plot(density_sec1, 67 | xlim = c( min(series)-sd(series),max(series)+sd(series) ), 68 | ylim = c( 0, max(max(density_sec1$y),max(density_sec2$y),max(density_sec3$y))), 69 | xlab='', 70 | main='', 71 | col=color[1], 72 | lwd = 3) 73 | lines(density_sec2,col=color[2],lwd = 3) 74 | lines(density_sec3,col=color[3],lwd = 3) 75 | legend('topleft',"(x,y)",legend_text, lty = c(1,1), lwd = c(2.5,2.5), col = color,bty='n',cex=2) 76 | 77 | } 78 | 79 | igbt_loc.test_period <- function(period_list){ 80 | series = concate_list(period_list) 81 | tail_len = 6 82 | unit_size = round((length(period_list)-tail_len)/2) 83 | section1 = concate_list(period_list[1:unit_size]) 84 | section2 = concate_list(period_list[(unit_size+1):(length(period_list)-tail_len)]) 85 | section3 = concate_list(period_list[(length(period_list)-tail_len+1):length(period_list)]) 86 | 87 | 88 | print("------------t.test--------------") 89 | print("section1 vs section2") 90 | print(t.test(section1,section2)) 91 | print("section1 vs section3") 92 | print(t.test(section1,section3)) 93 | print("section2 vs section3") 94 | print(t.test(section2,section3)) 95 | 96 | print("------------wilcox.test---------") 97 | print("section1 vs section2") 98 | print(wilcox.test(section1,section2)) 99 | print("section1 vs section3") 100 | print(wilcox.test(section1,section3)) 101 | print("section2 vs section3") 102 | print(wilcox.test(section2,section3)) 103 | 104 | } 105 | 106 | igbt_loc.test_section <- function(period_list){ 107 | series = concate_list(period_list) 108 | section1 = series[1:5000] 109 | section2 = series[5001:10000] 110 | section3 = series[10001:length(series)] 111 | 112 | 113 | print("------------t.test--------------") 114 | print("section1 vs section2") 115 | print(t.test(section1,section2)) 116 | print("section1 vs section3") 117 | print(t.test(section1,section3)) 118 | print("section2 vs section3") 119 | print(t.test(section2,section3)) 120 | 121 | print("------------wilcox.test---------") 122 | print("section1 vs section2") 123 | print(wilcox.test(section1,section2)) 124 | print("section1 vs section3") 125 | print(wilcox.test(section1,section3)) 126 | print("section2 vs section3") 127 | print(wilcox.test(section2,section3)) 128 | 129 | } 130 | 131 | 132 | # ########## density plot by sections 133 | # igbt_density_plot(igbt_COLLECTOR_CURRENT) 134 | # igbt_density_plot(igbt_COLLECTOR_VOLTAGE) 135 | # igbt_density_plot(igbt_GATE_CURRENT) 136 | # igbt_density_plot(igbt_GATE_VOLTAGE) 137 | # igbt_density_plot(igbt_HEAT_SINK_TEMP) 138 | # igbt_density_plot(igbt_PACKAGE_TEMP) 139 | 140 | ###### density plot by periods 141 | 142 | 143 | # ############### split by period 144 | # series = igbt_GATE_CURRENT 145 | # series_seg = split_by_cycle_2mode(series) 146 | # series_seg_filter = eliminate_noise(series_seg) 147 | # series_filter = concate_list(series_seg_filter) 148 | # igbt_density_plot_period(series_seg_filter) 149 | # #igbt_loc.test_period(series_seg_filter) 150 | 151 | ############ arima 152 | # series = igbt_PACKAGE_TEMP[1:8000] 153 | # tail_len = 2000 154 | # series = igbt_PACKAGE_TEMP 155 | # section1 = series[1:5000] 156 | # section2 = series[5001:10000] 157 | # section3 = series[10001:length(series)] 158 | # tail_len = length(section3) 159 | # train_series = series[1:(length(series)-tail_len)] 160 | # arima.model = auto.arima(train_series) 161 | # print(summary(arima.model)) 162 | # predic.result = forecast.Arima(arima.model,h=tail_len,level = 95) 163 | # 164 | # win.graph(13,7) 165 | # par(mar = c(3, 4, 3, 2),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 166 | # tcl = 0.5) 167 | # plot(series,xlab='Time',ylab='Measured Value',ylim=c(250,max(series))) 168 | # points(c((length(train_series)+1):length(series)),predic.result$mean,col='red',lwd=4) 169 | # points( c((length(train_series)+1):length(series)), predic.result$lower, col='blue', lwd=4 ) 170 | # points(c((length(train_series)+1):length(series)), predic.result$upper, col='blue', lwd=4) 171 | 172 | 173 | 174 | 175 | ############ arima with flunctuation 176 | series = igbt_PACKAGE_TEMP 177 | # tail_len = 2000 178 | #series = timeseries 179 | tail_len = 2000 180 | train_series = series[1:(length(series)-tail_len)] 181 | arima.model = auto.arima(train_series) 182 | print(summary(arima.model)) 183 | predic.result = forecast.Arima(arima.model,h=tail_len,level = 95) 184 | 185 | win.graph(30,7) 186 | par(mar = c(3, 4, 3, 2),cex.axis = 2, cex.lab = 2,cex.main=2,oma = c(0, 1, 0, 0),mgp = c(2, 0.6, 0), 187 | tcl = 0.5) 188 | plot(series,xlab='Time',ylab='Measured Value',ylim=c(min(series),max(series)),type='b') 189 | points(c((length(train_series)+1):length(series)),predic.result$mean,col='red',lwd=3) 190 | points( c((length(train_series)+1):length(series)), predic.result$lower, col='blue', lwd=3 ) 191 | points(c((length(train_series)+1):length(series)), predic.result$upper, col='blue', lwd=3) 192 | -------------------------------------------------------------------------------- /an_ge.R: -------------------------------------------------------------------------------- 1 | #' @description realized the calculation of BPRate based on the input BP 2 | ge_get_BPRate <- function(BP){ 3 | BP = as.numeric(BP) 4 | BPRate = rep(NA,5) 5 | for(i in c(6:length(BP))){ 6 | BPRate = append(BPRate,(BP[i-5]-BP[i])) 7 | } 8 | return(BPRate) 9 | } 10 | 11 | #' @description Plot 'series' by segment and the size of each segment is spercified by the param 'segsize'. 12 | #' Each plot have a small overlap part, which is for better visualization. 13 | segment_plot_series <- function(series, 14 | times, 15 | segsize=1000, 16 | out_dir="BP_BPRate_segment_plot", 17 | add_gaussian_line = FALSE, 18 | windowsize = 0.5 * segsize){ 19 | dir.create(out_dir) 20 | seg_list = seq(1,length(series),segsize) 21 | indexs = c(1:length(series)) 22 | if(add_gaussian_line){ 23 | data_smt_gaussian = smth.gaussian(series,window = windowsize) 24 | } 25 | 26 | # the first segment: 27 | seg_values = series[seg_list[1]:(seg_list[2]-1)] 28 | seg_times = times[seg_list[1]:(seg_list[2]-1)] 29 | seg_index = indexs[seg_list[1]:(seg_list[2]-1)] 30 | seg_name = sprintf("from_%s_to_%s",seg_times[1],seg_times[length(seg_times)]) 31 | 32 | png_path = sprintf("%s/f%g_%s.png",out_dir,1,seg_name) 33 | png(filename = png_path,width = 2048,height = 768) 34 | plot(seg_index,seg_values,type='b') 35 | if(add_gaussian_line){ 36 | lines(data_smt_gaussian[seg_list[1]:(seg_list[2]-1)],type='b',col="red") 37 | } 38 | dev.off() 39 | 40 | for( i in c(2:(length(seg_list)-1)) ){ 41 | seg_start = (seg_list[i]-0.1*segsize) 42 | seg_end = (seg_list[i+1]-1) 43 | seg_values = series[seg_start:seg_end] 44 | seg_index = indexs[seg_start:seg_end] 45 | seg_times = times[seg_start:seg_end] 46 | seg_name = sprintf("from_%s_to_%s",seg_times[1],seg_times[length(seg_times)]) 47 | 48 | png_path = sprintf("%s/f%g_%s.png",out_dir,i,seg_name) 49 | png(filename = png_path,width = 2048,height = 768) 50 | plot(seg_index,seg_values,type='b') 51 | if(add_gaussian_line){ 52 | lines(data_smt_gaussian[seg_start:seg_end],type='b',col="red") 53 | } 54 | dev.off() 55 | } 56 | 57 | } 58 | 59 | #' @description 60 | #' Compute different statistic indicators over a sliding window, which is spercified by the 61 | #' param 'methods'. And then plot by segment and size is spercified by the param 'segsize' 62 | #' and then save the plots below the dir 'output_dir' 63 | ge_rolling_analysis <- function(series, 64 | times, 65 | windowsize = 0.5*length(series), 66 | methods=c('var','cv','mean','skew','kurtosi','autocorr'), 67 | output_dir = "statistics_indicators", 68 | segsize = 1000) 69 | { 70 | print("------------------------caculate statistics indicator------------") 71 | output_dir = sprintf("%s_winsize.%g",output_dir,windowsize) 72 | dir.create(output_dir) 73 | #windowsize = length(series)*windowsize_rate 74 | for(method in methods){ 75 | rolling_result = c() 76 | rolling_result = switch (method, 77 | 'var' = window_var(series,windowsize = windowsize), 78 | 'cv' = window_cv(series,windowsize = windowsize), 79 | 'mean' = window_mean(series,windowsize = windowsize), 80 | 'skew' = window_skew(series,windowsize = windowsize), 81 | 'kurtosi' = window_kurtosi(series,windowsize = windowsize), 82 | 'autocorr' = window_autocorr(series,windowsize = windowsize), 83 | 'ar.ols' = window_ar.ols(series,windowsize = windowsize) 84 | ) 85 | times = times[c((length(times)-length(rolling_result)+1):length(times))] 86 | segment_plot_series(rolling_result, 87 | times, 88 | segsize = segsize, 89 | out_dir=sprintf("%s/%s/",output_dir,method)) 90 | print(sprintf("it is done to calculate the inticator %s",method)) 91 | print(sprintf("the length of series is %g",length(times))) 92 | print(sprintf("time from %s to %s",times[1],times[length(times)])) 93 | 94 | } 95 | 96 | print("------------------------------all done-----------------------------") 97 | 98 | } 99 | 100 | #' @description plot original time-series and leading indicator 101 | ge_analysis_original <- function(file_dir,resample_size = 10){ 102 | dir_split = unlist(strsplit(file_dir,'/')) 103 | filename = dir_split[length(dir_split)] 104 | ge_data = read.csv(file_dir,header = TRUE) 105 | start_time = sprintf("%s , %s ",ge_data$Date[1],ge_data$Time[1]) 106 | end_time = sprintf("%s , %s ",ge_data$Date[length(ge_data$Date)],ge_data$Time[length(ge_data$Time)]) 107 | ge_data = ge_data[,columname] 108 | ge_data = na.omit(ge_data) 109 | ge_data = ge_data[ge_data$BP!='---',] 110 | ge_data_sample = ge_data[seq(1,length(ge_data$BP),resample_size),] 111 | print("------------------------------------------------------") 112 | print(sprintf("----------analysis result for %s file --------",filename)) 113 | print(sprintf("original dataset size : %g ",length(ge_data$BP))) 114 | print(sprintf("resample dataset size : %g ",length(ge_data_sample$BP))) 115 | print(sprintf("the time interval is :")) 116 | print(start_time) 117 | print(end_time) 118 | print("------------------------------------------------------") 119 | win.graph(200,80) 120 | plot(ge_data_sample$BP,type='b',main="original time series plot") 121 | sliding_window(as.numeric(ge_data_sample$BP)) 122 | } 123 | 124 | #' @description get ARIMA model and Ljung-Box test result, original data plot, indicators plot 125 | ge_analysis_arima <- function(file_dir,resample_size = 10){ 126 | dir_split = unlist(strsplit(file_dir,'/')) 127 | filename = dir_split[length(dir_split)] 128 | ge_data = read.csv(file_dir,header = TRUE) 129 | start_time = sprintf("%s , %s ",ge_data$Date[1],ge_data$Time[1]) 130 | end_time = sprintf("%s , %s ",ge_data$Date[length(ge_data$Date)],ge_data$Time[length(ge_data$Time)]) 131 | ge_data = ge_data[,columname] 132 | ge_data = na.omit(ge_data) 133 | ge_data = ge_data[ge_data$BP!='---',] 134 | ge_data_sample = ge_data[seq(1,length(ge_data$BP),resample_size),] 135 | ge_data_arima_model = auto.arima(as.numeric(ge_data_sample$BP)) 136 | ge_data_arima_residuals = ge_data_arima_model$residuals 137 | test_result = Box.test(ge_data_arima_residuals,lag = 20,type="Ljung-Box") 138 | print("------------------------------------------------------") 139 | print(sprintf("----------analysis result for %s file --------",filename)) 140 | print(sprintf("original dataset size : %g ",length(ge_data$BP))) 141 | print(sprintf("resample dataset size : %g ",length(ge_data_sample$BP))) 142 | print(sprintf("the time interval is :")) 143 | print(start_time) 144 | print(end_time) 145 | print("arima model:") 146 | print(ge_data_arima_model) 147 | print("Ljung-Box Test Result: ") 148 | print(test_result) 149 | print("------------------------------------------------------") 150 | win.graph(200,80) 151 | plot(ge_data_arima_residuals,type='b',main="residual plot") 152 | sliding_window(ge_data_arima_residuals) 153 | } 154 | 155 | ################################################################################################## 156 | #' @description analysis ge_data by file. Arima model, smooth, Ljung-Box Test, rolling indicators. 157 | ge_analysis_unit <- function(root, 158 | filename, 159 | length_rate=0.5, 160 | smt_method = c('Original','Gaussian'), 161 | bandwidth_rate = 0.5 162 | ){ 163 | # read data 164 | ge_data = read.csv(sprintf("%s/%s",root,filename), header = TRUE) 165 | leave_columname = c('Date','Time','KM','Meters','Speed','Trac.Effort','BP','BC','ER','Fuel','BP.Flow') 166 | ge_data = ge_data[,leave_columname] 167 | ge_data = na.omit(ge_data) 168 | ge_data = ge_data[ge_data$BP!='---',] 169 | 170 | if(length(ge_data$BP)<500) ## data set too small 171 | { 172 | print(sprintf("data length is %g, too small",length(ge_data$BP))) 173 | return() 174 | } 175 | 176 | 177 | # resample, interval is 10s 178 | #ge_data = ge_data[seq(1,length(ge_data$BP),10),] 179 | 180 | # calculate BP Rate 181 | BP = as.numeric(ge_data$BP) 182 | BPRate = c() 183 | for(i in c(6:length(BP))){ 184 | BPRate = append(BPRate,(BP[i-5]-BP[i])) 185 | } 186 | 187 | BP = BP[-c(1:5)] # re-scale BP 188 | ge_data = ge_data[-c(1:5),] # re-scale 189 | 190 | if(smt_method == 'Gaussian'){ 191 | BP = na.omit(smth.gaussian(BP,window = length(BP)*bandwidth_rate)) 192 | BPRate =na.omit( smth.gaussian(BP,window = length(BPRate)*bandwidth_rate)) 193 | } 194 | 195 | # plot sliding window analysis result 196 | png_path = sprintf("%s/BP_%s_%f/%s.png",root,smt_method[1],bandwidth_rate,filename) 197 | png(filename = png_path,width = 2048,height = 768) 198 | sliding_window(BP,length(BP)*length_rate) 199 | dev.off() 200 | 201 | png_path = sprintf("%s/BPRate_%s_%f/%s.png",root,smt_method[1],bandwidth_rate,filename) 202 | png(filename = png_path,width = 2048,height = 768) 203 | sliding_window(BPRate,length(BPRate)*length_rate) 204 | dev.off() 205 | 206 | # print result 207 | BP_arima_model = auto.arima(BP) 208 | BPRate_arima_model = auto.arima(BPRate) 209 | BP_arima_residuals = BP_arima_model$residuals 210 | BPRate_arima_residuals = BPRate_arima_model$residuals 211 | 212 | BP_test_result = Box.test(BP_arima_residuals,lag = 20,type="Ljung-Box") 213 | BPRate_test_result = Box.test(BPRate_arima_residuals,lag = 20,type="Ljung-Box") 214 | 215 | start_time = sprintf("%s , %s ",ge_data$Date[1],ge_data$Time[1]) 216 | end_time = sprintf("%s , %s ",ge_data$Date[length(ge_data$Date)],ge_data$Time[length(ge_data$Time)]) 217 | 218 | print(sprintf("dataset size : %g ",length(BP))) 219 | print(sprintf("the time interval is :")) 220 | print(start_time) 221 | print(end_time) 222 | print("---BP series result: ") 223 | print("arima model:") 224 | print(BP_arima_model) 225 | print("Ljung-Box Test: ") 226 | print(BP_test_result) 227 | print("---BPRate series result: ") 228 | print("arima model:") 229 | print(BPRate_arima_model) 230 | print("Ljung-Box Test: ") 231 | print(BPRate_test_result) 232 | 233 | } 234 | 235 | # The main function of analysis ge data 236 | ge_analysis <- function(root, 237 | length_rate=0.5, 238 | smt_method = c('Original','Gaussian'), 239 | bandwidth_rate = 0.5){ 240 | file_list = list.files(root,pattern = ".csv") 241 | dir.create(sprintf("%s/BP_%s_%f",root,smt_method[1],bandwidth_rate)) 242 | dir.create(sprintf("%s/BPRate_%s_%f",root,smt_method[1],bandwidth_rate)) 243 | output_file = sprintf("%s/result_%s_%f.txt",root,smt_method[1],bandwidth_rate) 244 | sink(output_file) 245 | for( filename in file_list){ 246 | print(sprintf("------------- file: %s ---------------",filename)) 247 | ge_analysis_unit(root, 248 | filename, 249 | length_rate, 250 | smt_method = smt_method, 251 | bandwidth_rate = bandwidth_rate) 252 | print("----------------------------------------------") 253 | cat("\n\n") 254 | } 255 | unlink(output_file) 256 | sink() 257 | } 258 | ############################################################################################ 259 | 260 | 261 | 262 | 263 | 264 | -------------------------------------------------------------------------------- /an_common.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' plot the different statistic indicators of rolling window analysis for series 4 | #' 5 | sliding_window <- function(series, 6 | win.rate=0.5, 7 | detrending = c('no','gaussian','linear','quadratic','first-diff'), 8 | band.rate = 0.1){ 9 | 10 | detrending = match.arg(detrending) 11 | df.indicators = lead.indicators(series, 12 | win.rate = win.rate, 13 | detrending = detrending, 14 | band.rate = band.rate) 15 | 16 | dev.new() 17 | par(mar = (c(2, 2, 0, 1) + 0), oma = c(7, 2, 3, 1), mfrow = c(4, 2)) 18 | 19 | plot(series,xlab='',ylab='',type='b') 20 | 21 | xlim = c(1,df.indicators$timeindex[nrow(df.indicators)]) 22 | names = colnames(df.indicators) 23 | for(i in 2:ncol(df.indicators)){ 24 | indic = df.indicators[,i] 25 | ken = MannKendall(na.omit(indic)) 26 | plot(df.indicators$timeindex,indic, 27 | xlab='',ylab='',type='l',las = 1,xlim=xlim) 28 | legend("left",paste("Kendall tau=",round(ken$tau[1],digits = 3)),bty = 'n') 29 | legend("topleft",sprintf("%s",names[i]),bty = 'n') 30 | } 31 | mtext("Generic Early-Warnings", side = 3, line = 0.2, outer = TRUE) #outer=TRUE print on the outer margin 32 | 33 | } 34 | 35 | #' Dive 'series' into two part and plot density for this two part 36 | #' at the same figure for comparing. The diving point is specified by 'critical' 37 | gaussian_density <- function(series,critical){ 38 | win.graph() 39 | former_density = density(series[1:critical],kernel = "gaussian") 40 | latter_density = density(series[(critical+1):length(series)],kernel = "gaussian") 41 | color = c("blue","red") 42 | legend_text = c(sprintf("index from 1 to %g (critical=%g)", 43 | index(series)[critical],critical), 44 | sprintf("index from %g to the end", 45 | index(series)[critical+1])) 46 | plot( former_density, 47 | xlim = c(min(series)-sd(series),max(series)+sd(series)), 48 | ylim = c(0,max(max(former_density$y),max(latter_density$y)))) 49 | #lines(latter_density) 50 | polygon(former_density, col=color[1], border=color[1]) 51 | polygon(latter_density, col=color[2], border=color[2]) 52 | legend("topleft", "(x,y)",legend_text,lty=c(1,1), lwd=c(2.5,2.5), col=color) 53 | } 54 | 55 | #' get all statistic indicator -- old 56 | get_indicators <- function(time,series,windowsize = NULL){ 57 | variance = window_var(series,windowsize = windowsize) 58 | var_kendall = 0 59 | if(sum(is.na(variance)) == 0){ 60 | var_kendall = MannKendall(variance) 61 | } 62 | autocorrelation = window_autocorr(series,windowsize = windowsize) 63 | auto_kendall = 0 64 | if( sum( is.na(autocorrelation) ) == 0 ){ 65 | auto_kendall = MannKendall(autocorrelation) 66 | } 67 | skewness = window_skew(series,windowsize = windowsize) 68 | skew_kendall = 0 69 | if( sum( is.na( skewness ) ) == 0 ){ 70 | skew_kendall = MannKendall(skewness) 71 | } 72 | means = window_mean(series,windowsize = windowsize) 73 | mean_kendall = 0 74 | if( sum( is.na( means ) ) == 0 ){ 75 | mean_kendall = MannKendall(means) 76 | 77 | } 78 | covariation = window_cv(series,windowsize = windowsize) 79 | kurtosis = window_kurtosi(series,windowsize = windowsize) 80 | 81 | return(data.frame(time,variance,autocorrelation,skewness,means,covariation,kurtosis)) 82 | } 83 | 84 | # calculate leading indicator: 85 | lead.indicators <- function(x, 86 | indicators = c('var','ar1','acf1','skew','cv','kurtosis','mean'), 87 | win.rate = 0.5, 88 | detrending = c('no','gaussian','linear','quadratic','first-diff'), 89 | band.rate = 0.1 90 | ) 91 | { 92 | # detrend 93 | detrending = match.arg(detrending) 94 | xsm = x 95 | if( detrending != 'no'){ 96 | xsm = detrend(x,method = detrending,band.rate = band.rate) 97 | } 98 | 99 | win.size = round( win.rate * length(xsm) ) 100 | df.indicators = data.frame(time=win.size:length(xsm)) 101 | for( i in 1:length(indicators)){ 102 | name.indicator = indicators[i] 103 | value.indicator = numeric() 104 | if( name.indicator == 'var'){ 105 | variance = sapply(win.size:length(xsm), function(k){ 106 | var(xsm[(k-win.size+1):k],na.rm = TRUE) 107 | }) 108 | df.indicators = cbind(df.indicators,variance) 109 | print("finish to calculate the variance") 110 | }else if( name.indicator == 'ar1' ){ 111 | ar1 = sapply(win.size:length(xsm), function(k){ 112 | ret = ar.ols(xsm[(k-win.size+1):k],demean = TRUE,order.max = 1,aic = FALSE,intercept = FALSE) 113 | ret$ar[1] 114 | }) 115 | df.indicators = cbind(df.indicators,ar1) 116 | print("finish to calculate the ar1") 117 | }else if( name.indicator == 'acf1'){ 118 | acf1 = sapply(win.size:length(xsm), function(k){ 119 | ACF = acf(xsm[(k-win.size+1):k],type='correlation',plot=FALSE,lag.max = 1) 120 | ACF$acf[2] 121 | }) 122 | df.indicators = cbind(df.indicators,acf1) 123 | print("finish to calculate the acf1") 124 | }else if( name.indicator== 'skew' ){ 125 | sk = sapply(win.size:length(xsm), function(k){ 126 | sk = moments::skewness(xsm[(k-win.size+1):k],na.rm = TRUE) 127 | }) 128 | df.indicators = cbind(df.indicators,sk) 129 | print("finish to calculate the skew") 130 | }else if( name.indicator == 'cv'){ 131 | cv = sapply(win.size:length(xsm), function(k){ 132 | var(xsm[(k-win.size+1):k])/mean(xsm[(k-win.size+1):k]) 133 | }) 134 | df.indicators = cbind(df.indicators,cv) 135 | print("finish to calculate the cv") 136 | }else if( name.indicator == 'kurtosis'){ 137 | kur = sapply(win.size:length(xsm), function(k){ 138 | kurtosis(xsm[(k-win.size+1):k]) 139 | }) 140 | df.indicators = cbind(df.indicators,kur) 141 | print("finisht to calculate the kur") 142 | }else if( name.indicator == 'mean'){ 143 | mean = sapply(win.size:length(xsm), function(k){ 144 | mean(xsm[(k-win.size+1):k]) 145 | }) 146 | df.indicators = cbind(df.indicators,mean) 147 | print("finish to calculate the mean") 148 | } 149 | } 150 | colnames(df.indicators) = append('timeindex',indicators) 151 | return(df.indicators) 152 | 153 | } 154 | 155 | # calculate leading indicator: 156 | lead.indicators.parallel <- function(x, 157 | indicators = c('var','ar1','acf1','skew','cv','kurtosis','mean'), 158 | win.rate = 0.5, 159 | detrending = c('no','gaussian','linear','quadratic','first-diff'), 160 | band.rate = 0.1 161 | ) 162 | { 163 | no_cores = detectCores() - 2 164 | cl = makeCluster(no_cores) 165 | # detrend 166 | detrending = match.arg(detrending) 167 | xsm = x 168 | if( detrending != 'no'){ 169 | xsm = detrend(x,method = detrending,band.rate = band.rate) 170 | } 171 | 172 | win.size = round( win.rate * length(xsm) ) 173 | df.indicators = data.frame(time=win.size:length(xsm)) 174 | for( i in 1:length(indicators)){ 175 | name.indicator = indicators[i] 176 | value.indicator = numeric() 177 | if( name.indicator == 'var'){ 178 | variance = parSapply(cl,win.size:length(xsm), function(k){ 179 | var(xsm[(k-win.size+1):k],na.rm = TRUE) 180 | }) 181 | df.indicators = cbind(df.indicators,variance) 182 | }else if( name.indicator == 'ar1' ){ 183 | ar1 = parSapply(cl,win.size:length(xsm), function(k){ 184 | ret = ar.ols(xsm[(k-win.size+1):k],demean = TRUE,order.max = 1,aic = FALSE,intercept = FALSE) 185 | ret$ar[1] 186 | }) 187 | df.indicators = cbind(df.indicators,ar1) 188 | }else if( name.indicator == 'acf1'){ 189 | acf1 = parSapply(cl,win.size:length(xsm), function(k){ 190 | ACF = acf(xsm[(k-win.size+1):k],type='correlation',plot=FALSE,lag.max = 1) 191 | ACF$acf[2] 192 | }) 193 | df.indicators = cbind(df.indicators,acf1) 194 | }else if( name.indicator== 'skew' ){ 195 | sk = parSapply(win.size:length(xsm), function(k){ 196 | sk = skew(xsm[(k-win.size+1):k],na.rm = TRUE) 197 | }) 198 | df.indicators = cbind(df.indicators,sk) 199 | }else if( name.indicator == 'cv'){ 200 | cv = parSapply(cl,win.size:length(xsm), function(k){ 201 | var(xsm[(k-win.size+1):k])/mean(xsm[(k-win.size+1):k]) 202 | }) 203 | df.indicators = cbind(df.indicators,cv) 204 | }else if( name.indicator == 'kurtosis'){ 205 | kur = parSapply(win.size:length(xsm), function(k){ 206 | kurtosis(xsm[(k-win.size+1):k]) 207 | }) 208 | df.indicators = cbind(df.indicators,kur) 209 | }else if( name.indicator == 'mean'){ 210 | mean = parSapply(cl,win.size:length(xsm), function(k){ 211 | mean(xsm[(k-win.size+1):k]) 212 | }) 213 | df.indicators = cbind(df.indicators,mean) 214 | } 215 | } 216 | stopCluster(cl) 217 | colnames(df.indicators) = append('timeindex',indicators) 218 | return(df.indicators) 219 | 220 | } 221 | 222 | 223 | #' return a series, which is the result of smoothing original series 'x' 224 | detrend <- function(x, 225 | method = c('gaussian','linear','quadratic','first-diff'), 226 | band.rate = 0.1){ 227 | method <- match.arg(method) 228 | timeindex = index(x) 229 | y = x 230 | if(method == 'gaussian'){ 231 | bw =round(length(x)*band.rate) 232 | ksmY = ksmooth(timeindex,x,kernel = "normal", bandwidth =bw, range.x = range(timeindex),x.points = timeindex) 233 | y = y - ksmY$y 234 | }else if(method == 'linear'){ 235 | y = lm(x ~ c(1:length(x)))$residuals 236 | }else if(method == 'quadratic'){ 237 | y = lm( x ~ c(1:length(x)) + I(c(1:length(x))^2))$residuals 238 | }else if(method == 'first-diff'){ 239 | y = diff(x) 240 | } 241 | 242 | return(y) 243 | } 244 | 245 | #' variogram can be used to determine the stability of time series x 246 | variogram <- function(x,lag.max = 20) 247 | { 248 | acf.result = acf(x,lag.max = lag.max) 249 | acf.value = acf.result$acf[2:length(acf.result$acf)] 250 | 251 | variogram.value = (1-acf.value)/(1-acf.value[1]) 252 | 253 | return(variogram.value) 254 | } 255 | 256 | ####### Location test 257 | location_test_plot <- function(series,num2mu=round(length(series)/3)){ 258 | mu = mean(series[1:num2mu]) 259 | re.t = c() 260 | re.wilcox = c() 261 | re.index = c() 262 | i = num2mu 263 | while(i