今天在使用R语言对Weekly进行交叉验证时,发生如下报错:
R
错误于eval(family$initialize): y值必需满足0 <= y <= 1
错误代码为:
R
Weekly<-read.csv("Weekly.csv")
set.seed(1)
attach(Weekly)
glm.fit1 = glm(Direction~Lag1+Lag2, data=Weekly, family=binomial)
summary(glm.fit1)
现在让我们来解决这个问题。
首先打开数据:
R
fix(Weekly)
我们发现Weekly数据集中的变量Direction为分类变量,为此需要对他进行0-1变换。
Direction 0-1编码,0表示Down,1表示Up
修改一:
Direction.1<-as.factor(Direction)
Weekly<-data.frame(Weekly,Direction.1)
Direction<-Direction.1
R
Weekly<-resd.csv('Weekly.csv')
attach(Weekly)
Direction.1<-as.factor(Direction)
Weekly<-data.frame(Weekly,Direction.1)
Direction<-Direction.1
fix(Weekly)
glm.fit<-glm(Direction~Lag1+Lag2+Lag3+Lag4+Lag5+colume,data=Weekly,family=binomial)
summary(glm.fit)
上面修改方式过于繁琐,为此下面对修改结果进行简化:
修改二:
Weekly$Direction=as.factor(Direction)
R
Weekly<-read.csv("Weekly.csv")
set.seed(1)
attach(Weekly)
Weekly$Direction=as.factor(Direction)
summary(Weekly)
R
glm.fit1 = glm(Direction~Lag1+Lag2, data=Weekly, family=binomial)
summary(glm.fit1)
R
Call:
glm(formula = Direction ~ Lag1 + Lag2, family = binomial, data = Weekly)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.22122 0.06147 3.599 0.000319 ***
Lag1 -0.03872 0.02622 -1.477 0.139672
Lag2 0.06025 0.02655 2.270 0.023232 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 1496.2 on 1088 degrees of freedom
Residual deviance: 1488.2 on 1086 degrees of freedom
AIC: 1494.2
Number of Fisher Scoring iterations: 4
代码成功运行,问题解决。