import sys
sys.path.append('../')
import pandas as pd
import numpy as np
import forestplot as fp
pd.set_option('display.max_rows', 100)
df = (pd.read_csv('../examples/data/sleep-mmodel.csv')
.query("model=='all' | model=='young kids'")
)
df.head(3)
var | coef | se | T | pval | r2 | adj_r2 | ll | hl | model | group | label | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | age | 0.994889 | 1.969249 | 0.505213 | 0.613625 | 0.127289 | 0.103656 | -2.873825 | 4.863603 | all | age | in years |
3 | age | 22.634017 | 15.495320 | 1.460700 | 0.149315 | 0.178147 | -0.013619 | -8.361238 | 53.629272 | young kids | age | in years |
4 | black | -84.796612 | 82.150125 | -1.032215 | 0.302454 | 0.127289 | 0.103656 | -246.185715 | 76.592491 | all | other factors | =1 if black |
std_opts = dict(
dataframe = df,
estimate = "coef",
ll ="ll",
hl = "hl",
varlabel = "var",
model_col = "model",
return_df=True,
)
# Vanilla
_df, ax = fp.mforestplot(**std_opts)
# Vanilla
# Color rows and add borders
_df, ax = fp.mforestplot(**std_opts, color_alt_rows=True, despine=False)
# Vanilla
# Color rows
# Add annotation and make it a table
# Add right annotations
_df, ax = fp.mforestplot(**std_opts, color_alt_rows=True,
annote=["label"], annoteheaders=["Description"],
rightannote=["group"],
right_annoteheaders=["Group"],
table=True)
std_opts = dict(
dataframe = df,
estimate = "coef",
ll ="ll",
hl = "hl",
varlabel = "label",
model_col = "model",
return_df=True,
)
# With groups
_df, ax = fp.mforestplot(**std_opts, color_alt_rows=True,
groupvar="group",
rightannote=["group"],
right_annoteheaders=["Group"],
variable_header="Covariate",
table=True)
# With groups and group ordering
group_order = ["family factors", "health factors", "labor factors", "age", "occupation", "area of residence", "other factors"]
_df, ax = fp.mforestplot(**std_opts, color_alt_rows=True,
groupvar="group",
group_order=group_order,
rightannote=["group"],
right_annoteheaders=["Group"],
variable_header="Covariate",
table=True)
# With groups and group ordering
group_order = ["family factors", "health factors", "labor factors", "age", "occupation", "area of residence", "other factors"]
_df, ax = fp.mforestplot(**std_opts, color_alt_rows=True,
groupvar="group",
group_order=group_order,
rightannote=["group"],
right_annoteheaders=["Group"],
variable_header="Covariate",
capitalize="capitalize",
table=True)
# Add groups
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
)
# Add groups
# With border + color alternate rows
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
despine=False,
color_alt_rows=True,
)
# Add groups
# Add annotation on right
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
rightannote=['group'],
right_annoteheaders=['Variable group'],
)
std_opts = dict(
dataframe = df,
estimate = "coef",
ll ="ll",
hl = "hl",
varlabel = "var",
model_col = "model",
rightannote=['label','group'],
right_annoteheaders=['Description', 'Group'],
return_df=True,
)
# No groups
# Add multiple annotation on right
_df, ax = fp.mforestplot(**std_opts,
)
# No groups
# Add annotation on right
# No flush
_df, ax = fp.mforestplot(**std_opts,
flush=False,
)
# No groups
# Add annotation on right
# Add row colors
# No flush
_df, ax = fp.mforestplot(**std_opts,
flush=False,
color_alt_rows=True,
)
# W/ groups
# Add annotation on right
# Add row colors
# No flush
std_opts = dict(
dataframe = df,
estimate = "coef",
ll ="ll",
hl = "hl",
varlabel = "label",
model_col = "model",
rightannote=['var','group'],
right_annoteheaders=['Field', 'Group'],
return_df=True,
)
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
flush=False,
color_alt_rows=True,
)
std_opts = dict(
dataframe = df,
estimate = "coef",
ll ="ll",
hl = "hl",
varlabel = "label",
model_col = "model",
rightannote=['var','group'],
right_annoteheaders=['Field', 'Group'],
annote=['var','group'],
annoteheaders=['Field', 'Group'],
return_df=True,
)
# No groups
_df, ax = fp.mforestplot(**std_opts,
)
# Add group
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
)
# Add group
# Color row
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
color_alt_rows=True,
)
# Add group
# Keep borders
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
color_alt_rows=True,
**{'despine':False, "figsize":(4,8)}
)
# Add group
# Keep borders
# Add X-label
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
color_alt_rows=True,
**{'despine':False, "figsize":(4,8)}
)
# Add group
# Color row
# Change legend label
# Add x-label
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
modellabels=["Model 1", "Model 2"],
xlabel="X-label",
color_alt_rows=True,
)
# Add group
# Color row
# Change legend label
# Add x-label
# Legend at top
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
modellabels=["Model 1", "Model 2"],
xlabel="X-label",
color_alt_rows=True,
bbox_to_anchor=(.5,.95)
)
# Add group
# Color row
# Change legend label
# Add x-label
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
modellabels=["Model 1", "Model 2"],
xlabel="X-label",
color_alt_rows=True,
# bbox_to_anchor=None,
)
df = (pd.read_csv('../examples/data/sleep-mmodel.csv')
# .query("model=='all' | model=='young kids'")
)
df
var | coef | se | T | pval | r2 | adj_r2 | ll | hl | model | group | label | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | age | 0.994889 | 1.969249 | 0.505213 | 6.136249e-01 | 0.127289 | 0.103656 | -2.873825 | 4.863603 | all | age | in years |
1 | age | 1.482893 | 2.885462 | 0.513919 | 6.077171e-01 | 0.180382 | 0.139106 | -4.197237 | 7.163023 | men | age | in years |
2 | age | 0.953200 | 2.943209 | 0.323864 | 7.463431e-01 | 0.109105 | 0.053425 | -4.846721 | 6.753121 | women | age | in years |
3 | age | 22.634017 | 15.495320 | 1.460700 | 1.493148e-01 | 0.178147 | -0.013619 | -8.361238 | 53.629272 | young kids | age | in years |
4 | black | -84.796612 | 82.150125 | -1.032215 | 3.024539e-01 | 0.127289 | 0.103656 | -246.185715 | 76.592491 | all | other factors | =1 if black |
5 | black | -132.967109 | 132.581166 | -1.002911 | 3.167759e-01 | 0.180382 | 0.139106 | -393.957639 | 128.023420 | men | other factors | =1 if black |
6 | black | -68.350631 | 108.901754 | -0.627636 | 5.308819e-01 | 0.109105 | 0.053425 | -282.953621 | 146.252360 | women | other factors | =1 if black |
7 | black | -119.741039 | 296.432953 | -0.403940 | 6.876932e-01 | 0.178147 | -0.013619 | -712.695230 | 473.213151 | young kids | other factors | =1 if black |
8 | clerical | 22.505827 | 48.963263 | 0.459647 | 6.459627e-01 | 0.127289 | 0.103656 | -73.685593 | 118.697248 | all | occupation | =1 if clerical worker |
9 | clerical | -229.275962 | 102.888221 | -2.228399 | 2.665377e-02 | 0.180382 | 0.139106 | -431.814921 | -26.737003 | men | occupation | =1 if clerical worker |
10 | clerical | 106.507258 | 61.127321 | 1.742384 | 8.281375e-02 | 0.109105 | 0.053425 | -13.950911 | 226.965427 | women | occupation | =1 if clerical worker |
11 | clerical | -239.902574 | 211.760200 | -1.132897 | 2.617642e-01 | 0.178147 | -0.013619 | -663.486040 | 183.680893 | young kids | occupation | =1 if clerical worker |
12 | construc | 113.929558 | 105.045137 | 1.084577 | 2.786145e-01 | 0.127289 | 0.103656 | -92.438240 | 320.297357 | all | occupation | =1 if construction worker |
13 | construc | 65.696059 | 107.889208 | 0.608922 | 5.430737e-01 | 0.180382 | 0.139106 | -146.687512 | 278.079631 | men | occupation | =1 if construction worker |
14 | construc | 361.756430 | 428.264834 | 0.844703 | 3.991782e-01 | 0.109105 | 0.053425 | -482.186946 | 1205.699805 | women | occupation | =1 if construction worker |
15 | construc | -186.415201 | 358.223501 | -0.520388 | 6.047082e-01 | 0.178147 | -0.013619 | -902.968890 | 530.138488 | young kids | occupation | =1 if construction worker |
16 | educ | -7.210558 | 7.516736 | -0.959267 | 3.378725e-01 | 0.127289 | 0.103656 | -21.977660 | 7.556543 | all | labor factors | years of schooling |
17 | educ | -7.545420 | 9.720341 | -0.776250 | 4.382610e-01 | 0.180382 | 0.139106 | -26.680241 | 11.589402 | men | labor factors | years of schooling |
18 | educ | -6.278397 | 11.869883 | -0.528935 | 5.973743e-01 | 0.109105 | 0.053425 | -29.669318 | 17.112525 | women | labor factors | years of schooling |
19 | educ | -20.972116 | 26.974032 | -0.777493 | 4.399230e-01 | 0.178147 | -0.013619 | -74.928212 | 32.983981 | young kids | labor factors | years of schooling |
20 | gdhlth | -82.580434 | 58.204717 | -1.418793 | 1.565620e-01 | 0.127289 | 0.103656 | -196.927272 | 31.766405 | all | health factors | =1 if in good or excel. health |
21 | gdhlth | -188.284508 | 80.476539 | -2.339620 | 2.001135e-02 | 0.180382 | 0.139106 | -346.705309 | -29.863707 | men | health factors | =1 if in good or excel. health |
22 | gdhlth | -24.491583 | 87.716867 | -0.279212 | 7.803399e-01 | 0.109105 | 0.053425 | -197.347402 | 148.364236 | women | health factors | =1 if in good or excel. health |
23 | gdhlth | -141.814976 | 203.213022 | -0.697864 | 4.879582e-01 | 0.178147 | -0.013619 | -548.301540 | 264.671589 | young kids | health factors | =1 if in good or excel. health |
24 | lhrwage | 21.605697 | 31.989301 | 0.675404 | 4.997212e-01 | 0.127289 | 0.103656 | -41.239303 | 84.450696 | all | labor factors | log hourly wage |
25 | lhrwage | 3.544752 | 47.045923 | 0.075347 | 9.399931e-01 | 0.180382 | 0.139106 | -89.066745 | 96.156249 | men | labor factors | log hourly wage |
26 | lhrwage | -22.580482 | 53.782320 | -0.419850 | 6.749978e-01 | 0.109105 | 0.053425 | -128.564512 | 83.403547 | women | labor factors | log hourly wage |
27 | lhrwage | 35.609317 | 122.323738 | 0.291107 | 7.719733e-01 | 0.178147 | -0.013619 | -209.074589 | 280.293223 | young kids | labor factors | log hourly wage |
28 | prot | -9.249430 | 39.771462 | -0.232564 | 8.161916e-01 | 0.127289 | 0.103656 | -87.382976 | 68.884117 | all | other factors | =1 if Protestant |
29 | prot | -20.967328 | 52.201060 | -0.401665 | 6.882393e-01 | 0.180382 | 0.139106 | -123.726888 | 81.792233 | men | other factors | =1 if Protestant |
30 | prot | 1.066643 | 61.898047 | 0.017232 | 9.862667e-01 | 0.109105 | 0.053425 | -120.910327 | 123.043614 | women | other factors | =1 if Protestant |
31 | prot | -24.173124 | 127.566497 | -0.189494 | 8.503450e-01 | 0.178147 | -0.013619 | -279.344111 | 230.997863 | young kids | other factors | =1 if Protestant |
32 | selfe | -21.274179 | 63.303918 | -0.336064 | 7.369588e-01 | 0.127289 | 0.103656 | -145.638720 | 103.090363 | all | labor factors | =1 if self employed |
33 | selfe | 3.484094 | 77.585707 | 0.044906 | 9.642142e-01 | 0.180382 | 0.139106 | -149.246005 | 156.214193 | men | labor factors | =1 if self employed |
34 | selfe | -90.685936 | 112.556497 | -0.805693 | 4.212746e-01 | 0.109105 | 0.053425 | -312.491002 | 131.119130 | women | labor factors | =1 if self employed |
35 | selfe | -370.956512 | 241.153473 | -1.538259 | 1.292427e-01 | 0.178147 | -0.013619 | -853.335278 | 111.422255 | young kids | labor factors | =1 if self employed |
36 | smsa | -40.653557 | 39.507723 | -1.029003 | 3.039595e-01 | 0.127289 | 0.103656 | -118.268971 | 36.961857 | all | area of residence | =1 if live in smsa |
37 | smsa | -27.631427 | 52.418593 | -0.527130 | 5.985235e-01 | 0.180382 | 0.139106 | -130.819208 | 75.556354 | men | area of residence | =1 if live in smsa |
38 | smsa | -57.146400 | 61.521030 | -0.928892 | 3.539449e-01 | 0.109105 | 0.053425 | -178.380417 | 64.087616 | women | area of residence | =1 if live in smsa |
39 | smsa | 43.429572 | 138.683089 | 0.313157 | 7.552474e-01 | 0.178147 | -0.013619 | -233.977910 | 320.837053 | young kids | area of residence | =1 if live in smsa |
40 | south | 82.486980 | 46.377571 | 1.778596 | 7.589350e-02 | 0.127289 | 0.103656 | -8.624685 | 173.598644 | all | area of residence | =1 if live in south |
41 | south | 79.401694 | 64.439832 | 1.232183 | 2.189217e-01 | 0.180382 | 0.139106 | -47.450305 | 206.253693 | men | area of residence | =1 if live in south |
42 | south | 114.622934 | 69.300494 | 1.653999 | 9.952837e-02 | 0.109105 | 0.053425 | -21.941379 | 251.187246 | women | area of residence | =1 if live in south |
43 | south | 71.851381 | 136.010112 | 0.528280 | 5.992538e-01 | 0.178147 | -0.013619 | -200.209350 | 343.912111 | young kids | area of residence | =1 if live in south |
44 | spsepay | -0.001005 | 0.002648 | -0.379739 | 7.042954e-01 | 0.127289 | 0.103656 | -0.006207 | 0.004196 | all | other factors | spousal wage income |
45 | spsepay | 0.003069 | 0.004924 | 0.623344 | 5.335697e-01 | 0.180382 | 0.139106 | -0.006624 | 0.012762 | men | other factors | spousal wage income |
46 | spsepay | -0.000508 | 0.003621 | -0.140324 | 8.885302e-01 | 0.109105 | 0.053425 | -0.007644 | 0.006628 | women | other factors | spousal wage income |
47 | spsepay | -0.004701 | 0.011443 | -0.410849 | 6.826471e-01 | 0.178147 | -0.013619 | -0.027591 | 0.018188 | young kids | other factors | spousal wage income |
48 | totwrk | -0.151922 | 0.020366 | -7.459430 | 3.698705e-13 | 0.127289 | 0.103656 | -0.191934 | -0.111911 | all | labor factors | mins worked per week |
49 | totwrk | -0.191008 | 0.029765 | -6.417152 | 5.964764e-10 | 0.180382 | 0.139106 | -0.249602 | -0.132414 | men | labor factors | mins worked per week |
50 | totwrk | -0.125512 | 0.032713 | -3.836783 | 1.621349e-04 | 0.109105 | 0.053425 | -0.189977 | -0.061048 | women | labor factors | mins worked per week |
51 | totwrk | -0.112995 | 0.076363 | -1.479712 | 1.441818e-01 | 0.178147 | -0.013619 | -0.265743 | 0.039753 | young kids | labor factors | mins worked per week |
52 | yrsmarr | -0.075958 | 2.009356 | -0.037802 | 9.698602e-01 | 0.127289 | 0.103656 | -4.023464 | 3.871548 | all | family factors | years married |
53 | yrsmarr | 0.224116 | 2.934791 | 0.076365 | 9.391834e-01 | 0.180382 | 0.139106 | -5.553120 | 6.001352 | men | family factors | years married |
54 | yrsmarr | -1.414137 | 2.931985 | -0.482314 | 6.300539e-01 | 0.109105 | 0.053425 | -7.191939 | 4.363665 | women | family factors | years married |
55 | yrsmarr | -31.211144 | 16.914011 | -1.845283 | 6.993267e-02 | 0.178147 | -0.013619 | -65.044204 | 2.621915 | young kids | family factors | years married |
std_opts = dict(
dataframe = df,
estimate = "coef",
ll ="ll",
hl = "hl",
varlabel = "label",
model_col = "model",
rightannote=['var','group'],
right_annoteheaders=['Field', 'Group'],
annote=['var','group'],
annoteheaders=['Field', 'Group'],
return_df=True,
)
# Add group
# Color row
# Change legend label
# Add x-label
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
xlabel="X-label",
color_alt_rows=True,
# bbox_to_anchor=None,
)
# Add group
# Color row
# Change legend label
# Add x-label
modellabels=["Have young kids", "Men", "Women", "Full sample"]
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
modellabels=modellabels,
xlabel="X-label",
color_alt_rows=True,
)
# Add group
# Color row
# Change legend label
# Add x-label
# Legend at top
modellabels=["Have young kids", "Women", "Men", "Full sample"]
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
modellabels=modellabels,
xlabel="X-label",
color_alt_rows=True,
bbox_to_anchor=(.5,.95)
)
# Add group
# Color row
# Change legend label
# Add x-label
# Legend at top
modellabels=["Have young kids", "Women", "Men", "Full sample"]
_df, ax = fp.mforestplot(**std_opts,
modellabels=modellabels,
xlabel="X-label",
color_alt_rows=True,
flush=False,
)
df = (pd.read_csv('../examples/data/sleep-mmodel.csv')
.query("model=='all' | model=='young kids'")
)
std_opts = dict(
dataframe = df,
estimate = "coef",
ll ="ll",
hl = "hl",
varlabel="label",
model_col="model",
)
df.head()
var | coef | se | T | pval | r2 | adj_r2 | ll | hl | model | group | label | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | age | 0.994889 | 1.969249 | 0.505213 | 0.613625 | 0.127289 | 0.103656 | -2.873825 | 4.863603 | all | age | in years |
3 | age | 22.634017 | 15.495320 | 1.460700 | 0.149315 | 0.178147 | -0.013619 | -8.361238 | 53.629272 | young kids | age | in years |
4 | black | -84.796612 | 82.150125 | -1.032215 | 0.302454 | 0.127289 | 0.103656 | -246.185715 | 76.592491 | all | other factors | =1 if black |
7 | black | -119.741039 | 296.432953 | -0.403940 | 0.687693 | 0.178147 | -0.013619 | -712.695230 | 473.213151 | young kids | other factors | =1 if black |
8 | clerical | 22.505827 | 48.963263 | 0.459647 | 0.645963 | 0.127289 | 0.103656 | -73.685593 | 118.697248 | all | occupation | =1 if clerical worker |
_df, ax = fp.mforestplot(**std_opts,
color_alt_rows=True,
groupvar="group",
table=True,
rightannote=["var", "group"],
right_annoteheaders=["Variable", "Variable group"],
xlabel="Coefficient (95% CI)",
modellabels=["Have young kids", "Full sample"],
mcolor=["#CC6677", "#4477AA"],
xticks=[-1200,-600, 0, 600],
return_df=True,
# Additional kwargs for customizations
**{"marker": "D", # set maker symbol as diamond
"markersize": 35, # adjust marker size
"xlinestyle": (0, (10, 5)), # long dash for x-reference line
"xlinecolor": "#808080", # gray color for x-reference line
"xtick_size": 12, # adjust x-ticker fontsize
"xlinestyle": (0, (10, 5)), # long dash for x-reference line
"xlinecolor": ".8", # gray color for x-reference line
}
)