无法为以下逻辑回归
修复以下错误training=(IBM$Serial<625)
data=IBM[!training,]
dim(data)
stock.direction <- data$Direction
training_model=glm(stock.direction~data$lag2,data=data,family=binomial)
###Error### ---- Error in eval(family$initialize) : y values must be 0 <= y <= 1
我正在使用的数据中的几行
X Date Open High Low Close Adj.Close Volume Return lag1 lag2 lag3 Direction Serial
1 28-11-2012 190.979996 192.039993 189.270004 191.979996 165.107727 3603600 0.004010855 0.004010855 -0.001198021 -0.006354834 Up 1
2 29-11-2012 192.75 192.899994 190.199997 191.529999 164.720734 4077900 0.00114865 0.00114865 -0.004020279 -0.009502386 Up 2
3 30-11-2012 191.75 192 189.5 190.070007 163.465073 4936400 0.003630178 0.003630178 -0.001894039 -0.005576956 Up 3
4 03-12-2012 190.759995 191.300003 188.360001 189.479996 162.957703 3349600 0.001213907 0.001213907 -0.002480478 -0.001636046 Up 4
答案 0 :(得分:3)
要求y值介于0和1之间的原因是因为数据中的分类特征(如“方向”)属于“字符”类型。您需要将它们转换为使用as.factor(data$Direction)
键入'factor'。所以:glm(Direction ~ lag2, data=...)
不需要声明stock.direction。
您可以使用命令class(variable)
检查变量类,如果它们是字符,则可以转换为factor并在同一数据框中创建新列。它应该工作。
答案 1 :(得分:2)
我遇到了同样的错误“ eval(family $ initialize)中的错误:y值必须为0 <= y <= 1”,并通过在red.csv函数中添加“ stringsAsFactors = T”来解决。
之前:gene.train = read.csv(“ gene.train.csv”,标头= T)#错误
之后:gene.train = read.csv(“ gene.train.csv”,标头= T,stringsAsFactors = T)#没有错误。
答案 2 :(得分:0)
如果不了解数据,你应该这样做
library(dplyr)
df <- read.table(header = T, stringsAsFactors = F, text ="X Date Open High Low Close Adj.Close Volume Return lag1 lag2 lag3 Direction Serial
1 28-11-2012 190.979996 192.039993 189.270004 191.979996 165.107727 3603600 0.004010855 0.004010855 -0.001198021 -0.006354834 Up 1
2 29-11-2012 192.75 192.899994 190.199997 191.529999 164.720734 4077900 0.00114865 0.00114865 -0.004020279 -0.009502386 Up 2
3 30-11-2012 191.75 192 189.5 190.070007 163.465073 4936400 0.003630178 0.003630178 -0.001894039 -0.005576956 Up 3
4 03-12-2012 190.759995 191.300003 188.360001 189.479996 162.957703 3349600 0.001213907 0.001213907 -0.002480478 -0.001636046 Up 4
1 28-11-2012 190.979996 192.039993 189.270004 191.979996 165.107727 3603600 0.004010855 0.004010855 -0.001198021 -0.006354834 Up 1
2 29-11-2012 192.75 192.899994 190.199997 191.529999 164.720734 4077900 0.00114865 0.00114865 -0.004020279 -0.009502386 Down 2
3 30-11-2012 191.75 192 189.5 190.070007 163.465073 4936400 0.003630178 0.003630178 -0.001894039 -0.005576956 Up 3
4 03-12-2012 190.759995 191.300003 188.360001 189.479996 162.957703 3349600 0.001213907 0.001213907 -0.002480478 -0.001636046 Down 4
") %>%
mutate(bin = ifelse(Direction == "Up", 1, 0))
glm(bin ~ High, family = "binomial", data = df)