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
## 
## To aggregate all non-grouping columns: take_all(mtcars, mean, by = am)
## 
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
## 
##     sort_by
## 
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
##  To return to the console output, use 'expss_output_default()'.

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

mydata <- read.csv(file="Data/mydata.csv", header = T)

Univariate Plots: Histograms & Tables

table(mydata$sexual_orientation) #the table command shows us what the levels of this variable are, and how many participants are in each level
## 
##               Asexual                    Bi           Gay/Lesbian 
##                    33                   145                    51 
## Heterosexual/Straight    I use another term     Prefer not to say 
##                   858                    31                    85
table(mydata$mhealth)
## 
##              anxiety disorder                       bipolar 
##                           123                             6 
##                    depression              eating disorders 
##                            32                            28 
##                    none or NA obsessive compulsive disorder 
##                           928                            26 
##                         other                          ptsd 
##                            34                            26
hist(mydata$big5_con) #the hist command creates a histogram of the variable

hist(mydata$pas_covid)

hist(mydata$pss)

hist(mydata$edeq12)

Univariate Normality

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

describe(mydata) #we use this to check univariate normality... skew and kurtosis, (-2/+2)
##                     vars    n mean   sd median trimmed  mad  min max range
## sexual_orientation*    1 1203 3.80 1.02   4.00    3.83 0.00 1.00   6  5.00
## mhealth*               2 1203 4.64 1.42   5.00    4.87 0.00 1.00   8  7.00
## big5_con               3 1203 4.84 1.19   5.00    4.88 1.48 1.00   7  6.00
## pas_covid              4 1203 3.24 0.67   3.22    3.25 0.66 1.22   5  3.78
## pss                    5 1203 2.93 0.95   3.00    2.92 1.11 1.00   5  4.00
## edeq12                 6 1203 1.88 0.73   1.75    1.81 0.74 1.00   4  3.00
##                      skew kurtosis   se
## sexual_orientation* -0.50     1.32 0.03
## mhealth*            -1.36     2.46 0.04
## big5_con            -0.29    -0.27 0.03
## pas_covid           -0.13    -0.01 0.02
## pss                  0.08    -0.75 0.03
## edeq12               0.68    -0.55 0.02

Bivariate Plots

Crosstabs

cross_cases(mydata, sexual_orientation, 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 
 sexual_orientation 
   Asexual  4 1 24 1 2 1
   Bi  25 2 5 8 81 5 13 6
   Gay/Lesbian  13 1 1 29 3 2 2
   Heterosexual/Straight  71 2 24 16 704 15 11 15
   I use another term  2 1 1 25 1 1
   Prefer not to say  8 2 2 65 1 6 1
   #Total cases  123 6 32 28 928 26 34 26

Scatterplots

plot(mydata$pas_covid, mydata$pss,
     main="Scatterplot of pas_covid and pss",
     xlab = "pas_covid",
     ylab = "pss")

plot(mydata$big5_con, mydata$edeq12,
     main="Scatterplot of big5_con and edeq12",
     xlab = "big5_con",
     ylab = "edeq12")

Boxplots

# boxplots use one categorical and one continuous variable
# make sure that you enter them in the right order!!
# categorical variable goes before the tilde ~
# continuous variable goes after the tilde ~

boxplot(data=mydata, pss~sexual_orientation,
        main="Boxplot of sexual_orientation and pss",
        xlab = "sexual_orientation",
        ylab = "pss")

boxplot(data=mydata, edeq12~mhealth,
        main="Boxplot of mhealth and edeq12",
        xlab = "mhealth",
        ylab = "edeq12")