如何为时间序列数据创建线性回归模型?

时间:2019-10-29 07:58:14

标签: python pandas time-series linear-regression

如何为时间序列数据创建线性回归模型?

我删除了日期时间,并按照常规回归方法进行处理,但显示出r平方为-7。我有13H1到17H2的数据。

df:

UID BaselineHalf Metric_Type Segment rateadj_amount_usd CPI_Inflation Exports固定_投资GDP GDP 100130_Print HW 2013-12-31 Print HW CANADA_PRINT_NAMED 2212.060000 3.036892 5.99463 -1.890996 3.885646 2.970826 3.762586 4.716683 -3.32253 -2.444949 10.148924 5.35529 7.001484 2.402204

    df1 = df[df['UID']== '100130_Print HW']
    x = df1[['CPI_Inflation', 'Exports', 'Fixed_Invstment', 'GDP', 
    'Govt_Growth',
   'Imports', 'Industrial_Production', 'Merchandise_Exports',
   'Merchandise_Imports', 'Nominal_Retail_Sales', 'Private_Consumption',
   'Real_Retail_Sales', 'WPI_Inflation']]
    y = df1['rateadj_amount_usd']
    X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, 
    random_state=101)
    lm = LinearRegression()
    lm.fit(X_train,y_train)
    predictions = lm.predict(X_test)
    from sklearn.metrics import r2_score
    coefficient_of_determination = r2_score(y_test,predictions)

1 个答案:

答案 0 :(得分:1)

我看到了您的方法中的一个普遍问题:您尝试回归时间序列,但删除了时间数据,并从数据中提取了一个随机样本(使用train_test_split())。但是,数据点是随机相关的。当然,给定年份的数据在很大程度上取决于前一年。但是,您做模型的方式不能使用此信息。

因此,从R平方可以看出,您的模型效果非常差。使用时间序列数据进行尝试。