ERG Tutorial 3 Linear Model
A = lm( medv ~ lstat + I(lstat^2), date = Boston)#对lstat和lstat^2进行线性回归
A = lm( medv ~ poly(lstat, 5), date = Boston)#对lstat,lstat^2……lstat^3进行线性回归
自定义函数
AAA = function(x,y){
x = 123
y = 345
print(XXX)
}
#AAA是function的名字,调用的时候要用 AAA(),要加括号
Logistic Regression
glm和lm类似
glm.fits=glm(Direction~Lag1+Lag2+Lag3+Lag4+Lag5+Volume,data=Smarket,family=binomial)
predict函数中的type问题
glm.probs=predict(glm.fits,type="response")
#在做分类预测的时候,如果预测值为概率,则type = response 给出具体的预测概率,而 type = class按规定的阙值给出分类
mean(a==b)
>mean(glm.pred==Direction)
[1] 0.5216
> table(glm.pred,Direction)
Direction
glm.pred Down Up
Down 145 141
Up 457 507
#(145+507)/1250=0.5216 预测的精准性,1-0.5216=0.4784是error,预测误差
Linear Discriminant Analysis线性判别分析
lda
Quadratic Discriminant Analysis二次判别分析
qdarr