統計モデルを使用して非線形回帰モデルを計算しようとしています。特に、patsy 構文の学習に問題があります。
patsy 構文を使用して非線形モデルを定式化する方法のチュートリアルまたは例はありますか?
特に、この例 ( http://statsmodels.sourceforge.net/devel/examples/generated/example_ols.html )の非線形モデルは、patsy を使用してどのように指定されますか?
事前にどうもありがとうございました
アンディ
統計モデルを使用して非線形回帰モデルを計算しようとしています。特に、patsy 構文の学習に問題があります。
patsy 構文を使用して非線形モデルを定式化する方法のチュートリアルまたは例はありますか?
特に、この例 ( http://statsmodels.sourceforge.net/devel/examples/generated/example_ols.html )の非線形モデルは、patsy を使用してどのように指定されますか?
事前にどうもありがとうございました
アンディ
Patsy isn't really useful for fitting general non-linear models, but the models on the page you link to are a special sort of non-linear model -- they're using a linear model fitting method (OLS), and applying it to non-linear transformations of the basic variables. A standard and very useful trick is to combine multiple non-linear transformations of the same variable in order to effectively fit more general curves. For this, patsy is very useful.
What you really want to know is how to express variable transformations in patsy. This is pretty easy. The way patsy works, given a formula string like "x1 + x2:x3"
, it scans through and interprets the special patsy operators like +
and :
, and then the stuff that's left over (x1
, x2
, x3
) are interpreted as arbitrary python code. So you can just as well write "np.sin(x1) + np.log(x2):x3"
or whatever.
The only thing to watch out for is that if you want to write a transformation that uses python operators that clash with patsy operators. Like, if you want to use +
or **
in your transformation, then you have to be careful to make sure that patsy doesn't interpret those itself, and leaves them to python. The trick here is that patsy will ignore any operators that appear inside a function call (or other complex python expression that patsy doesn't understand, but mostly this is function calls). So if you write "x1 + np.log(x2 + x3)"
, then patsy will treat this as two predictors, x1
and np.log(x2 + x3)
-- you can see it interpreted the first +
, but it left the second one alone for python to interpret.
But what if you wanted to, say, add two variables together and use them as a predictor, without taking the log? Well, from what we know already, we can come up with a simple hack: we could define a function that just returns its input (the identity function), and call it, like: "x1 + I(x2 + x3)"
. Now the function call to I(...)
will prevent patsy from seeing the second +
, but when we actually evaluate the term I(x2 + x3)
will be the same as x2
plus x3
.
And helpfully, patsy automatically provides a function called I()
that works like this, which is always available to use.
Now you know everything you need to know to reproduce the examples on that page. For the first one, the formula is "x + I(x**2)"
. For the second, the formula is "x + np.sin(x) + I((x - 5)**2)"
.
And for the last example, it's easiest to just use patsy's built-in categorical coding support: "x + C(groups)"
. (Here C
is another special built-in function that lets us adjust how categorical data is coded. Here we're just using it to tell patsy that even though groups
looks like a numerical vector -- its values are 0, 1, 2 -- in fact we should treat it as being categorical, with each value representing a different group. Then patsy applies its default categorical coding)