I want to plot my Statsmodels mixed-model result with X against Y, and would like some verification that I've understood it correctly. I am mostly uncertain how I should treat the fixed parameters (other_fixed_effects
in the code segment), and if they should be included.
Fixed parameters:
Intercept 330.777402
Score 39.101520
Group Var 8.264719
Group x Score Cov 0.248543
Score Var 0.007475
dtype: float64
My code:
# Load data from fitted model
parameters = final_volume_fit.params
r_effects = final_volume_fit.random_effects
# Fig size
fig = plt.figure(figsize = (16, 9))
# Fixed effects
fixed_intercept = parameters[0]
fixed_score = parameters[1]
fixed_grp_var = parameters[2]
fixed_grpxscore_var = parameters[3]
fixed_score_var = parameters[4]
# Color counter
ctr = 0
# Group represent the different stocks, j=(AAPL,...,VIAC)
for group in r_effects:
# Random effects, intercept and slope
group_intercept = r_effects[group][0]
group_slope = r_effects[group][1]
# Random and fixed effects added togheter
# (Fixed intercept + Intercept(j))
y_group_intercept = fixed_intercept+group_intercept
# (Fixed slope + slope(j))
y_group_slope = fixed_score+group_slope
# Observed traded volume for the timeperiod
z = corrected_data_transformed.loc[corrected_data_transformed['Stock'] == group]
stock_dates = list(z['Date'])
stock_volume = list(z['Transformed_Volume'])
stock_line = []
# Stock dates represents the timeperiod, i=(1,...,15), that observations was carried out during.
for i in stock_dates:
# Data point for regressionline
data_point = y_group_intercept + y_group_slope*i
#Should these fixed effects (other_fixed_effects) also be added to the estimation of data_point?
other_fixed_effects = fixed_grp_var*i + fixed_grpxscore_var*i + fixed_score_var*i
observation_ij = data_point + other_fixed_effects
# Add estimation to regressionline
stock_line.append(observation_ij)
# Seaborn plot ax
ax = sns.lineplot(x = stock_line, y = stock_volume ,
color = colors[ctr], label = group)
# Increase color counter
ctr += 1
# Plot settings
ax.set(xlabel='Regressionline', ylabel='Volume')
ax.legend(bbox_to_anchor=(0, 1), loc=2, borderaxespad=0.1)
ax.figsize = (16, 9)
ax.set_title('Regression-line for each observation X against registered volume during timeperiod Y', weight='bold').set_fontsize('16')
picture = plt.savefig('yijplot2.png', bbox_inches='tight')
plt.show()