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 get total summary skip 'by' argument: take_all(mtcars, mean)

Import Data

# import our data for the lab
# for the homework, you will import the mydata.csv that we created in 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 participants are in each level
## 
##          1 under 18 2 between 18 and 25 3 between 26 and 35 4 between 36 and 45 
##                 645                  56                   6                  90 
##           5 over 45 
##                 190
table(d2$mhealth)
## 
##              anxiety disorder                       bipolar 
##                           100                             4 
##                    depression              eating disorders 
##                            25                            19 
##                    none or NA obsessive compulsive disorder 
##                           782                            17 
##                         other                          ptsd 
##                            24                            16
hist(d2$covid_pos) #the hist command creates a histogram of the variable

hist(d2$support)

hist(d2$pas_covid)

Univariate Normality

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

We analyzed the skew and kurtosis of our … and most were within the accepted range (-2/+2). However, some variables (list them in parentheses) 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 univariate normality ... skew and kurtosis, (-2/+2)
##           vars   n mean   sd median trimmed  mad min max range  skew kurtosis
## age*         1 987 2.11 1.66   1.00    1.89 0.00   1   5     4  0.96    -0.94
## mhealth*     2 987 4.63 1.38   5.00    4.89 0.00   1   8     7 -1.52     2.77
## covid_pos    3 987 1.84 3.27   0.00    1.08 0.00   0  15    15  1.76     2.13
## support      4 987 3.59 0.95   3.67    3.64 0.99   1   5     4 -0.42    -0.61
## gender*      5 987 1.37 0.79   1.00    1.20 0.00   1   4     3  1.77     1.45
## pas_covid    6 987 3.23 0.68   3.22    3.25 0.66   1   5     4 -0.22     0.11
##             se
## age*      0.05
## mhealth*  0.04
## covid_pos 0.10
## support   0.03
## gender*   0.03
## pas_covid 0.02

Bivariate Plots

Crosstabs

cross_cases(d2,mhealth,covid_pos) #update variable2 and variable3 with your categorical variable names
 covid_pos 
 0   1   2   3   4   5   6   7   8   9   10   11   12   13   14   15 
 mhealth 
   anxiety disorder  59 4 4 5 7 4 3 5 3 3 1 1 1
   bipolar  1 1 1 1
   depression  18 1 1 1 1 1 1 1
   eating disorders  7 1 2 4 3 1 1
   none or NA  564 19 15 27 26 27 20 21 11 17 13 7 11 1 2 1
   obsessive compulsive disorder  4 2 1 2 1 1 2 1 1 1 1
   other  11 1 3 1 3 2 3
   ptsd  9 1 1 1 1 1 2
   #Total cases  673 28 28 35 37 35 28 31 21 26 15 9 13 3 3 2

Scatterplots

plot(d2$pas_covid, d2$support,
     main="Scatterplot of pas_covid and support",
     xlab = "pas_covid",
     ylab = "support")

plot(d2$covid_pos, d2$support,
     main="Scatterplot of covid_pos and support",
     xlab = "covid_pos",
     ylab = "support")

Boxplots

#boxplots use ONE CATEGORICAL and ONE CONTINUOUS variable
#make sure that you enter them in the right order !
# categorical variables goes BEfORE the tilde
# continuous variable goes AFTER the tilde 
boxplot(data=d2, support~mhealth,
        main="Boxplot of mhealth and support",
        xlab = "mhealth",
        ylab = "support")

boxplot(data=d2, pas_covid~gender,
        main="Boxplot of gender and pas_covid",
        xlab = "gender",
        ylab = "pas_covid")