-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference_functions.py
391 lines (332 loc) · 16.4 KB
/
inference_functions.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import scipy.io as sio
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import pearsonr
import statsmodels.api as sm
#import plotly.graph_objects as go
#from IPython.lib.display import YouTubeVideo
#import ipywidgets as widgets
#from IPython.display import display, clear_output, Javascript, Code
def load_data():
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/Mark-Kramer/METER-Units/master/swim_lesson_data.csv")
swim_lessons = np.array(df.iloc[0:N-1,0])
drownings = np.array(df.iloc[0:N-1,1])
xy = np.array(df.iloc[0:N-1,2])
return swim_lessons,drownings,xy
def load_more_data():
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/Mark-Kramer/METER-Units/master/swim_lesson_data.csv")
swim_lessons = np.array(df.iloc[0:N-1,0])
drownings = np.array(df.iloc[0:N-1,1])
xy = np.array(df.iloc[0:N-1,2])
distance_from_ocean = np.array(df.iloc[0:N-1,3])
return swim_lessons,drownings,xy
def load_code():
import requests
url = "https://raw.githubusercontent.com/Mark-Kramer/METER-Units/main/sample_size_functions.py"
response = requests.get(url)
response.status_code
code = response.text
exec(code)
def load_data(using_colab=0):
# Load the data.
if using_colab:
data = sio.loadmat('/content/METER-Units/swim_lesson_data.mat')
else:
data = sio.loadmat('swim_lesson_data.mat') # Load the data default
swim_lessons = data['swim_lessons'] # ... and define the variables.
drownings = data['drownings']
xy = data['xy']
distance_from_ocean = data['distance_from_ocean']
return swim_lessons, drownings, xy
def load_more_data(using_colab=0):
# Load the data.
if using_colab:
data = sio.loadmat('/content/METER-Units/swim_lesson_data.mat')
else:
data = sio.loadmat('swim_lesson_data.mat') # Load the data default
distance_from_ocean = data['distance_from_ocean']
return distance_from_ocean
def plot_spatial_coordinates(xy, colors):
#import plotly.graph_objects as go
# Example x-y coordinates
x_coordinates = xy[:,1]
y_coordinates = xy[:,0]
# Create a scattermapbox trace
trace = go.Scattermapbox(
lat=y_coordinates,
lon=x_coordinates,
mode='markers',
marker=dict(
size=10,
color=colors, #residuals.to_numpy(),
colorscale='RdYlBu', # Choose a colorscale (Red-Blue in this case)
cmin=-0.25, #min(residuals.to_numpy()),
cmax= 0.25, #max(residuals.to_numpy()),
colorbar=dict(title='Variable'),
opacity=0.6
),
)
# Define the layout for the map
layout = go.Layout(
mapbox=dict(
center=dict(lat=sum(y_coordinates)/len(y_coordinates), lon=sum(x_coordinates)/len(x_coordinates)),
zoom=9,
style='open-street-map' # You can change the map style
),
title='X-Y Coordinates on Map'
)
# Create the figure
fig = go.Figure(data=[trace], layout=layout)
fig.update_layout(width=800, height=600)
# Show the plot
fig.show();
def create_dropdown_estimate_regression(swim_lessons, drownings):
# Function to handle dropdown value change
def on_dropdown_change(change):
code = """
from statsmodels.formula.api import ols # import the required module
dat = {"x": swim_lessons, "y": drownings} # define the predictor "x" and outcome "y"
regression_results = ols("y ~ 1 + x", data=dat).fit() # fit the model.
print('Slope estimate =',round(regression_results.params[1],3)) # Print the slope
print('p-value =',round(regression_results.pvalues[1],3)) # ... and the p-value.
"""
with output:
clear_output()
if change['new'] == 'I want to write all the code myself.':
print("\nGood for you! Here are some suggestions:\n")
print("1. Consider using `ols` in `statsmodels`.")
print("2. The outcome variable is `drownings`.")
print("3. The predictor variable is `swim_lessons`.\n")
print("If you get stuck, select the `Show me the code and the results.` option\n")
elif change['new'] == 'Show me the code and I will run it myself.':
print("\n Here's the code:\n")
display(Code(data=code, language='python'))
elif change['new'] == 'Show me the code and the results.':
print("\n Here's the code:\n")
display(Code(data=code, language='python'))
print("\n And here are the results:\n")
from statsmodels.formula.api import ols
dat = {"x": swim_lessons, "y": drownings}
regression_results = ols("y ~ 1 + x", data=dat).fit()
print('Slope estimate =',round(regression_results.params[1],5))
print('p-value =',round(regression_results.pvalues[1],10))
elif change['new'] == 'Just show me the results!':
print("\n Running code ... here are the results:\n")
from statsmodels.formula.api import ols
dat = {"x": swim_lessons, "y": drownings}
regression_results = ols("y ~ 1 + x", data=dat).fit()
print('Slope estimate =',round(regression_results.params[1],5))
print('p-value =',round(regression_results.pvalues[1],10))
# Create a dropdown widget
dropdown = widgets.Dropdown(
options=['I want to write all the code myself.', 'Show me the code and I will run it myself.', 'Show me the code and the results.', 'Just show me the results!'],
value=None,
description='Options:',
disabled=False,
)
# Output widget to display text
output = widgets.Output()
# Register the event handler
dropdown.observe(on_dropdown_change, names='value')
# Display the dropdown widget and output widget
display(dropdown)
display(output)
def create_dropdown_plot_regression(swim_lessons, drownings):
# Function to handle dropdown value change
def on_dropdown_change(change):
code = """
# Estimate the regression model.
from statsmodels.formula.api import ols # import the required module
dat = {"x": swim_lessons, "y": drownings} # define the predictor "x" and outcome "y"
regression_results = ols("y ~ 1 + x", data=dat).fit() # fit the model.
# Get model prediction.
pred = regression_results.get_prediction().summary_frame()
mn = pred['mean']
ci_low = pred['mean_ci_lower']
ci_upp = pred['mean_ci_upper']
# And plot it.
indices_sorted = np.argsort(swim_lessons,0)
plt.figure(figsize=(12, 8))
plt.scatter(swim_lessons,drownings)
plt.plot(swim_lessons[indices_sorted[:,0]],mn[indices_sorted[:,0]], 'r')
plt.plot(swim_lessons[indices_sorted[:,0]],ci_low[indices_sorted[:,0]], ':r')
plt.plot(swim_lessons[indices_sorted[:,0]],ci_upp[indices_sorted[:,0]], ':r')
plt.xlabel('Swim lessons')
plt.ylabel('Drownings');
"""
with output:
clear_output()
if change['new'] == 'I want to write all the code myself.':
print("\nGood for you! Here are some suggestions:\n")
print("1. Use the results of your regression estimate.")
print("2. Plot the observed data, `swim_lessons` versus `drownings`")
print("3. Plot your regression results (and 95% confidence intervals) on top of these data.\n")
print("If you get stuck, select the `Show me the code and the results.` option\n")
elif change['new'] == 'Show me the code and I will run it myself.':
print("\n Here's the code:\n")
display(Code(data=code, language='python'))
elif change['new'] == 'Show me the code and the results.':
print("\n Here's the code:\n")
display(Code(data=code, language='python'))
print("\n And here are the results:\n")
plot_regression_results_2d(swim_lessons, drownings)
elif change['new'] == 'Just show me the results!':
print("\n Running code ... here are the results:\n")
plot_regression_results_2d(swim_lessons, drownings)
# Create a dropdown widget
dropdown = widgets.Dropdown(
options=['I want to write all the code myself.', 'Show me the code and I will run it myself.', 'Show me the code and the results.', 'Just show me the results!'],
value=None,
description='Options:',
disabled=False,
)
# Output widget to display text
output = widgets.Output()
# Register the event handler
dropdown.observe(on_dropdown_change, names='value')
# Display the dropdown widget and output widget
display(dropdown)
display(output)
def create_dropdown_estimate_regression_3(swim_lessons, drownings, distance_from_ocean):
# Function to handle dropdown value change
def on_dropdown_change(change):
code = """
from statsmodels.formula.api import ols # import the required module
dat = {"w": distance_from_ocean, "x": swim_lessons, "y": drownings}
regression_results_2_predictor = ols("y ~1 + w + x", data=dat).fit()
print('Distance from ocean')
print('Slope estimate =',round(regression_results_2_predictor.params[1],4))
print('p-value =',round(regression_results_2_predictor.pvalues[1],12))
print('Number of swim lessons')
print('Slope estimate =',round(regression_results_2_predictor.params[2],5))
print('p-value =',round(regression_results_2_predictor.pvalues[2],3))
"""
with output:
clear_output()
if change['new'] == 'I want to write all the code myself.':
print("\nGood for you! Here are some suggestions:\n")
print("1. Consider using `ols` in `statsmodels`.")
print("2. The outcome variable is `drownings`.")
print("3. The predictor variables are `swim_lessons` and `distance_to_ocean`.\n")
print("If you get stuck, select the `Show me the code and the results.` option\n")
elif change['new'] == 'Show me the code and I will run it myself.':
print("\n Here's the code:\n")
display(Code(data=code, language='python'))
elif change['new'] == 'Show me the code and the results.':
print("\n Here's the code:\n")
display(Code(data=code, language='python'))
print("\n And here are the results:\n")
from statsmodels.formula.api import ols # import the required module
dat = {"w": distance_from_ocean, "x": swim_lessons, "y": drownings}
regression_results_2_predictor = ols("y ~1 + w + x", data=dat).fit()
print('Distance from ocean')
print('Slope estimate =',round(regression_results_2_predictor.params[1],4))
print('p-value =',round(regression_results_2_predictor.pvalues[1],12))
print('\nNumber of swim lessons')
print('Slope estimate =',round(regression_results_2_predictor.params[2],5))
print('p-value =',round(regression_results_2_predictor.pvalues[2],3))
elif change['new'] == 'Just show me the results!':
print("\n Running code ... here are the results:\n")
from statsmodels.formula.api import ols # import the required module
dat = {"w": distance_from_ocean, "x": swim_lessons, "y": drownings}
regression_results_2_predictor = ols("y ~1 + w + x", data=dat).fit()
print('Distance from ocean')
print('Slope estimate =',round(regression_results_2_predictor.params[1],4))
print('p-value =',round(regression_results_2_predictor.pvalues[1],12))
print('\nNumber of swim lessons')
print('Slope estimate =',round(regression_results_2_predictor.params[2],5))
print('p-value =',round(regression_results_2_predictor.pvalues[2],3))
# Create a dropdown widget
dropdown = widgets.Dropdown(
options=['I want to write all the code myself.', 'Show me the code and I will run it myself.', 'Show me the code and the results.', 'Just show me the results!'],
value=None,
description='Options:',
disabled=False,
)
# Output widget to display text
output = widgets.Output()
# Register the event handler
dropdown.observe(on_dropdown_change, names='value')
# Display the dropdown widget and output widget
display(dropdown)
display(output)
def plot_regression_results_2d(swim_lessons, drownings):
from statsmodels.formula.api import ols # import the required module
dat = {"x": swim_lessons, "y": drownings} # define the predictor "x" and outcome "y"
regression_results = ols("y ~ 1 + x", data=dat).fit() # fit the model.
# Get model prediction.
pred = regression_results.get_prediction().summary_frame()
mn = pred['mean']
ci_low = pred['mean_ci_lower']
ci_upp = pred['mean_ci_upper']
# And plot it.
indices_sorted = np.argsort(swim_lessons,0)
plt.figure(figsize=(12, 8))
plt.scatter(swim_lessons,drownings)
plt.plot(swim_lessons[indices_sorted[:,0]],mn[indices_sorted[:,0]], 'r')
plt.plot(swim_lessons[indices_sorted[:,0]],ci_low[indices_sorted[:,0]], ':r')
plt.plot(swim_lessons[indices_sorted[:,0]],ci_upp[indices_sorted[:,0]], ':r')
plt.xlabel('Swim lessons')
plt.ylabel('Drownings')
plt.show()
def plot_regression_results_3d(swim_lessons, drownings, distance_from_ocean):
# Create a meshgrid for 3D plotting
x1 = np.transpose(distance_from_ocean)[0];
x2 = np.transpose(swim_lessons)[0];
y = np.transpose(drownings)[0];
x1_range = np.linspace(x1.min(), x1.max(), 100)
x2_range = np.linspace(x2.min(), x2.max(), 100)
x1_mesh, x2_mesh = np.meshgrid(x1_range, x2_range)
# Predict the response for each point in the meshgrid
dat = {"w": distance_from_ocean, "x": swim_lessons, "y": drownings}
from statsmodels.formula.api import ols # import the required module
regression_results_2_predictor = ols("y ~1 + w + x", data=dat).fit()
coefficients = regression_results_2_predictor.params
y_pred_mesh = coefficients[0] + coefficients[1] * x1_mesh + coefficients[2] * x2_mesh
# Create an interactive 3D plot using plotly
fig = go.Figure()
# Scatter plot for data points
fig.add_trace(go.Scatter3d(
x=x1,
y=x2,
z=y,
mode='markers',
marker=dict(size=5, color='red') #,
# name='Data Points'
))
# Surface plot for OLS regression surface
fig.add_trace(go.Surface(
x=x1_mesh,
y=x2_mesh,
z=y_pred_mesh,
#colorscale='blues',
opacity=0.7,
name='OLS Surface'
))
# Set layout
fig.update_layout(
scene=dict(
xaxis_title='Distance from ocean',
yaxis_title='Swim lessons',
zaxis_title='Drownings',
)
)
fig.update_layout(width=800, height=600)
# Show the interactive plot
fig.show()
# Show the plot
plt.show()
def compute_residuals_2d(swim_lessons, drownings):
from statsmodels.formula.api import ols # import the required module
dat = {"x": swim_lessons, "y": drownings} # define the predictor "x" and outcome "y"
regression_results = ols("y ~ 1 + x", data=dat).fit() # fit the model.
residuals = regression_results.resid
return residuals
def compute_residuals_3d(swim_lessons, drownings, distance_from_ocean):
from statsmodels.formula.api import ols # import the required module
dat = {"w": distance_from_ocean, "x": swim_lessons, "y": drownings}
regression_results = ols("y ~1 + w + x", data=dat).fit()
residuals = regression_results.resid
return residuals