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 "fakedata.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$age)
## 
##          1 under 18 2 between 18 and 25 
##                 625                  49
table(d2$mhealth)
## 
##              anxiety disorder                       bipolar 
##                            76                             3 
##                    depression              eating disorders 
##                            10                            17 
##                    none or NA obsessive compulsive disorder 
##                           523                            15 
##                         other                          ptsd 
##                            18                            12
# use histograms to visualize 4 continuous variables
hist(d2$pas_covid)

hist(d2$isolation_c)

hist(d2$mfq_26)

hist(d2$pss)

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  range
## X              1 674 5157.18 2600.55 5746.00 5299.97 3071.21 20.0 8860.0 8840.0
## age*           2 674    1.07    0.26    1.00    1.00    0.00  1.0    2.0    1.0
## mhealth*       3 674    4.61    1.44    5.00    4.85    0.00  1.0    8.0    7.0
## pss            4 674    3.13    0.94    3.25    3.15    1.11  1.0    5.0    4.0
## pas_covid      5 674    3.24    0.68    3.33    3.26    0.66  1.0    5.0    4.0
## isolation_c    6 674    2.29    0.81    2.25    2.30    1.11  1.0    3.5    2.5
## mfq_26         7 674    4.23    0.66    4.25    4.24    0.67  1.8    6.0    4.2
##              skew kurtosis     se
## X           -0.40    -1.14 100.17
## age*         3.28     8.80   0.01
## mhealth*    -1.44     2.32   0.06
## pss         -0.13    -0.73   0.04
## pas_covid   -0.29     0.16   0.03
## isolation_c -0.02    -1.24   0.03
## mfq_26      -0.23     0.08   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 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$pss)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$pss
## W = 0.98038, p-value = 0.00000007451
shapiro.test(d2$pas_covid)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$pas_covid
## W = 0.991, p-value = 0.0003978
shapiro.test(d2$isolation_c)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$isolation_c
## W = 0.93164, p-value < 0.00000000000000022
shapiro.test(d2$mfq_26)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$mfq_26
## W = 0.99567, p-value = 0.0579

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

Bivariate Plots

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$pss, d2$pas_covid, 
     main="Scatterplot of Percieved Stress and Pandemic Anxiety",
     xlab = "Percieved Stress",
     ylab = "Pandemic Anxiety")

plot(d2$isolation_c, d2$mfq_26, 
     main="Scatterplot of Loneliness and Mental Flexability",
     xlab = "Loneliness",
     ylab = "Mental Flexability")

# 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, pss~age,
        main="Boxplot of Percieved Stress by Age",
        xlab = "Percieved Stress",
        ylab = "Age")

boxplot(data=d2, pas_covid~mhealth,
        main="Boxplot of Pandemic Anxiety by Mental Health Disorders ",
        xlab = "Pandemic Anxiety",
        ylab = "Mental Health Disorders")

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