问题描述
我正在尝试将statsmodel线性回归函数与公式一起使用.我的样本数据来自Pandas数据框.我在公式中的列名有一个小问题.由于下游的过程,我在列名称中使用了连字符.例如:
I am trying to use the statsmodel linear regression functions with formulas. My sample data is coming from a Pandas data frame. I am having a slight problem with column names within the formula. Due to the downstream processes, I have hyphens within my column names. For example:
+------+-------+-------+
+ VOLT + B-NN + B-IDW +
+------+-------+-------+
现在,保留连字符的原因之一是因为它允许python拆分字符串以进行其他分析,因此我必须保留它.如您所见,当我想使用VOLT ~ B-NN
使用B-NN回归VOLT时,我遇到了一个问题,因为patsy公式找不到B.
Now, one of the reasons for keeping the hyphen as it allows python to split the string for other analysis, so I have to keep it. As you can see, when I want to regress VOLT with B-NN using VOLT ~ B-NN
, I encounter a problem as the patsy formula cannot find B.
有没有办法告诉Patsy B-NN 是变量名而不是B减去NN?
Is there a way to tell Patsy that B-NN is a variable name and not B minus NN?
谢谢.
BJR
推荐答案
patsy使用Q
引用名称,例如Q('B-IDW')
patsy uses Q
for quoting names, e.g. Q('B-IDW')
http://patsy.readthedocs.io/en/latest/builtins-reference.html#patsy.builtins.Q
my_fit_function("y ~ Q('weight.in.kg')", ...)
这篇关于当变量具有连字符时的Patsy公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!