### 設備參數設定與發生不良類型分析

 情境： 另解 一 二 某製程需要有兩種機台 (甲與乙) 共同操作。 生產時，甲機台有兩項參數可調整 (FactorX1, FactorY1)。 生產時，乙機台有兩項參數可調整 (FactorX2, FactorY2)。 機台參數調整後，導至製成品發生三種變異 (A, B, C)。 問題： 是否可以在產生時，藉由機台參數之設定，便可預判製成品變異種類？

```  訓練.A  訓練.B 訓練.C  測試.A  測試.B 測試.C
A     45      0      0      5      0      0
B      0     37      4      0     12      1
C      0      1     34      0      0     11
```

```library(party)

rm(list=ls())

sink("RE-5.log")

RE.TRAIN <- NULL
RE.TEST <- NULL

RE.Summary <- NULL
RE.NumFields <- NULL

LoadData <- function(csvFile='RE.csv') {
RE.Data <- read.csv(csvFile)
}

SummaryData <- function(RE=RE.Data) {
options(digits=1)

fieldNames <- colnames(RE)
i = 0
for (fieldName in fieldNames) {
i = i + 1
if (class(RE[, fieldName]) == 'numeric') {
assign('RE.NumFields', c(RE.NumFields, i), envir = .GlobalEnv)
Summary.Field <- c(summary(RE[, fieldName]), sd(RE[, fieldName]))

if (is.null(RE.Summary)) {
assign('RE.Summary', Summary.Field, envir = .GlobalEnv)
}
else {
assign('RE.Summary', rbind(RE.Summary, Summary.Field), envir = .GlobalEnv)
}
}
}

i = 1
for (fieldName in fieldNames) {
if (class(RE[, fieldName]) == 'numeric') {
rownames(RE.Summary)[i] <- fieldName
i = i + 1
}
}

colnames(RE.Summary)[7] <- 'StdDev.'
RE.Summary
}

CoVarianceData <- function(RE=RE.Data) {
options(digits=2)

if (is.null(RE.NumFields)) {
fieldNames <- colnames(RE)
i = 0

for (fieldName in fieldNames) {
i = i + 1
if (class(RE[, fieldName]) == 'numeric') {
assign('RE.NumFields', c(RE.NumFields, i), envir = .GlobalEnv)
}
}
}

cov(RE[, RE.NumFields])

}

CoRelationData <- function(RE=RE.Data) {
options(digits=2)

if (is.null(RE.NumFields)) {
fieldNames <- colnames(RE)
i = 0

for (fieldName in fieldNames) {
i = i + 1
if (class(RE[, fieldName]) == 'numeric') {
assign('RE.NumFields', c(RE.NumFields, i), envir = .GlobalEnv)
}
}
}

cor(RE[, RE.NumFields])

}

SampleData <- function(TrainPortion=0.8, RE=RE.Data) {
sampleSeed <- floor(unclass(Sys.time()))
set.seed(sampleSeed)

TempData <- sample(2, nrow(RE), replace=TRUE, prob=c(TrainPortion, 1 - TrainPortion))

assign('RE.TRAIN', RE[TempData==1, ], envir = .GlobalEnv)
assign('RE.TEST', RE[TempData==2, ], envir = .GlobalEnv)
}

DicisionTree <- function(linearModel, factorField='Failure') {
RE.Model <- linearModel
RE.TREE <- ctree(RE.Model, data=RE.TRAIN)
RE.PREDICT <- predict(RE.TREE, newdata = RE.TEST)

S <- table(predict(RE.TREE), RE.TRAIN[, factorField])
colnames(S) <- paste('\u8a13\u7df4', colnames(S), sep='.')

T <- table(RE.PREDICT, RE.TEST[, factorField])
colnames(T) <- paste('\u6e2c\u8a66', colnames(T), sep='.')

RE.DECISION <- cbind(S, T)
print (RE.DECISION)

chartName <- paste('DT', 'RE', sep='-')
fileName <- paste(chartName, 'png', sep='.')
png(fileName, width=2048, height=2048)
plot(RE.TREE)
dev.off()
}

PlotCharts <- function(RE=RE.Data) {
fieldNames <- colnames(RE)

if (is.null(RE.NumFields)) {
i = 0

for (fieldName in fieldNames) {
i = i + 1
if (class(RE[, fieldName]) == 'numeric') {
assign('RE.NumFields', c(RE.NumFields, i), envir = .GlobalEnv)
}
}
}

for (fieldID in RE.NumFields) {
fieldName <- fieldNames[fieldID]
chartName <- paste('HIST', 'RE', fieldName, sep='-')
fileName <- paste(chartName, 'png', sep='.')

png(fileName, width=1024, height=768)
par(mfrow=c(1, 2))
hist(RE[, fieldName], xlab=fieldName, main='\u76f4\u65b9\u5716')
plot(density(RE[, fieldName]), main='\u5bc6\u5ea6\u5716')
dev.off()
}

chartName <- paste('COR', 'RE', sep='-')
fileName <- paste(chartName, 'png', sep='.')
png(fileName, width=4096, height=4096)
pairs(RE)
dev.off()
}

PlotScatterChart <- function(xFieldName, yFieldName, factorField='Failure', RE=RE.Data) {
Plot.Legend <- as.numeric(RE.Data[, factorField])
Plot.NumLegend <- max(Plot.Legend)

Plot.Colors <- rainbow(Plot.NumLegend)
Plot.Char <- seq(1, Plot.NumLegend+1, 1)

chartName <- paste('XY', 'RE', xFieldName, yFieldName, sep='-')
fileName <- paste(chartName, 'png', sep='.')
png(fileName, width=1024, height=960)
plot(RE.Data[, xFieldName], RE.Data[, yFieldName],
col=Plot.Colors,
pch=Plot.Legend,
cex=1, main='\u6563\u5e03\u5716',
xlab=xFieldName, ylab=yFieldName)
legend("topright",
col=Plot.Colors,
pch=Plot.Char,
cex=1,
legend=unique(RE.Data[, factorField]))
dev.off()
}

## Main

jobTask = c(FALSE, TRUE, FALSE)
RE.Data <- LoadData()

if (jobTask[1]) {
SummaryData(RE.Data)
CoRelationData(RE.Data)
CoVarianceData(RE.Data)
}

if (jobTask[2]) {
SampleData(0.7)
DicisionTree(Failure ~ FactorX1 + FactorY1 + FactorX2 + FactorY2 +
FactorX1 * FactorY1 + FactorX2 * FactorY2)
}

if (jobTask[3]) {
PlotCharts()
PlotScatterChart('FactorX1', 'FactorY1')
PlotScatterChart('FactorX2', 'FactorY2')
}

sink()```
ċ
RE-5.R
(5k)

2015年5月16日 上午4:01