File size: 3,264 Bytes
94bbd2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import streamlit as st
import plotly.express as px
import numpy as np
import plotly.graph_objects as go
from sklearn.metrics import r2_score
from collections import OrderedDict
import pickle
import json
import streamlit as st
import plotly.express as px
import numpy as np
import plotly.graph_objects as go
from sklearn.metrics import r2_score
import pickle
import json
import pandas as pd
import statsmodels.api as sm
from sklearn.metrics import mean_absolute_percentage_error
import sys
from utilities import (set_header, 
                       initialize_data,
                       load_local_css,
                       create_channel_summary,
                       create_contribution_pie,
                       create_contribuion_stacked_plot,
                       create_channel_spends_sales_plot,
                       format_numbers,
                       channel_name_formating,
                       load_authenticator)




def plot_actual_vs_predicted(date, y, predicted_values, model):
    fig = go.Figure()

    fig.add_trace(go.Scatter(x=date, y=y, mode='lines', name='Actual', line=dict(color='#6c757d')))
    fig.add_trace(go.Scatter(x=date, y=predicted_values, mode='lines', name='Predicted', line=dict(color='#FF3A3B')))
    
    # Calculate MAPE
    mape = mean_absolute_percentage_error(y, predicted_values)
    
    # Calculate AdjR2 # Assuming X is your feature matrix
    adjr2 = model.rsquared_adj

    # Create a table to display the metrics
    metrics_table = pd.DataFrame({
        'Metric': ['MAPE', 'R-squared', 'AdjR-squared'],
        'Value': [mape, model.rsquared, adjr2]
    })

    fig.update_layout(
        xaxis=dict(title='Date'),
        yaxis=dict(title='Value'),
        xaxis_tickangle=-30
    )
    #metrics_table.set_index(['Metric'],inplace=True)
    return metrics_table, fig 


X=pd.read_csv('actual_data.csv')
y=X['total_prospect_id']
date=X['date']
X=X.drop(['total_prospect_id','date','Unnamed: 0'],axis=1)

print(X.columns)
original_stdout = sys.stdout
sys.stdout = open('temp_stdout.txt', 'w')

# Perform linear regression
model = sm.OLS(y, X).fit()


sys.stdout.close()
sys.stdout = original_stdout

st.set_page_config(layout='wide')
load_local_css('styles.css')
set_header()
st.title('Analysis of Result')

st.write(model.summary(yname='Prospects'))

st.subheader('Actual vs Predicted Plot')
metrics_table,fig = plot_actual_vs_predicted(date, y, model.predict(X), model)

st.plotly_chart(fig,use_container_width=True)
#st.plotly_chart(fig)

# Display the metrics table

metrics_table=np.round(metrics_table,2)
metrics_table_html = metrics_table.to_html(index=False, escape=False)

# Display the metrics table in Streamlit as HTML
#st.subheader('Model Metrics')
#st.markdown(metrics_table_html, unsafe_allow_html=True)
# st.subheader('Model Metrics')
# st.table(metrics_table)

custom_css = """
<style>
table {
    width: 80%; /* Adjust the table width as needed */
    border-collapse: collapse;
}
th, td {
    padding: 8px;
    text-align: left;
    border-bottom: 1px solid #ddd;
}
</style>
"""

# Display the metrics table in Streamlit as HTML with custom CSS
st.subheader('Model Metrics')
st.markdown(custom_css, unsafe_allow_html=True)
st.markdown(metrics_table_html, unsafe_allow_html=True)