为什么我的逻辑回归得到 'Error in weights * y : non-numeric argument to binary operator'?

Why am I getting 'Error in weights * y : non-numeric argument to binary operator' in my logistic regression?

我愿意为我的数据集执行逻辑回归。我使用:

glm.fit=glm(direccion~Profit, data=datos, family=binomial)

    Minute  ecopet  TASA10  direccion   Minute  cl1     Day         Profit  
1   571     2160     5       1          571    51.85    2015-02-20  -0.03   
2   572     2160     5       1          572    51.92    2015-02-20   0.04   
3   573     2160     5       1          573    51.84    2015-02-20  -0.04   
4   574     2160     5       1          574    51.77    2015-02-20  -0.11   
5   575     2160     10      1          575    51.69    2015-02-20  -0.19   
6   576     2165     5       1          576    51.69    2015-02-20  -0.16   
7   577     2165    -5       0          577    51.64    2015-02-20  -0.28   
8   578     2165    -10      0          578    51.47    2015-02-20  -0.37   
9   579     2165    -10      0          579    51.41    2015-02-20  -0.36   
10  580     2170    -15      0          580    51.44    2015-02-20  -0.25   
11  581     2170    -30      0          581    51.48    2015-02-20  -0.21   
12  582     2160    -20      0          582    51.52    2015-02-20  -0.12   
13  583     2155    -5       0          583    51.56    2015-02-20   0.09   
14  584     2155    -5       0          584    51.51    2015-02-20   0.10   
15  585     2155    -5       0          585    51.44    2015-02-20   0.00   
16  586     2140     10      1          586    51.30    2015-02-20  -0.18   
17  587     2140     10      1          587    51.31    2015-02-20  -0.21   
18  588     2150     0       0          588    51.31    2015-02-20  -0.25

如您所见,变量'direccion'是一个二元变量,是我逻辑回归中的因变量。只要变量 'TASA10' 为正,它就为 1,否则为 0。问题是在我 运行 代码之后,我得到:

'Error in weights * y : non-numeric argument to binary operator'

你知道这是为什么吗?

谢谢!!

direccion 列似乎是字符列而不是数字列。可以通过运行str(datos)验证;你会看到类似

的内容
'data.frame':   18 obs. of  8 variables:
 $ Minute   : int  571 572 573 574 575 576 577 578 579 580 ...
 $ ecopet   : int  2160 2160 2160 2160 2160 2165 2165 2165 2165 2170 ...
 $ TASA10   : int  5 5 5 5 10 5 -5 -10 -10 -15 ...
 $ direccion: chr  "1" "1" "1" "1" ...
 $ Minute.1 : int  571 572 573 574 575 576 577 578 579 580 ...
 $ cl1      : num  51.9 51.9 51.8 51.8 51.7 ...
 $ Day      : Factor w/ 1 level "2015-02-20": 1 1 1 1 1 1 1 1 1 1 ...
 $ Profit   : num  -0.03 0.04 -0.04 -0.11 -0.19 -0.16 -0.28 -0.37 -0.36 -0.25 ...

特别注意 direccion 列的类型。这可以通过 运行

来解决
datos$direccion <- as.numeric(datos$direccion)

如果这是一个因素,那么您需要确保不会丢失编码,方法是使用

datos$direccion <- as.numeric(as.character(datos$direccion))

更好的方法是在您的管道中回顾生成此数据框的代码并将其修复为编码为数字而不是字符串。

glm()只接受numericfactor类型的变量,不知道如何处理character类型的变量。

您可以创建一个简单的因式分解函数,将所有字符 (chr) 列转换为因子,同时保留数字列:

factorize = function(column, df){
  #' Check if column is character and  turn to factor

  if (class(df[1,column]) == "character"){
    out = as.factor(df[,column])
  } else { # if it's numeric
    out = df[,column]
  }
  return(out)
}

store.colnames = colnames(data)
data  = lapply(store.colnames, function(column) factorize(column, data))
data = as.data.frame(data)
colnames(data) = store.colnames

代码可以更漂亮,但它会完成工作,我只是想说明这一点。

或者,您可以将单个列更改为因子类型:

datos$direccion = as.factor(datos$direccion)

希望对您有所帮助!