pvaluedotone commited on
Commit
ca6c024
·
verified ·
1 Parent(s): 2a33e43

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import statsmodels.formula.api as smf
4
+ from linearmodels.iv import IV2SLS
5
+ import warnings
6
+
7
+ warnings.simplefilter(action='ignore', category=FutureWarning)
8
+
9
+ def process_file(file):
10
+ global df
11
+ df = pd.read_csv(file.name)
12
+ return df.columns.tolist()
13
+
14
+ def run_2sls(dependent_var, endogenous_vars, instruments, exogenous_vars):
15
+ if not all([dependent_var, endogenous_vars, instruments]):
16
+ return "Error: Please select all required variables."
17
+
18
+ endogenous_vars = endogenous_vars.split(",")
19
+ instruments = instruments.split(",")
20
+ exogenous_vars = exogenous_vars.split(",") if exogenous_vars else []
21
+
22
+ if len(instruments) < len(endogenous_vars):
23
+ return "Error: The number of instruments must be at least equal to the number of endogenous variables."
24
+
25
+ try:
26
+ df_selected = df[[dependent_var] + endogenous_vars + instruments + exogenous_vars].dropna()
27
+
28
+ # First stage
29
+ predicted_vars = []
30
+ for var in endogenous_vars:
31
+ first_stage_formula = f'{var} ~ ' + ' + '.join(instruments + exogenous_vars)
32
+ first_stage = smf.ols(first_stage_formula, data=df_selected).fit()
33
+ df_selected[f'{var}_hat'] = first_stage.fittedvalues
34
+ predicted_vars.append(f'{var}_hat')
35
+
36
+ # Second stage
37
+ second_stage_formula = f'{dependent_var} ~ ' + ' + '.join(predicted_vars + exogenous_vars)
38
+ second_stage = smf.ols(second_stage_formula, data=df_selected).fit()
39
+
40
+ return second_stage.summary().as_text()
41
+
42
+ except Exception as e:
43
+ return f"Error: {str(e)}"
44
+
45
+ with gr.Blocks() as app:
46
+ gr.Markdown("## Two-Stage Least Squares Regression (2SLS)")
47
+
48
+ file_input = gr.File(label="Upload CSV File")
49
+ column_output = gr.Label(label="Available Columns")
50
+ file_input.change(process_file, inputs=file_input, outputs=column_output)
51
+
52
+ dependent_var = gr.Dropdown(label="Dependent Variable")
53
+ endogenous_vars = gr.Textbox(label="Endogenous Variables (comma-separated)")
54
+ instruments = gr.Textbox(label="Instruments (comma-separated)")
55
+ exogenous_vars = gr.Textbox(label="Exogenous Variables (comma-separated, optional)")
56
+
57
+ run_button = gr.Button("Run 2SLS Regression")
58
+ output = gr.Textbox(label="Regression Output", lines=20)
59
+
60
+ run_button.click(run_2sls, inputs=[dependent_var, endogenous_vars, instruments, exogenous_vars], outputs=output)
61
+
62
+ app.launch()