x1=runif (500) - 0.5
x2=runif (500) - 0.5
y=1*(x1^2-x2^2 > 0)
plot(x1[y == 0], x2[y == 0], col = "purple", xlab = "X1", ylab = "X2", pch = "*")
points(x1[y == 1], x2[y == 1], col = "green", pch = 4)
log.fit = glm(y ~ x1 + x2, family = binomial)
summary(log.fit)
##
## Call:
## glm(formula = y ~ x1 + x2, family = binomial)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.239 -1.181 1.123 1.162 1.205
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.033447 0.089682 0.373 0.709
## x1 0.008086 0.306000 0.026 0.979
## x2 0.217831 0.310963 0.701 0.484
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 693.02 on 499 degrees of freedom
## Residual deviance: 692.53 on 497 degrees of freedom
## AIC: 698.53
##
## Number of Fisher Scoring iterations: 3
data = data.frame(x1 = x1, x2 = x2, y = as.factor(y))
lm.prob = predict(log.fit, data, type = "response")
lm.pred = ifelse(lm.prob > 0.50, 1, 0)
data.pos = data[lm.pred == 1, ]
data.neg = data[lm.pred == 0, ]
plot(data.pos$x1, data.pos$x2, col = "blue", xlab = "X1", ylab = "X2", pch = "+")
points(data.neg$x1, data.neg$x2, col = "red", pch = 4)
lm.fit = glm(y ~ poly(x1, 2) + poly(x2, 2) + I(x1 * x2), data = data, family = binomial)
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
lm.prob = predict(lm.fit, data, type = "response")
lm.pred = ifelse(lm.prob > 0.5, 1, 0)
data.pos = data[lm.pred == 1, ]
data.neg = data[lm.pred == 0, ]
plot(data.pos$x1, data.pos$x2, col = "blue", xlab = "X1", ylab = "X2", pch = "+")
points(data.neg$x1, data.neg$x2, col = "gray", pch = 4)
library(e1071)
svm.fit = svm(y ~ x1 + x2, data, kernel = "linear", cost = 0.01)
svm.pred2 = predict(svm.fit, data)
data.pos1 = data[svm.pred2 == 1, ]
data.neg1 = data[svm.pred2 == 0, ]
plot(data.pos1$x1, data.pos1$x2, col = "blue", xlab = "X1", ylab = "X2", pch = "+")
points(data.neg1$x1, data.neg1$x2, col = "gray", pch = 4)
svm.fit = svm(as.factor(y) ~ x1 + x2, data, gamma = 1)
svm.pred = predict(svm.fit, data)
data.pos = data[svm.pred == 1, ]
data.neg = data[svm.pred == 0, ]
plot(data.pos$x1, data.pos$x2, col = "blue", xlab = "X1", ylab = "X2", pch = "+")
points(data.neg$x1, data.neg$x2, col = "gray", pch = 4)
Support Vector machine and logistic regression are very efficient in predicting non-linear predictors with the gamma.