我想将level_ts和sales_ts系列的频率设置为12。我使用robjects.r('ts')作为ts,但是类型frequency = 12或frequency = 1没什么区别
我在R中运行,并且根据该python脚本,我发现它始终将其频率视为1
我可以在R中管理它,但不知道如何在python中做
我将数据强制转换为R向量以便在ts()内部使用,但它不起作用
import numpy as np
import pandas as pd
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
ts=robjects.r('ts')
forecast=importr('forecast')
from rpy2.robjects import pandas2ri
pandas2ri.activate()
# data_level and data_sales are lists with length 48
leveldata = pd.DataFrame(data = data_level, columns = ['VALUE'])
salesdata = pd.DataFrame(data = data_sales, columns = ['VALUE'])
# R vectors
level_vector = robjects.FloatVector(np.log(leveldata['VALUE']))
sales_vector = robjects.FloatVector(salesdata['VALUE'])
# I want to set the frequency=12 but I can't
level_ts = ts(level_vector, frequency=12)
sales_ts = ts(sales_vector, frequency=12)
month_index = len(level_ts) % 12
fcast_index = 12 - month_index
# multivariate, I want to use time series in fit_arimax line
fit_arimax = forecast.auto_arima(level_ts, d=1, xreg=sales_ts)
fcast_sales = np.array([40, 65, 90, 30, 30, 40, 50, 50, 70, 80, 90, 130, 78])
forecast_output = forecast.forecast(fit_arimax, xreg=fcast_sales, h=fcast_index)
vector = np.array(forecast_output.rx('mean'))
fcast_level = np.exp(vector)
# output
invdays = []
for i in range(fcast_index):
invdays.append((fcast_level[0][i] / fcast_sales[i + 1]) * 2)
print(fcast_level)
print(invdays)
我希望将时间序列频率设置为12,而不是1,data_level和data_sales是过去4年的数据,每条记录是一个月的数据