#!/usr/bin/env python
# coding: utf-8
# In[118]:
import numpy as np
import pandas as pd
import statsmodels.api as sm
import plotly.express as px
import plotly.graph_objects as go
# In[180]:
base_age = np.random.normal(loc = 50, scale = 15, size = 20)
age = np.concatenate([base_age, base_age]) + np.random.normal(0, 0.5, 40)
mecfs_status = np.concatenate([np.full(20, 0), np.full(20, 1)])
brain_metric = (0.2 * age) + mecfs_status + np.random.normal(loc=0, scale=0.1, size=40)
df = pd.DataFrame({
"Age": age,
"ME/CFS Status": mecfs_status,
"Brain Metric": brain_metric,
})
df.head()
# In[181]:
X = sm.add_constant(df['ME/CFS Status'])
y = df['Brain Metric']
model = sm.OLS(y, X)
results = model.fit()
fig = px.scatter(df, x='ME/CFS Status', y='Brain Metric', color="Age", opacity=0.65)
fig.update_traces(marker=dict(size=10))
fig.add_traces(go.Scatter(x=X['ME/CFS Status'], y=results.predict(X), showlegend=False))
fig.update_layout(
width=500,
height=500,
title={
'text': "Regression with only ME/CFS Status (Matched for age)",
'y':0.95,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'
}
)
fig.show()
print(results.summary())
# In[182]:
X = sm.add_constant(df[['ME/CFS Status', 'Age']])
y = df['Brain Metric']
results = sm.OLS(y, X).fit()
xrange = [X['ME/CFS Status'].min(), X['ME/CFS Status'].max()]
yrange = [X['Age'].min(), X['Age'].max()]
xx, yy = np.meshgrid(xrange, yrange)
pred = results.predict(sm.add_constant(np.c_[xx.ravel(), yy.ravel()]))
pred = pred.reshape(xx.shape)
# Generate the plot
fig = px.scatter_3d(df, x='ME/CFS Status', y='Age', z='Brain Metric', color="Age")
fig.update_traces(marker=dict(size=3))
fig.add_traces(go.Surface(
x=xrange,
y=yrange,
z=pred,
opacity=0.5,
colorscale=[[0, 'lightblue'], [1, 'lightblue']],
showscale=False,
))
fig.update_layout(
width=800,
height=600,
title={
'text': "Regression with ME/CFS Status and Age (Matched for age)",
'y':0.95,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'
})
fig.show()
print(results.summary())
# In[ ]:
# In[184]:
base_age = np.random.normal(loc=50, scale=15, size=20)
age = np.concatenate([base_age, base_age + 10]) + np.random.normal(0, 0.5, 40)
mecfs_status = np.concatenate([np.full(20, 0), np.full(20, 1)])
brain_metric = (0.2 * age) + mecfs_status + np.random.normal(loc=0, scale=0.1, size=40)
df = pd.DataFrame({
"Age": age,
"ME/CFS Status": mecfs_status,
"Brain Metric": brain_metric,
})
df.head()
# In[185]:
X = sm.add_constant(df['ME/CFS Status'])
y = df['Brain Metric']
model = sm.OLS(y, X)
results = model.fit()
fig = px.scatter(df, x='ME/CFS Status', y='Brain Metric', color="Age", opacity=0.65)
fig.update_traces(marker=dict(size=10))
fig.add_traces(go.Scatter(x=X['ME/CFS Status'], y=results.predict(X), showlegend=False))
fig.update_layout(
width=500,
height=500,
title={
'text': "Regression with only ME/CFS Status (Unmatched ages)",
'y':0.95,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'
}
)
fig.show()
print(results.summary())
# In[186]:
X = sm.add_constant(df[['ME/CFS Status', 'Age']])
y = df['Brain Metric']
results = sm.OLS(y, X).fit()
xrange = [X['ME/CFS Status'].min(), X['ME/CFS Status'].max()]
yrange = [X['Age'].min(), X['Age'].max()]
xx, yy = np.meshgrid(xrange, yrange)
pred = results.predict(sm.add_constant(np.c_[xx.ravel(), yy.ravel()]))
pred = pred.reshape(xx.shape)
# Generate the plot
fig = px.scatter_3d(df, x='ME/CFS Status', y='Age', z='Brain Metric', color="Age")
fig.update_traces(marker=dict(size=3))
fig.add_traces(go.Surface(
x=xrange,
y=yrange,
z=pred,
opacity=0.5,
colorscale=[[0, 'lightblue'], [1, 'lightblue']],
showscale=False,
))
fig.update_layout(
width=600,
height=600,
title={
'text': "Regression with ME/CFS Status and Age (Unmatched ages)",
'y':0.95,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'
})
fig.show()
print(results.summary())