Last active
December 20, 2015 04:19
-
-
Save ottadini/6069736 to your computer and use it in GitHub Desktop.
An attempt at prediction of TC using the kohonen package.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ===== # | |
library(kohonen) | |
# Load data, available at https://gist.github.com/ottadini/6068259 | |
somdata <- read.csv("somdata.csv") | |
# Create SCALED test and training sets from data: | |
inTrain <- sample(nrow(somdata), nrow(somdata)*(2/3)) | |
training <- scale(somdata[inTrain, ]) | |
testing <- scale(somdata[-inTrain, ], | |
center = attr(training, "scaled:center"), | |
scale = attr(training, "scaled:scale")) | |
# Attempting to follow the examples in Wehrens and Buydens, 2007, 21(5), J Stat Soft. | |
# Supervised kohonen map, where the dependent variable is MEAS_TC (somdata[1]). | |
somX <- training[, -1] # edit: Ron Wehrens | |
somY <- training[, 1] | |
somdata.xyf <- xyf(data=somX, Y=somY, contin=TRUE, rlen=500, | |
xweight=0.2, grid=somgrid(5, 5, "hexagonal")) | |
# Follow example from Wehrens & Buydens 2007 page 9: | |
tc.xyf <- predict(somdata.xyf, newdata=testing)$prediction # Y codebook vectors | |
tc.predict <- as.numeric(tc.xyf) | |
plot(somdata.xyf, type="property", property=tc.predict, main="Prediction of TC") | |
plot(somdata.xyf, type="changes") | |
# Basic plot: | |
par(mfrow=c(1,1)) | |
x <- seq(nrow(testing)) | |
plot(x, testing[, "MEAS_TC"], type="l", col="black", ylim=c(-2, 2)) | |
par(new=TRUE) | |
plot(x, tc.predict, type="l", col="red", ylim=c(-2, 2)) | |
# Plot of codebook vectors | |
par(mfrow=c(1, 2)) | |
plot(somdata.xyf, type="codes") | |
# Pre-scaled (original) meas_tc data: | |
meas.tc.testing <- somdata[-inTrain, "MEAS_TC"] | |
# Un-scale() the predicted tc: | |
descale <- attr(testing, 'scaled:scale')[["MEAS_TC"]] | |
decentre <- attr(testing, 'scaled:center')[["MEAS_TC"]] | |
tc.predict.descale <- sapply(tc.predict, function(x) x * descale + decentre) | |
# Basic plot: | |
par(mfrow=c(1, 1)) | |
plot(x, meas.tc.testing, type="l", col="black", ylim=c(0, 3)) | |
par(new=TRUE) | |
plot(x, tc.predict.descale, type="l", col="red", ylim=c(0, 3)) | |
# ===== # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment