Basic Statistics

Load Libraries

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

#install.packages("")

library(psych)# for the describe() command

Import Data

# Import the "projectdata.csv" file for this Lab

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

# Note: for your Project version, you will import "projectdata.csv" that you created and exported at the end of the Project Data Prep assignment.

Univariate Plots: Histograms & Tables

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

# use tables to visualize 2 categorical variables
table(d2$education)
## 
##              1 equivalent to not completing high school 
##                                                     276 
##                  2 equivalent to high school completion 
##                                                     259 
## 3 equivalent to vocational/technical program completion 
##                                                       4 
##                        4 equivalent to AP/IB completion 
##                                                      90 
##                                  5 undergraduate degree 
##                                                       1 
##                                       prefer not to say 
##                                                      55
table(d2$mhealth)
## 
##              anxiety disorder                       bipolar 
##                            79                             3 
##                    depression              eating disorders 
##                            11                            19 
##                    none or NA obsessive compulsive disorder 
##                           530                            16 
##                         other                          ptsd 
##                            18                             9
# use histograms to visualize 4 continuous variables
hist(d2$iou)

hist(d2$mfq_26)

hist(d2$pss)

hist(d2$gad)

Univariate Normality for Continuous Variables (individually)

# quick & dirty normality test: check skew & kurtosis values
describe(d2)
##            vars   n    mean      sd  median trimmed     mad  min     max
## X             1 685 5200.06 2585.40 5845.00 5349.83 2959.27 20.0 8860.00
## education*    2 685    2.19    1.48    2.00    1.91    1.48  1.0    6.00
## mhealth*      3 685    4.58    1.43    5.00    4.83    0.00  1.0    8.00
## iou           4 685    2.69    0.92    2.56    2.65    1.04  1.0    4.89
## mfq_26        5 685    4.24    0.65    4.25    4.25    0.67  1.8    6.00
## pss           6 685    3.14    0.94    3.25    3.15    1.11  1.0    5.00
## gad           7 685    2.19    0.92    2.00    2.13    1.06  1.0    4.00
##              range  skew kurtosis    se
## X          8840.00 -0.42    -1.11 98.78
## education*    5.00  1.40     0.97  0.06
## mhealth*      7.00 -1.50     2.25  0.05
## iou           3.89  0.30    -0.82  0.04
## mfq_26        4.20 -0.23     0.09  0.02
## pss           4.00 -0.13    -0.73  0.04
## gad           3.00  0.44    -1.01  0.04
## 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 four continuous variables and all were within the acceptable range (-2/+2).

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

# Objective normality test: Shapiro-Wilks test
# We want the test to be NON-SIG (aka > .05) to indicate normality

options(scipen = 999)
# the above code turns off scientific notation


shapiro.test(d2$iou)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$iou
## W = 0.97144, p-value = 0.000000000267
shapiro.test(d2$mfq_26)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$mfq_26
## W = 0.99553, p-value = 0.04563
shapiro.test(d2$pss)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$pss
## W = 0.98033, p-value = 0.00000005813
shapiro.test(d2$gad)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$gad
## W = 0.92445, p-value < 0.00000000000000022

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

[TYPE Write-up of Normality here]

Bivariate Plots

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$mfq_26, d2$pss,
     main="Mental Flexibility and Percieved Stress",
     xlab = "Mental Flexibility",
     ylab = "Percieved Stress")

plot(d2$iou, d2$gad,
     main="Intolerance of Uncertainty and General Anxiety Disorder",
     xlab = "Intolerance of Uncertainty",
     ylab = "General Anxiety Disorder")

# 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,gad~mhealth,
        main="Boxplot of Mental Health by Anxiety Disorder",
        xlab = "Mental Health",
        ylab = "Anxiety Disorder")

boxplot(data=d2,mfq_26~education,
        main="Mental Flexibility by Perceived Stress",
        xlab = "Education level",
        ylab = "Mental Flexibility")

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

Those are the basics!!