[fbf06f]: / partyMod / inst / RR / Rieger_RF_NA / Ozone.R

Download this file

119 lines (88 with data), 3.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
##############################################################################
# #
# R-Code zur HiWi-Stelle #
# #
# Berechnungen am realen Datensatz "Ozone" #
# #
##############################################################################
library(mlbench)
data(Ozone)
# fünf fehlende Werte im Response V4 -> rausschmeißen
Ozone <- Ozone[-which(is.na(Ozone$V4)), ]
library("impute", lib.loc = "~/lib/oldR")
packageDescription("impute")$Version
#library("mvtnorm")
#packageDescription("mvtnorm")$Version
library("party", lib.loc = "~/lib/oldR")
packageDescription("party")$Version
nIter <- 100
MSE <- impMSE <- vector("numeric")
set.seed(856329)
for (i in 1:nIter) {
rf <- try(cforest(V4 ~ ., data = dat[1,],
control = cforest_control(maxsurrogate=3, ntree=50, minsplit=30)), silent=TRUE)
while(inherits(rf, "try-error")) {
bootstrap <- sample(1:dim(Ozone)[1], replace=TRUE)
dat <- Ozone[bootstrap, ] # genauso groß wie Original-Stpr.
MSE[i] <- impMSE[i] <- NA
# RF + Surrogat
#--------------
cat("Surrogates ")
rf <- try(cforest(V4 ~ ., data = dat,
control = cforest_control(maxsurrogate = 3, ntree = 50, minsplit = 30)))
}
print(i)
oob <- Ozone[-bootstrap, ]
f <- predict(rf, newdata = oob)
MSE[i] <- mean((oob$V4 - f)^2)
# Imputation + RF
#----------------
cat("Imputation ")
dat[-1] <- as.data.frame(impute.knn(as.matrix(dat[-1]))$data)
for(j in 1:3) {
dat[, paste("V", j, sep="")] <- as.factor(dat[, paste("V", j, sep="")])
}
for(j in 4:13) {
dat[, paste("V", j, sep="")] <- as.numeric(dat[, paste("V", j, sep="")])
}
rf <- cforest(V4 ~ ., data = dat,
control = cforest_control(maxsurrogate = 3, ntree = 50, minsplit = 30))
print(i)
f <- predict(rf, newdata = oob)
impMSE[i] <- mean((oob$V4 - f)^2)
}
boxplot(MSE - impMSE, main = "Differences between RF + Surrogate and Imputation + RF")
abline(h=0)
boxplot(MSE, impMSE, MSE-impMSE, names=c("RF + surrogate", "Imputation + RF",
"differences"))
abline(h=0)
#------------------------------------------------------------------------------#
#save(MSE, impMSE, file = "Ozone.Rdata")
load("Ozone.Rdata")
#------------------------------------------------------------------------------#
MSE <- as.data.frame(MSE)
colnames(MSE) <- "risk"
ozon <- cbind(MSE, imp = FALSE)
head(ozon)
summary(ozon)
impMSE <- as.data.frame(impMSE)
colnames(impMSE) <- "risk"
ozon2 <- cbind(impMSE, imp = TRUE)
head(ozon2)
summary(ozon2)
ozon3 <- rbind(ozon, ozon2)
head(ozon3)
summary(ozon3)
ozon <- ozon3
#------------------------------------------------------------------------------#
#save(ozon, file = "ozon.Rdata")
load("ozon.Rdata")
#------------------------------------------------------------------------------#
library(lattice)
ozon$imput <- as.factor(ozon$imp)
levels(ozon$imput) <- c("sur", "knn")
pdf(file = "Ozone.pdf", width = 5, height = 5)
bwplot(risk ~ imput, data = ozon, cex.lab = 1.5, cex.axis = 1.5, font = 2)
dev.off()
t.test(risk ~ imput, data = ozon, paired = FALSE)
################################################################################