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$mhealth)
## 
##              anxiety disorder                       bipolar 
##                            41                             3 
##                    depression              eating disorders 
##                             4                            14 
##                    none or NA obsessive compulsive disorder 
##                           161                            11 
##                         other                          ptsd 
##                            12                             9
table(d2$treatment)
## 
##                    in treatment      no psychological disorders 
##                              31                              80 
##                not in treatment                           other 
##                             106                               5 
##               seeking treatment treatment disrupted by COVID-19 
##                              12                              21
# use histograms to visualize 4 continuous variables
hist(d2$brs)

hist(d2$edeq12)

hist(d2$rse)

hist(d2$mfq_26)

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 255 7528.90 733.06 7481.00 7518.04 916.25 6294.00 8858.00
## mhealth*      2 255    4.48   1.76    5.00    4.57   0.00    1.00    8.00
## treatment*    3 255    2.80   1.31    3.00    2.65   1.48    1.00    6.00
## brs           4 255    2.55   0.85    2.33    2.52   0.74    1.00    5.00
## edeq12        5 255    2.16   0.79    2.17    2.14   0.99    1.00    4.00
## rse           6 255    2.14   0.65    2.00    2.11   0.59    1.00    4.00
## mfq_26        7 255    4.05   0.66    4.05    4.04   0.67    2.15    5.75
##             range  skew kurtosis    se
## X          2564.0  0.13    -1.17 45.91
## mhealth*      7.0 -0.82     0.33  0.11
## treatment*    5.0  1.04     0.78  0.08
## brs           4.0  0.28    -0.39  0.05
## edeq12        3.0  0.21    -1.07  0.05
## rse           3.0  0.53    -0.25  0.04
## mfq_26        3.6  0.09    -0.22  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$brs)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$brs
## W = 0.98035, p-value = 0.001347
shapiro.test(d2$edeq12)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$edeq12
## W = 0.95456, p-value = 3.659e-07
shapiro.test(d2$rse)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$rse
## W = 0.96891, p-value = 2.365e-05
shapiro.test(d2$mfq_26)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$mfq_26
## W = 0.99455, p-value = 0.4949

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$edeq12, d2$rse,
     main="Scatterplot of Eating Disorder Questionnaire-12 and Rosenberg Self-Esteem Inventory",
     xlab = "Eating Disorder Questionnaire-12",
     ylab = "Rosenberg Self-Esteem Inventory")

plot(d2$mfq_26, d2$brs,
     main="Scatterplot of Mental Flexibility Questionnaire and Brief Resilience Scale",
     xlab = "Mental Flexibility Questionnaire",
     ylab = "Brief Resilience Scale")

# 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, edeq12~mhealth,
        main="Boxplot of Eating Disorder Questionnaire-12 and Mental health disorders",
        xlab = "Mental Health Disorders",
        ylab = "Eating Disorder Questionnaire-12")

boxplot(data=d2, rse~treatment,
        main="Boxplot of Rosenberg Self-Esteem Inventory and Mental Health Treatment ",
        xlab = "Mental Health Treatment",
        ylab = "Rosenberg Self-Esteem Inventory")

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