# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
#Load packages
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from scipy.stats import randint
from sklearn.metrics import roc_curve, roc_auc_score, auc
from sklearn.model_selection import RandomizedSearchCV
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
from sklearn.metrics import confusion_matrix,plot_confusion_matrix
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split,cross_val_score,KFold
from sklearn.metrics import classification_report,accuracy_score,precision_score,recall_score,f1_score
from sklearn.preprocessing import LabelEncoder,OneHotEncoder,StandardScaler,OrdinalEncoder,LabelBinarizer
from sklearn.ensemble import RandomForestClassifier
df = pd.read_csv('../input/depression-anxiety-stress-scales/DASS_data_21.02.19/data.csv', sep=r'\t', engine='python')
df.head()
print(df.shape)
print(df.dtypes)
"""
**Extracting the psych questions and storing their answers into a separate dataframe.**
"""
# Extract the columns with just the answers to the first 42 questions
df2 = df.loc[:,::3]
df3 = df2.iloc[:,0:42]
df3.head()
# Dataframe summary statistics
df3.describe()
# Majority of the people (average value) confessesed that they were anxious/upset to some extent.
# Making a correlation plot for df3
plt.figure(figsize=(16, 16))
sns.heatmap(df3.corr(), cmap='BuPu')
"""
**Extracting the personality questions and storing their answers into a separate dataframe.**
"""
# Now extracting the dataframe with personality identification questions
print("Index of TIPI1 column is: " + str(df.columns.get_loc("TIPI1")))
df4 = df.iloc[:,131:141]
df4.head()
"""
***About 53% of the participants agree moderately/strongly that they are anxious/easily upset!***
"""
# Pie chart to visualize the Anxious/Easily upset personality type
df4['TIPI4'].value_counts().plot(kind='pie', autopct='%1.0f%%')
# So how can we predict their personality based on the questions they answered earlier?
"""
**Changing the multi-class target variable (TIPI4 - Personality type:Anxiety,Easily Upset) to binary (1:Anxiety, 0:No Anxiety)**
"""
# Setting target and features for basic classfication model
df4["TIPI4"] = df4["TIPI4"].replace([5, 4, 3, 2, 1], 0)
df4["TIPI4"] = df4["TIPI4"].replace([7, 6], 1)
target = df4["TIPI4"] 
features = df3
"""
**Extracting the participants' background info and storing the responses in a separate dataframe.**
"""
bkgd_features = df.loc[:, ["education", "urban", "gender", "engnat", "age", "religion", "orientation","race", "voted", "married", "familysize"]]
bkgd_features.head()
# Making a correlation plot for background features - don't see any multicollinearity!
plt.figure(figsize=(8, 5))
sns.heatmap(bkgd_features.corr(), cmap='BuPu')
"""
**Merging the background info df with the target variable**
"""
bkgd_table = pd.concat([bkgd_features, target], axis=1)
bkgd_table.head()
"""
# Analyzing the age column 
"""
mean_age_ofAnxiety = bkgd_table.groupby(bkgd_table["TIPI4"] == 1)["age"].mean()
print("Average age of people w/ anxiety is " + str("%.2f" % mean_age_ofAnxiety.iloc[1]) + " years")
# Getting the correlation between the married feature and anxiety
# printing out categories of education column - some participants entered 0?!
print(bkgd_table['married'].unique())
data1 = bkgd_table[bkgd_table["married"] > 0]
print(data1['married'].unique())
df1_crosstab = pd.crosstab(data1["TIPI4"], data1["married"])
df1_crosstab.head()
df1_crosstab.loc[len(df1_crosstab.index)] = df1_crosstab.iloc[1, :]/ (df1_crosstab.iloc[0, :] + df1_crosstab.iloc[1, :])
pd.options.display.float_format = "{:,.2f}".format
df1_new = df1_crosstab.rename(columns={1: 'Never Married',2: 'Currently Married', 3: 'Previously Married'}, index={0: 'No Anxiety', 1: 'Anxiety', 2: '%'})
df1_new.head()
# It's interesting to see that 42% of both currently and previously married people have anxiety issues.
# Not suprisingly, the # of single people w/ anxiety is 12% higher than that of currently and previously married people.
"""
# Analyzing the orientation column 
"""
# printing out categories of sexual orientation- some participants entered 0?!
data2 = bkgd_table[bkgd_table["orientation"] > 0]
df2_crosstab = pd.crosstab(data2["TIPI4"], data2["orientation"])
df2_crosstab.head()
df2_crosstab.loc[len(df2_crosstab.index)] = df2_crosstab.iloc[1, :]/ (df2_crosstab.iloc[0, :] + df2_crosstab.iloc[1, :])
pd.options.display.float_format = "{:,.2f}".format
df2_new = df2_crosstab.rename(columns={1: 'He',2: 'Bi', 3: 'Ho', 4: 'A', 5:'Other' }, index={0: 'No Anxiety', 1: 'Anxiety', 2: '%'})
df2_new.head()
# It was interesting to see that 56% of both the homosexual and asexual populations (individually) have anxiety issues. 
# This number is 7% lower for the heterosexual population and 5% higher for the bisexual population.
"""
# Analyzing the race column 
"""
# printing out categories of race column - some participants entered 0?!
data3 = bkgd_table[bkgd_table["race"] > 0]
df3_crosstab = pd.crosstab(data3["TIPI4"], data3["race"])
df3_crosstab.head()
df3_crosstab.loc[len(df3_crosstab.index)] = df3_crosstab.iloc[1, :]/ (df3_crosstab.iloc[0, :] + df3_crosstab.iloc[1, :])
pd.options.display.float_format = "{:,.2f}".format
df3_new = df3_crosstab.rename(columns={10: 'Asian',20: 'Arab', 30: 'Black', 40: 'Indigenous Australian', 50:'Native American', 60:'White', 70:'Other' }, index={0: 'No Anxiety', 1: 'Anxiety', 2: '%'})
df3_new.head()
# About 50% of most racial populations have anxiety issues. This is consistent with the initial pie chart visualization. 
# But interestingly, about 62% of the the Native American population suffer from anxiety issues.
"""
# Analyzing the education column 
"""
# printing out categories of education column - some participants entered 0?!
print(bkgd_table['education'].unique())
data4 = bkgd_table[bkgd_table["education"] > 0]
print(data4['education'].unique())
# getting correlation between education level and anxiety
Ed_level = ['Less than high school','High School','Uni degree','Grad degree']
df4_crosstab = pd.crosstab(data4["TIPI4"], data4["education"])
df4_crosstab.head()
df4_crosstab.loc[len(df4_crosstab.index)] = df4_crosstab.iloc[1, :]/ (df4_crosstab.iloc[0, :] + df4_crosstab.iloc[1, :])
pd.options.display.float_format = "{:,.2f}".format
df4_new = df4_crosstab.rename(columns={1: 'Some HS',2: 'HS', 3: 'UG', 4: 'GRAD' }, index={0: 'No Anxiety', 1: 'Anxiety', 2: '%'})
df4_new.head()
# It's not suprising to see that as the education level increases (from Some HS to GRAD degree) the 
# % participants w/anxiety decreases.
"""
# Building a Random Forest model to predict anxiety w/ background info and psych questions
"""
"""
**Merging the background info df and psych questions df**
"""
data = pd.concat([df3, bkgd_features, target], axis=1)
#Drop na observations
data_final=data.dropna(how='any')
data_final_2 = data_final.loc[~((data_final['education'] == 0) | (data_final['urban'] == 0)|(data_final['gender'] == 0)|(data_final['engnat'] == 0)|(data_final['age'] == 0)|(data_final['religion'] == 0)|(data_final['orientation'] == 0)|(data_final['race'] == 0)|(data_final['voted'] == 0)|(data_final['married'] == 0)|(data_final['familysize'] == 0))]
data_final_2.head()

# Normalize the feature columns
from sklearn import preprocessing
from sklearn.preprocessing import MinMaxScaler
min_max_scaler = preprocessing.MinMaxScaler()
all_features_final = data_final_2.drop("TIPI4", axis = 1)
target_final = data_final_2["TIPI4"]
X_minmax = min_max_scaler.fit_transform(all_features_final)
X_train, X_test, y_train, y_test = train_test_split(X_minmax, target_final, test_size=0.2, random_state=42)
#Define the model
RFmod = RandomForestClassifier(random_state=1)

#Choose some hyperparameter values 
RFparams={'n_estimators':randint(10,1000),'max_features':['sqrt',None],
          'max_depth':randint(1,10),'min_samples_leaf':randint(1,10)}
#Run the random search
clfRF = RandomizedSearchCV(RFmod,RFparams,#model and parameters
                             cv=10,#number of cross validation folds
                             scoring='roc_auc',#accuracy metric
                             n_iter=1)#number of random parameter combinations
clfRF.fit(X_train,y_train)
#Look at the parameters for the best model
clfRF.best_estimator_
# Compute the training and testing set ROC curves
clfpreds_train = clfRF.best_estimator_.predict_proba(X_train).T[1]
fpr1, tpr1, thresh1 = roc_curve(y_train, clfpreds_train)
roc_auc_train= roc_auc_score(y_train, clfpreds_train)

clfpreds_test = clfRF.best_estimator_.predict_proba(X_test).T[1]
fpr2, tpr2, thresh2 = roc_curve(y_test, clfpreds_test)
roc_auc_test= roc_auc_score(y_test, clfpreds_test)

# Plot the ROC curves
plt.plot([0, 1], [0, 1], linestyle='--')
plt.plot(fpr1, tpr1, label='Training set (AUC = %0.2f)' % roc_auc_train)
plt.plot(fpr2, tpr2, label='Testing set (AUC = %0.2f)' % roc_auc_test)
plt.xlim([-0.01, 1.0])
plt.ylim([0.0, 1.01])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.legend(loc='lower right');
plt.show()
"""
# Next try out a neural network model!
"""