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 drop variable use NULL: let(mtcars, am = NULL) %>% head()

Import & Examine Data

# Import the "projectdata.csv" file

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

str(d2)
## 'data.frame':    675 obs. of  7 variables:
##  $ X      : int  520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
##  $ age    : chr  "1 under 18" "1 under 18" "1 under 18" "1 under 18" ...
##  $ mhealth: chr  "none or NA" "none or NA" "none or NA" "none or NA" ...
##  $ pss    : num  2.75 2.25 3 2 1.75 2 1 1.25 3 1.25 ...
##  $ phq    : num  1.56 1.44 1.11 1.33 1.44 ...
##  $ gad    : num  1.14 1.29 1 1 1.14 ...
##  $ edeq12 : num  1.33 1.08 1 1 1.17 ...
# 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$age)
## 
##          1 under 18 2 between 18 and 25 
##                 621                  54
table(d2$mhealth)
## 
##              anxiety disorder                       bipolar 
##                            75                             3 
##                    depression              eating disorders 
##                            12                            19 
##                    none or NA obsessive compulsive disorder 
##                           519                            15 
##                         other                          ptsd 
##                            18                            14
# use histograms to visualize continuous data (4 variables)
hist(d2$pss)

hist(d2$phq)

hist(d2$gad)

hist(d2$edeq12)

## Univariate Normality for Continuous Variables 
describe(d2)
##          vars   n    mean      sd  median trimmed     mad min  max range  skew
## X           1 675 5160.26 2593.06 5745.00 5304.39 3031.92  20 8860  8840 -0.40
## age*        2 675    1.08    0.27    1.00    1.00    0.00   1    2     1  3.09
## mhealth*    3 675    4.62    1.45    5.00    4.84    0.00   1    8     7 -1.37
## pss         4 675    3.13    0.95    3.25    3.15    1.11   1    5     4 -0.14
## phq         5 675    2.28    0.86    2.22    2.23    0.99   1    4     3  0.36
## gad         6 675    2.20    0.93    2.00    2.14    1.06   1    4     3  0.42
## edeq12      7 675    1.95    0.77    1.83    1.88    0.86   1    4     3  0.54
##          kurtosis    se
## X           -1.13 99.81
## age*         7.56  0.01
## mhealth*     2.28  0.06
## pss         -0.76  0.04
## phq         -0.91  0.03
## gad         -1.06  0.04
## edeq12      -0.83  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,age, mhealth)
 mhealth 
 anxiety disorder   bipolar   depression   eating disorders   none or NA   obsessive compulsive disorder   other   ptsd 
 age 
   1 under 18  65 2 11 16 484 12 17 14
   2 between 18 and 25  10 1 1 3 35 3 1
   #Total cases  75 3 12 19 519 15 18 14
## 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$pss,d2$phq,
     main="Scatterplot of PSS Scores and PHQ Scores",
     xlab = "PHQ",
     ylab = "PSS")

plot(d2$gad, d2$edeq12,
     main="Scatterplot of GAD Scores and EDEQ12 Scores",
     xlab = "GAD",
     ylab = "EDEQ12")

# 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, edeq12~mhealth,
        main="Boxplot of EDEQ12 Scores by Mental Health",
        xlab = "Mental Health",
        ylab = "EDEQ12 Scores")

boxplot(data=d2, gad~age,
        main="Boxplot of GAD Scores by Age",
        xlab = "Age",
        ylab = "GAD Scores")

# 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!!