Mominali23/relations
0
1import streamlit as st2import pandas as pd3import numpy as np4import statsmodels.api as sm5from sklearn.linear_model import LinearRegression6 7# App title8st.title("Data Relationship Estimator")9 10# Upload file11uploaded_file = st.file_uploader("Upload your Excel file", type=["xlsx", "xls"])12 13if uploaded_file:14 try:15 # Read the uploaded Excel file16 data = pd.read_excel(uploaded_file)17 st.success("File uploaded successfully!")18 19 # Display data preview20 st.write("### Data Preview")21 st.write(data.head())22 23 # Allow the user to select variables24 st.write("### Select Variables for Analysis")25 numerical_cols = data.select_dtypes(include=["number"]).columns.tolist()26 if len(numerical_cols) < 2:27 st.error("Not enough numerical data for analysis. Please upload valid data.")28 else:29 x_cols = st.multiselect("Select Independent Variables (X)", numerical_cols)30 y_col = st.selectbox("Select Dependent Variable (Y)", numerical_cols)31 32 if x_cols and y_col:33 # Subset data34 X = data[x_cols]35 y = data[y_col]36 37 # Fit a regression model38 st.write("### Regression Model")39 X_with_const = sm.add_constant(X) # Add intercept term40 model = sm.OLS(y, X_with_const).fit()41 st.write(model.summary())42 43 # Display the equation44 coeffs = model.params45 equation = f"{y_col} = "46 for i, col in enumerate(['Intercept'] + x_cols):47 term = f"{coeffs[i]:.3f}"48 if i > 0:49 term += f" * {col}"50 equation += term51 if i < len(coeffs) - 1:52 equation += " + "53 54 st.write("### Mathematical Relationship")55 st.write(f"**{equation}**")56 57 # Explanation58 st.write("### Explanation")59 st.write("""60 - **Intercept**: The baseline value of the dependent variable (Y) when all independent variables (X) are zero.61 - **Coefficients**: These constants quantify the relationship between each independent variable and the dependent variable.62 - **R-squared**: A measure of how well the model explains the variance in the dependent variable.63 - **P-values**: Indicate whether the relationships between variables are statistically significant.64 """)65 else:66 st.error("Please select both independent and dependent variables.")67 68 except Exception as e:69 st.error(f"An error occurred: {e}")70else:71 st.info("Please upload an Excel file to begin.")72 