Py学习  »  Python

基于python-statsmodel的多元线性回归

Park Dongyeon • 4 年前 • 472 次点击  

在r中,可以执行如下的多元线性回归

temp = lm(log(volume_1[11:62])~log(price_1[11:62])+log(volume_1[10:61]))

在python中,可以使用 R风格的公式,所以我认为下面的代码应该也能工作,

import statsmodels.formula.api as smf
import pandas as pd
import numpy as np

rando = lambda x: np.random.randint(low=1, high=100, size=x)

df = pd.DataFrame(data={'volume_1': rando(62), 'price_1': rando(62)})

temp = smf.ols(formula='np.log(volume_1)[11:62] ~ np.log(price_1)[11:62] + np.log(volume_1)[10:61]', 
               data=df) 
# np.log(volume_1)[10:61] express the lagged volume

但我明白了

PatsyError: Number of rows mismatch between data argument and volume_1[11:62] (62 versus 51)
volume_1[11:62] ~ price_1[11:62] + volume_1[10:61]

我想不可能只回归列中的一部分行,因为data=df有62行,而其他变量有51行。

有没有什么方法可以像r一样方便地进行回归?

df type是pandas dataframe,列名是volume_1,price_1

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/41226
 
472 次点击  
文章 [ 1 ]  |  最新文章 4 年前
jtweeder
Reply   •   1 楼
jtweeder    5 年前

使用来自 github question 在patsy存储库中,这将是使lag列正常工作的方法。

import statsmodels.formula.api as smf
import pandas as pd
import numpy as np

rando = lambda x: np.random.randint(low=1, high=100, size=x)

df = pd.DataFrame(data={'volume_1': rando(62), 'price_1': rando(62)})

def lag(x, n):
    if n == 0:
        return x
    if isinstance(x,pd.Series):
        return x.shift(n)

    x = x.astype('float')
    x[n:] = x[0:-n]
    x[:n] = np.nan
    return x

temp = smf.ols(formula='np.log(volume_1) ~ np.log(price_1) + np.log(lag(volume_1,1))', 
               data=df[11:62])