2021-07-02 16:33:48 +02:00
|
|
|
# ---
|
|
|
|
# jupyter:
|
|
|
|
# jupytext:
|
|
|
|
# formats: ipynb,py:percent
|
|
|
|
# text_representation:
|
|
|
|
# extension: .py
|
|
|
|
# format_name: percent
|
|
|
|
# format_version: '1.3'
|
|
|
|
# jupytext_version: 1.11.2
|
|
|
|
# kernelspec:
|
|
|
|
# display_name: straw2analysis
|
|
|
|
# language: python
|
|
|
|
# name: straw2analysis
|
|
|
|
# ---
|
|
|
|
|
|
|
|
# %%
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import seaborn as sns
|
|
|
|
|
|
|
|
nb_dir = os.path.split(os.getcwd())[0]
|
|
|
|
if nb_dir not in sys.path:
|
|
|
|
sys.path.append(nb_dir)
|
|
|
|
import participants.query_db
|
|
|
|
from features.esm import *
|
|
|
|
|
|
|
|
# %%
|
|
|
|
participants_inactive_usernames = participants.query_db.get_usernames(
|
|
|
|
collection_start=datetime.date.fromisoformat("2020-08-01")
|
|
|
|
)
|
|
|
|
df_esm_inactive = get_esm_data(participants_inactive_usernames)
|
|
|
|
|
|
|
|
# %%
|
|
|
|
df_esm_preprocessed = preprocess_esm(df_esm_inactive)
|
2021-07-03 16:34:11 +02:00
|
|
|
|
|
|
|
# %% [markdown]
|
|
|
|
# # PANAS
|
2021-07-02 16:33:48 +02:00
|
|
|
|
|
|
|
# %%
|
2021-07-03 16:34:11 +02:00
|
|
|
df_esm_PANAS = df_esm_preprocessed[
|
|
|
|
(df_esm_preprocessed["questionnaire_id"] == 8)
|
|
|
|
| (df_esm_preprocessed["questionnaire_id"] == 9)
|
2021-07-02 16:33:48 +02:00
|
|
|
]
|
2021-07-03 16:34:11 +02:00
|
|
|
df_esm_PANAS_clean = clean_up_esm(df_esm_PANAS)
|
2021-07-02 16:33:48 +02:00
|
|
|
|
2021-07-04 13:41:34 +02:00
|
|
|
# %% [markdown]
|
|
|
|
# Group by participants, date, and subscale and calculate daily means.
|
2021-07-02 16:33:48 +02:00
|
|
|
|
|
|
|
# %%
|
2021-07-04 13:41:34 +02:00
|
|
|
df_esm_PANAS_daily_means = (
|
|
|
|
df_esm_PANAS_clean.groupby(
|
|
|
|
["participant_id", "date_lj", "questionnaire_id"])
|
|
|
|
.esm_user_answer_numeric.agg("mean")
|
2021-07-03 18:46:06 +02:00
|
|
|
.reset_index()
|
2021-07-04 13:41:34 +02:00
|
|
|
.rename(columns={"esm_user_answer_numeric": "esm_numeric_mean"})
|
2021-07-03 18:46:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# %% [markdown]
|
2021-07-04 13:41:34 +02:00
|
|
|
# Next, calculate mean, median, and standard deviation across all days for each participant.
|
2021-07-03 18:46:06 +02:00
|
|
|
|
|
|
|
# %%
|
|
|
|
df_esm_PANAS_summary_participant = (
|
2021-07-04 13:41:34 +02:00
|
|
|
df_esm_PANAS_daily_means.groupby(["participant_id", "questionnaire_id"])
|
2021-07-03 18:46:06 +02:00
|
|
|
.agg(["mean", "median", "std"])
|
|
|
|
.reset_index(col_level=1)
|
|
|
|
)
|
|
|
|
df_esm_PANAS_summary_participant.columns = df_esm_PANAS_summary_participant.columns.get_level_values(
|
|
|
|
1
|
|
|
|
)
|
|
|
|
df_esm_PANAS_summary_participant[
|
|
|
|
"PANAS_subscale"
|
2021-07-04 13:41:34 +02:00
|
|
|
] = df_esm_PANAS_daily_means.questionnaire_id.astype("category").cat.rename_categories(
|
2021-07-03 18:46:06 +02:00
|
|
|
{8.0: "PA", 9.0: "NA"}
|
|
|
|
)
|
|
|
|
|
2021-07-04 13:41:34 +02:00
|
|
|
# %%
|
|
|
|
sns.displot(
|
|
|
|
data=df_esm_PANAS_summary_participant, x="mean", hue="PANAS_subscale", binwidth=0.2
|
|
|
|
)
|
2021-07-03 18:46:06 +02:00
|
|
|
|
|
|
|
# %%
|
|
|
|
sns.displot(
|
2021-07-04 13:41:34 +02:00
|
|
|
data=df_esm_PANAS_summary_participant, x="median", hue="PANAS_subscale", binwidth=0.2
|
2021-07-03 18:46:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# %%
|
|
|
|
sns.displot(
|
2021-07-04 13:41:34 +02:00
|
|
|
data=df_esm_PANAS_summary_participant, x="std", hue="PANAS_subscale", binwidth=0.05
|
2021-07-03 18:46:06 +02:00
|
|
|
)
|
2021-07-04 13:41:34 +02:00
|
|
|
|
|
|
|
# %%
|
|
|
|
df_esm_PANAS_summary_participant[df_esm_PANAS_summary_participant["std"] < 0.1]
|