Basic Statistics

Load Libraries

# if you haven't used a given package before, you'll need to download it first
# after download is finished, insert a "#" 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
## Loading required package: maditr
## 
## To select columns from data: columns(mtcars, mpg, vs:carb)
## 
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
##  To return to the console output, use 'expss_output_default()'.

Import & Examine Data

# Import the "projectdata.csv" file

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

str(d2)
## 'data.frame':    588 obs. of  7 variables:
##  $ X        : int  2814 3295 717 6056 4753 5365 2044 1246 1250 1761 ...
##  $ mhealth  : chr  "none or NA" "none or NA" "none or NA" "none or NA" ...
##  $ treatment: chr  "not in treatment" "no psychological disorders" "not in treatment" "not in treatment" ...
##  $ pswq     : num  1.43 1.79 2.36 2.5 1.14 ...
##  $ phq      : num  1.44 1.33 1.44 1 1.33 ...
##  $ gad      : num  1.29 1 1.14 1.14 1.14 ...
##  $ edeq12   : num  1.08 1 1.17 1.42 1 ...
# 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 (2 variables)
table(d2$treatment)
## 
##                    in treatment      no psychological disorders 
##                              45                             219 
##                not in treatment                           other 
##                             262                              11 
##               seeking treatment treatment disrupted by COVID-19 
##                              22                              29
table(d2$mhealth)
## 
##              anxiety disorder                       bipolar 
##                            72                             3 
##                    depression              eating disorders 
##                            12                            19 
##                    none or NA obsessive compulsive disorder 
##                           438                            15 
##                         other                          ptsd 
##                            16                            13
# use histograms to visualize continuous data (4 variables)
hist(d2$pswq)

hist(d2$gad)

hist(d2$phq)

hist(d2$edeq12)

Univariate Normality for Continuous Variables

describe(d2)
##            vars   n    mean      sd  median trimmed     mad min  max range
## X             1 588 5108.78 2593.60 5669.50 5249.01 3052.67  20 8858  8838
## mhealth*      2 588    4.57    1.51    5.00    4.77    0.00   1    8     7
## treatment*    3 588    2.72    1.10    3.00    2.57    1.48   1    6     5
## pswq          4 588    2.68    0.76    2.79    2.70    0.85   1    4     3
## phq           5 588    2.29    0.87    2.22    2.24    0.99   1    4     3
## gad           6 588    2.21    0.94    2.00    2.15    1.06   1    4     3
## edeq12        7 588    1.95    0.78    1.83    1.89    0.99   1    4     3
##             skew kurtosis     se
## X          -0.39    -1.14 106.96
## mhealth*   -1.26     1.73   0.06
## treatment*  1.29     2.15   0.05
## pswq       -0.19    -1.01   0.03
## phq         0.35    -0.94   0.04
## gad         0.40    -1.09   0.04
## edeq12      0.53    -0.87   0.03
## 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.

Write-up of Normality

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, treatment, mhealth)
 mhealth 
 anxiety disorder   bipolar   depression   eating disorders   none or NA   obsessive compulsive disorder   other   ptsd 
 treatment 
   in treatment  12 1 9 6 4 10 3
   no psychological disorders  2 216 1
   not in treatment  40 2 6 6 191 9 2 6
   other  2 1 6 1 1
   seeking treatment  6 3 11 2
   treatment disrupted by COVID-19  10 1 1 4 8 2 2 1
   #Total cases  72 3 12 19 438 15 16 13
## Some students may have issues with this function working. If this happens to you, please try these 2 options:
## Option 1: install the "maditr" package and then call in its library.
## Option 2: If Option 1 doesn't work, then you will use xtabs() instead. Fill in the code below and remove the "#" to run. Then hashtag out the cross_cases() line.

# xtabs(~ + , data=)

# 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 and General Anxiety Disorder",
     xlab = "Patient Health Questionnaire",
     ylab = "General Anxiety Disorder")

plot(d2$pswq, d2$edeq12,
     main="Scatterplot of Penn State Worry Questionnaire and Eating Disorder Questionnaire",
     xlab = "Penn State Worry Questionnaire",
     ylab = "Eating Disorder Questionnaire")

# Note: for HW, you will choose to plot 2 combos of your 4 continuous variables, based on your potential 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 construct names, NOT their R abbrev or full 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 Patient Health Questionnaire by Mental Health Disorders",
        xlab = "Mental Health Disorders",
        ylab = "Patient Health Questionnaire")

boxplot(data=d2, gad~treatment,
        main="General Anxiety Disorder by Mental Health Treatment",
        xlab = "Mental Health Treatment",
        ylab = "General Anxiety Disorder")

# 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 potential hypotheses. You may repeat 1 variable to see its association with others. Again, 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 construct names, NOT their R abbrev or full scales, so someone reading your plots can understand them.

That’s it!!