Basic Statistics

Load Libraries

# if you haven't used a given package before, you'll need to download it first
# delete the "#" before the install function and run it to download
# re-insert the "#" before the install function so that the file will Knit later
# then run the library function calling that package

#install.packages("psych")
#install.packages("expss")

library(psych) # for the describe() command
library(expss) # for the cross_cases() command
## Warning: package 'expss' was built under R version 4.4.3
## Loading required package: maditr
## Warning: package 'maditr' was built under R version 4.4.3
## 
## To aggregate several columns with one summary: take(mtcars, mpg, hp, fun = mean, by = am)

##Import Data

# Import the "fakedata.csv" file

d2 <- read.csv("Data/projectdata.csv")


# Note: for the HW, you will import "projectdata.csv" that you created and exported in the Data Prep Lab

Univariate Plots: Histograms & Tables

Tables are used to visualize individual categorical variables. Histograms are used to visualize individual continuous variables.

# use tables to visualize categorical data
table(d2$mhealth)
## 
##              anxiety disorder                       bipolar 
##                            42                             3 
##                    depression              eating disorders 
##                             4                            14 
##                    none or NA obsessive compulsive disorder 
##                           162                            11 
##                         other                          ptsd 
##                            11                             9
table(d2$treatment)
## 
##                    in treatment      no psychological disorders 
##                              31                              83 
##                not in treatment                           other 
##                             105                               5 
##               seeking treatment treatment disrupted by COVID-19 
##                              11                              21
# use histograms to visualize continuous data
hist(d2$phq)

hist(d2$gad)

hist(d2$rse)

hist(d2$brs)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##            vars   n    mean     sd  median trimmed    mad  min  max range  skew
## X             1 256 7532.89 731.65 7484.50 7522.92 902.90 6294 8858  2564  0.11
## mhealth*      2 256    4.46   1.76    5.00    4.55   0.00    1    8     7 -0.82
## treatment*    3 256    2.79   1.30    3.00    2.63   1.48    1    6     5  1.08
## phq           4 256    2.69   0.85    2.78    2.70   0.99    1    4     3 -0.09
## gad           5 256    2.65   0.92    2.71    2.67   1.06    1    4     3 -0.18
## rse           6 256    2.15   0.65    2.00    2.11   0.59    1    4     3  0.51
## brs           7 256    2.56   0.85    2.50    2.54   0.74    1    5     4  0.27
##            kurtosis    se
## X             -1.16 45.73
## mhealth*       0.29  0.11
## treatment*     0.88  0.08
## phq           -1.06  0.05
## gad           -1.15  0.06
## rse           -0.27  0.04
## brs           -0.39  0.05
## For the required write-up below, choose one of these options to paste and edit below based on your output.

## OPTION 1
# We analyzed the skew and kurtosis of our continuous variables and all were within the accepted range (-2/+2).

## OPTION 2
# We analyzed the skew and kurtosis of our continuous variables and (#) were within the accepted range (-2/+2). However, (#) variables (list variable name(s) here) were outside of the accepted range. For this analysis, we will use them anyway, but outside of this class this is bad practice.

We analyzed the skew and kurtosis of our continuous variables and all were within the accepted range (-2/+2)

Bivariate Plots

Crosstabs

Crosstabs are used to visualize combinations of two categorical variables.

cross_cases(d2,mhealth, treatment)
 treatment 
 in treatment   no psychological disorders   not in treatment   other   seeking treatment   treatment disrupted by COVID-19 
 mhealth 
   anxiety disorder  8 1 23 1 4 5
   bipolar  2 1
   depression  2 1 1
   eating disorders  7 3 4
   none or NA  4 81 65 2 6 4
   obsessive compulsive disorder  3 6 2
   other  7 1 1 2
   ptsd  2 4 1 2
   #Total cases  31 83 105 5 11 21
# Note: for HW, replace the two lab variables with your project ones)

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$phq, d2$gad,
     main="Scatterplot of Patient Health Questionnaire-9 and Generalized Anxiety Disorder-7",
     xlab = "Patient Health Questionaire-9",
     ylab = "Generalized Anxiety Disorder-7")

plot(d2$rse, d2$brs,
     main="Scatterplot of Rosenberg Self-Esteem Inventory and Brief Resilience Scale",
     xlab = "Rosenberg Self-Esteem Inventory",
     ylab = "Brief Resilience Scale")

# Note: for HW, you will choose to plot 2 combos of your 4 continuous variables, based on your hypotheses. You may repeat 1 variable to see its association with 2 others. You will need replace the variable names on the first line of the function as well as the 'main' (aka plot title), 'xlab' and 'ylab' lines to correctly label the graphs -- remember to use the actual variable names, not their scales, so someone reading your plots can understand them.

Boxplots

Boxplots are used to visualize combinations of one categorical and one continuous variable.

# ORDER MATTERS HERE: 'continuous variable' ~ 'categorical variable' 

boxplot(data=d2, phq ~ mhealth,
        main="Boxplot of Mental Health Disorders and Patient Health Questionaire-9",
        xlab = "Mental Health Disorders",
        ylab = "Patient Health Questionaire-9")

boxplot(data=d2, rse ~ treatment,
        main="Boxplot of Treatment and Rosenberg Self-Esteem Inventory",
        xlab = "Treatment",
        ylab = "Rosenberg Self-Esteem Inventory")

# Note: for HW, you will choose to plot 2 combos of any of your 4 continuous variables with either of your 2 categorical variables, based on your hypotheses. You may repeat 1 variable to see its association with others. Again, you will need replace the variable names on the first line of the function as well as the 'main' (aka plot title), 'xlab' and 'ylab' lines to correctly label the graphs -- remember to use the actual variable names, not their scales, so someone reading your plots can understand them.

We did it!!