Skip to content

Instantly share code, notes, and snippets.

@marcovivero
Created March 24, 2016 22:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcovivero/1e952c2542c2fa18c6ed to your computer and use it in GitHub Desktop.
Save marcovivero/1e952c2542c2fa18c6ed to your computer and use it in GitHub Desktop.
### Regression Example
f = function(x) {
0.1 * cos(x)^2 + 0.25 * sin(x)^(3)
}
regressionData = data.frame(rnorm(5000), sd = 5)
regressionData[,2] = f(regressionData[,1]) + rnorm(5000, sd = 0.1)
names(regressionData) = c("x", "y")
plot(x = regressionData$x, y = regressionData$y, pch = 19, cex = 0.25)
curve(f, n = 10000, ylim = c(-5, 5), col = "red", add = T, lwd = 2.25)
linearModel = lm(y ~ x, data = regressionData)
splineModel = smooth.spline(x = regressionData$x, y = regressionData$y)
points(x = regressionData$x, y = linearModel$fitted.values, col = "blue", pch = 19, cex = 0.15)
points(x = splineModel$x, y = splineModel$y, col = "green", pch = 19, cex = 0.15)
### Classification Example
zeros = data.frame(
rnorm(650, mean = 1.5, sd = 2),
rnorm(650, mean = 1, sd = 3),
rep(0, 650))
names(zeros) = c("x1", "x2", "y")
ones = data.frame(
rnorm(350, mean = -2.5, sd = 4),
rnorm(350, mean = -5, sd = 2),
rep(1, 350))
names(ones) = c("x1", "x2", "y")
classificationData = rbind(zeros, ones)[sample(1:1000, size = 1000),]
plot(x = classificationData$x1, y = classificationData$x2, cex = 0)
points(
x = classificationData[which(classificationData$y == 1),]$x1,
y = classificationData[which(classificationData$y == 1),]$x2,
col = "green", pch = 19, cex = 0.75)
points(
x = classificationData[which(classificationData$y == 0),]$x1,
y = classificationData[which(classificationData$y == 0),]$x2,
col = "red", pch = 19, cex = 0.75)
library(MASS)
ldaModel = lda(y ~ x1 + x2, data = classificationData)
meanZero = ldaModel$means[1,]
meanOne = ldaModel$means[2,]
invCov = ginv(cov(as.matrix(classificationData[,-3])))
a0 = log(ldaModel$prior[1] / ldaModel$prior[2]) -
1/2 * ((meanZero + meanOne) %*% invCov %*% (meanZero - meanOne))
aS = invCov %*% (meanZero - meanOne)
g = Vectorize(function(x) {
(-aS[1]/aS[2])*x - a0 / aS[2]
})
curve(g, col = "blue", lwd = 1.2, add = T)
# Inference example.
zeros = classificationData[which(classificationData$y == 0),]
mean(zeros$x1)
mean(zeros$x2)
cov(zeros[, -3])
### Clustering example.
plot(x= classificationData$x1, y = classificationData$x2, cex = 0.5, pch = 19)
clusters = kmeans(x = classificationData[, -3], centers = 2)$cluster
predZeros = which(clusters == 1)
predOnes = which(clusters == 2)
points(
x = classificationData[predZeros,]$x1,
y = classificationData[predZeros,]$x2,
col = "red", pch = 19, cex = 0.25)
points(
x = classificationData[predOnes,]$x1,
y = classificationData[predOnes,]$x2,
col = "green", pch = 19, cex = 0.25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment