0
$\begingroup$

I have weekly time series data for the last 2 years with variables "week", "marketing_spend", "web_traffic", and "revenue" Now I need to forecast "potential_web_traiffc" & "potential_revenue" for the next 12 weeks unfortunately, I don't know the "marketing_spend" for next 12 weeks. I am asked to forecast potential costs and based on it forecast other outputs.

I am a little confused, I am planning to run a univariate model to get the "marketing_spend" for the next 12 weeks and then run a linear regression to get "potential_web_traiffc" and then run another linear regression to get "potential_revenue"

I know it doesn't sound right but can someone guide me on what could be the right approach? This is out dummy data

import pandas as pd
import numpy as np

# Create a date range from 2021-01-07 to 2022-12-29 (Thursdays only)
dates = pd.date_range(start='2021-01-07', end='2022-12-29', freq='W-THU')

# Create a DataFrame with random data
data = pd.DataFrame({
    'date': dates,
    'cost': np.random.uniform(low=100000, high=1500000, size=len(dates)).astype(int),
})
# Calculate the revenue column based on the cost and traffic columns
data['traffic'] = (data['cost']  * 2.5).round().astype(int)
data['revenue'] = (data['cost']  * 2).round().astype(int)
# Create new columns for week, month, and year
data['week'] = data['date'].dt.isocalendar().week
data['month'] = data['date'].dt.month
data['year'] = data['date'].dt.year

df = data
df

The model I am using to forecast cost is as follows

import statsmodels.api as sm

# Convert date column to datetime format and set as index
df['date'] = pd.to_datetime(df['date'])
df.set_index('date', inplace=True)

# Create a new data frame with only the 'cost' column
cost_df = df[['cost']]

# Split data into train and test sets
train_data = cost_df.iloc[:-12]
test_data = cost_df.iloc[-12:]

# Convert index to numerical values
train_data['numeric_index'] = range(len(train_data.index))

# Build and fit the model using OLS regression
model = sm.OLS(train_data['cost'], sm.add_constant(train_data['numeric_index'])).fit()

# Create a new dataframe for the next 12 weeks
next_weeks = pd.DataFrame(pd.date_range(start=df.index[-1]+pd.Timedelta(days=1), periods=12, freq='W-THU'), columns=['date'])
next_weeks['week'] = next_weeks['date'].dt.isocalendar().week
next_weeks['month'] = next_weeks['date'].dt.month
next_weeks['year'] = next_weeks['date'].dt.year

# Convert index to numerical values
next_weeks['numeric_index'] = range(len(train_data.index), len(train_data.index) + len(next_weeks))

# Use the model to make predictions for the next 12 weeks
next_weeks['cost'] = model.predict(sm.add_constant(next_weeks['numeric_index']))

# Print the data frame with the forecasted cost for the next 12 weeks
print(next_weeks)

now I am using the next_weeks dataframe as test set and forecasting traffic and revenue based on it

from sklearn.multioutput import MultiOutputRegressor
from sklearn.ensemble import RandomForestRegressor

# Split the data into training and testing sets
X_train = df[['week', 'month', 'cost']]
y_train = df[['traffic', 'revenue']]
X_test = next_weeks[['week', 'month', 'cost']]

# Train a random forest regressor as a multioutput regressor
regr = MultiOutputRegressor(RandomForestRegressor(random_state=42))
regr.fit(X_train, y_train)

# Use the model to make predictions for the next 12 weeks
y_pred = regr.predict(X_test)

# Add the predicted traffic and revenue columns to the next_weeks dataframe
next_weeks[['traffic', 'revenue']] = np.round(y_pred).astype(int)

print(next_weeks)

can someone please help me find a better solution or guide me towards the correct approach to such a problem? Thank you in advance

$\endgroup$

1 Answer 1

0
$\begingroup$

Your approach is perfectly fine! But instead of estimating the marketing expenditure univariately, you could try to build another regression model to estimate it using other parameters as well.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.