Basic Statistics

Load Libraries

# if you haven't run this code before, you'll need to download the below packages first
# instructions on how to do this are included in the video
# but as a reminder, you use the packages tab to the right

library(psych) # for the describe() command
library(expss) # for the cross_cases() command
## Loading required package: maditr
## 
## Use magrittr pipe '%>%' to chain several operations:
##              mtcars %>%
##                  let(mpg_hp = mpg/hp) %>%
##                  take(mean(mpg_hp), by = am)
## 
## 
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
## 
##     sort_by

Import Data

# import our data for the lab
# for the homework, you will import the mydata.csv that we created in the Data Prep Lab
 
d2 <- read.csv(file="Data/mydata.csv", header = T)

Univariate Plots: Histograms & Tables

table(d2$age) #the table command shows us what the levels of this variable are, and how many particpants are in each lvel
## 
##          1 under 18 2 between 18 and 25 3 between 26 and 35 4 between 36 and 45 
##                 629                  54                   5                  86 
##           5 over 45 
##                 180
table(d2$mhealth)
## 
##              anxiety disorder                       bipolar 
##                            97                             4 
##                    depression              eating disorders 
##                            23                            19 
##                    none or NA obsessive compulsive disorder 
##                           755                            18 
##                         other                          ptsd 
##                            23                            15
hist(d2$rse) #the hist command creates a histogram of the variable

hist(d2$gad)

hist(d2$pswq)

hist(d2$pas_covid)

Univariate Normality

We analyzed the skew and kurtosis of our continuous variables… and most were within the accepted range (-2/+2). However, some variables (rse and pswq) were outside of the accepted range. For this analysis, we will use them anyway, but outside of this class this is bad practice.

describe(d2) #we use this to check for normality ... slow and kurtosis, (-2/+2) 
##           vars   n mean   sd median trimmed  mad min  max range  skew kurtosis
## age*         1 954 2.09 1.65   1.00    1.87 0.00   1 5.00  4.00  0.98    -0.88
## mhealth*     2 954 4.63 1.38   5.00    4.89 0.00   1 8.00  7.00 -1.53     2.77
## rse          3 954 2.67 0.71   2.70    2.69 0.74   1 4.00  3.00 -0.21    -0.73
## gad          4 954 1.99 0.89   1.71    1.89 0.85   1 4.00  3.00  0.76    -0.57
## pswq         5 954 2.71 0.80   2.71    2.71 0.95   1 4.75  3.75  0.05    -0.76
## pas_covid    6 954 3.23 0.68   3.22    3.25 0.66   1 5.00  4.00 -0.23     0.12
##             se
## age*      0.05
## mhealth*  0.04
## rse       0.02
## gad       0.03
## pswq      0.03
## pas_covid 0.02

Bivariate Plots

Crosstabs

cross_cases(d2, age, mhealth) # update variable2 and variable3 with your categorical variable names
 mhealth 
 anxiety disorder   bipolar   depression   eating disorders   none or NA   obsessive compulsive disorder   other   ptsd 
 age 
   1 under 18  67 2 10 16 494 13 16 11
   2 between 18 and 25  11 1 3 35 3 1
   3 between 26 and 35  1 4
   4 between 36 and 45  10 1 4 66 2 3
   5 over 45  8 9 156 3 4
   #Total cases  97 4 23 19 755 18 23 15

Scatterplots

plot(d2$pswq, d2$gad,
     main="Scatterplot of pswq and gad",
     xlab = "pswq",
     ylab = "gad")

plot(d2$gad, d2$rse,
     main="Scatterplot of gad and rse",
     xlab = "gad",
     ylab = "rse")

Boxplots

# boxplots use ONE CATEGORICAL AND ONE CONTINUOUS variable 
# make sure that you enter them in the right order!!
# categorical goes BEFORE the tilde ~
# continuous variablw goes AFTER the tilde !

boxplot(data=d2, rse~age,
        main="Boxplot of Age and Self-Esteem",
        xlab = "Age",
        ylab = "Self-Esteem")

boxplot(data=d2, pswq~mhealth,
        main="Boxplot of Mental Health and Worry",
        xlab = "Mental Health",
        ylab = "Worry")