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("psych")

library(psych) # for the describe() command
## Warning: package 'psych' was built under R version 4.5.3

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$gender)
## 
##             female I use another term               male  Prefer not to say 
##                228                 17                 41                  6
table(d2$sleep_hours)
## 
##  1 < 5 hours  2 5-6 hours  3 7-8 hours 4 8-10 hours 5 > 10 hours 
##           36          104           97           40           15
# use histograms to visualize 4 continuous variables
hist(d2$mfq_state)

hist(d2$phq)

hist(d2$gad)

hist(d2$brs)

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 292 7558.67 737.94 7529.50 7555.59 922.92 6291.00 8860
## gender*         2 292    1.40   0.80    1.00    1.23   0.00    1.00    4
## sleep_hours*    3 292    2.64   1.03    3.00    2.61   1.48    1.00    5
## mfq_state       4 292    3.70   0.99    3.75    3.73   0.93    1.12    6
## phq             5 292    2.62   0.84    2.67    2.62   0.99    1.00    4
## gad             6 292    2.59   0.91    2.71    2.60   1.06    1.00    4
## brs             7 292    2.65   0.85    2.58    2.65   0.86    1.00    5
##                range  skew kurtosis    se
## X            2569.00  0.04    -1.19 43.18
## gender*         3.00  1.73     1.51  0.05
## sleep_hours*    4.00  0.37    -0.33  0.06
## mfq_state       4.88 -0.23    -0.35  0.06
## phq             3.00 -0.02    -1.00  0.05
## gad             3.00 -0.10    -1.14  0.05
## brs             4.00  0.16    -0.58  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 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$mfq_state)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$mfq_state
## W = 0.98921, p-value = 0.02898
shapiro.test(d2$phq)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$phq
## W = 0.96518, p-value = 0.00000173
shapiro.test(d2$gad)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$gad
## W = 0.95028, p-value = 0.00000002164
shapiro.test(d2$brs)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$brs
## W = 0.98184, p-value = 0.0009191

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$mfq_state, d2$gad,
     main="Scatterplot of Mental Flexibility and Anxiety ",
     xlab = "Mental Flexibility",
     ylab = "Anxiety")

plot(d2$phq, d2$brs, 
     main="Scatterplot of Symptoms of Depression and Resilience ",
     xlab = "Symptoms of Depression",
     ylab = "Resilience")

# 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~sleep_hours,
        main="Boxplot of Symptoms of Depression by Hours of Sleep",
        xlab = "Hours of Sleep",
        ylab = "Symptoms of Depression")

boxplot(data= d2, brs~sleep_hours,
        main="Boxplot of Resilience by Hours of Sleep ",
        xlab = "Hours of Sleep",
        ylab = "Resilience")

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