为什么在我的逻辑回归中,二元运算符的非数字参数出现权重错误? [英] Why am I getting 'Error in weights * y : non-numeric argument to binary operator' in my logistic regression?

查看:54
本文介绍了为什么在我的逻辑回归中,二元运算符的非数字参数出现权重错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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’是一个二元变量,是我的Logistic回归中的因变量。只要变量‘TASA10’为正,则为1,否则为0。问题是,在我运行代码之后,我得到:

‘权重错误*y:二元运算符的非数字参数’

您知道为什么吗?

谢谢!!

推荐答案

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))

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

这篇关于为什么在我的逻辑回归中,二元运算符的非数字参数出现权重错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆