Skip to content

Commit

Permalink
BLD: begin porting ANOVA chapter (#11)
Browse files Browse the repository at this point in the history
* MAINT: update toc function

* BLD: building

* BUG: add d as a good name to pylint

* MAINT: remove patsy as req
  • Loading branch information
eigenfoo authored Jun 26, 2019
1 parent d3afd94 commit aa9a60f
Show file tree
Hide file tree
Showing 8 changed files with 602 additions and 113 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / stateme
bad-functions=map,filter,input

# Good variable names which should always be accepted, separated by a comma
good-names=a,b,c,f,i,j,k,df,x,y,y2,_,fig,ax
good-names=a,b,c,d,f,i,j,k,df,x,y,y2,_,fig,ax

# Bad variable names which should always be refused, separated by a comma
bad-names=foo,bar,baz,toto,tutu,tata
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ venv: # Set up Python virtual environment.
python -m venv ${VENV_PATH}; \
source ${VENV_PATH}/bin/activate; \
pip install -U pip; \
pip install -r requirements.txt; \
pip install -r requirements-dev.txt; \
deactivate; \
)
@printf "\n\nVirtual environment created! \033[1;34mRun \`source ${VENV_PATH}/bin/activate\` to activate it.\033[0m\n\n\n"
Expand Down
346 changes: 285 additions & 61 deletions index.html

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,54 @@ def dummy_coding_plot():
ax.legend(fontsize="large")

return fig, ax


def one_way_anova_plot():
a = np.random.normal(0, 1, 20)
b = np.random.normal(-2, 1, 20)
c = np.random.normal(3, 1, 20)
d = np.random.normal(1.5, 1, 20)

df = pd.DataFrame()
df["y"] = np.concatenate([a, b, c, d])
df["group_2"] = np.concatenate(
[np.zeros_like(b)] + [np.ones_like(b)] + 2 * [np.zeros_like(b)]
)
df["group_3"] = np.concatenate(
2 * [np.zeros_like(c)] + [np.ones_like(c)] + [np.zeros_like(c)]
)
df["group_4"] = np.concatenate(3 * [np.zeros_like(d)] + [np.ones_like(d)])

res = smf.ols("y ~ 1 + group_2 + group_3 + group_4", df).fit()
beta0, beta1, beta2, beta3 = res.params

fig, ax = plt.subplots(figsize=[10, 8])
ax.scatter(0 * np.ones_like(a), a, color="k")
ax.scatter(1 * np.ones_like(b), b, color="k")
ax.scatter(2 * np.ones_like(c), c, color="k")
ax.scatter(3 * np.ones_like(d), d, color="k")

ax.axhline(beta0, color="b", label=r"$\beta_0$ (group 1 mean)")

ax.plot([0.7, 1.3], 2 * [beta0 + beta1], color="navy")
ax.plot(
[0, 1],
[beta0, beta0 + beta1],
color="r",
label=r"$\beta_1, \beta_2, ...$ (slopes/differences to $\beta_0$)",
)

ax.plot(
[1.7, 2.3],
2 * [beta0 + beta2],
color="navy",
label=r"$\beta_0+\beta_1, \beta_0+\beta_2 ...$ (group 2, 3 ... means)",
)
ax.plot([1, 2], [beta0, beta0 + beta2], color="r")

ax.plot([2.7, 3.3], 2 * [beta0 + beta3], color="navy")
ax.plot([2, 3], [beta0, beta0 + beta3], color="r")

ax.legend(fontsize="large")

return fig, ax
5 changes: 5 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-r requirements.txt
black==19.3b0
nbdime==1.0.6
nbinteract==0.2.4
pylint==2.3.1
5 changes: 0 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
black==19.3b0
jupyter==1.0.0
matplotlib==3.1.0
nbdime==1.0.6
nbinteract==0.2.4
numpy==1.16.4
pandas==0.24.2
patsy==0.5.1
pylint==2.3.1
scipy==1.3.0
statsmodels==0.10.0
297 changes: 254 additions & 43 deletions tests-as-linear.ipynb

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def tabulate_results(test_values, ols_results, names, coeff="x"):
return table


def generate_toc(notebook="tests-as-linear.ipynb"):
def generate_toc(notebook="tests-as-linear.ipynb", max_header_levels=2):
"""
Generates a table of contents in Markdown.
Expand All @@ -70,6 +70,9 @@ def generate_toc(notebook="tests-as-linear.ipynb"):
----------
notebook : str
Path to notebook for which to generate a table of contents.
max_header_levels : int
Maximum number of header levels to show in table of contents (i.e. the
depth of headers to display).
Returns
-------
Expand All @@ -83,7 +86,7 @@ def generate_toc(notebook="tests-as-linear.ipynb"):
for cell in cells:
if cell["cell_type"] == "markdown":
for line in cell["source"]:
match = re.search(r"^#+ ", line)
match = re.search(r"^[#]{{1,{0}}} ".format(max_header_levels), line)
if match:
level = len(line) - len(line.lstrip("#"))
link = line.strip(" #\n").replace(" ", "-")
Expand Down

0 comments on commit aa9a60f

Please sign in to comment.