R代码
决策树
library(tree)
tree.car <- tree(High ~ . - Sales, data = Carseats) #去除scales然后构造决策树
Logistic回归
require(MASS)
glm.fit <- glm(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume,
data = Smarket,
family = binomial)
# binomial表示要用Logitsti回归
LDA
require(MASS)
lda.fit <- lda(Direction ~ Lag1 + Lag2, data = Smarket_2001TO2004)
QDA
require(MASS)
qda.fit <- qda(Direction ~ Lag1 + Lag2, data = Smarket_2001TO2004)
SVM
library(e1071)
svmfit <- svm(y~., data=dat[train,],
kernel="linear",
cost=10,
scale=FALSE)
# kernel="linear"表明用线性核
# scale=FALSE说明不需要对每个特征进行均值为零标准差为1的缩放
# 参数cost指出违背边界的代价权重, 越大则边界较窄
核函数使用径向基函数, 径向基函数的公式为:\(exp\{−γ|u−v|2\}\).
svmfit <- svm(y~., data=dat[train,],
kernel="radial",
gamma=1,
cost=1)
核函数使用多项式核, 公式为: \((gamma∗u′∗v+coef0)^{degree}\)
svmfit <- svm(y~., data=dat[train,],
kernel="polynomial",
degree=3,
gamma=1,
coef0=1,
cost=1)