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 several columns with one summary: take(mtcars, mpg, hp, fun = 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$mhealth) #the table command shows us what the levels of this variable are, and how many participants are in each level
## 
##              anxiety disorder                       bipolar 
##                           121                             5 
##                    depression              eating disorders 
##                            29                            29 
##                    none or NA obsessive compulsive disorder 
##                           768                            25 
##                         other                          ptsd 
##                            33                            20
table(mydata$treatment)
## 
##                    in treatment      no psychological disorders 
##                              75                             385 
##                not in treatment                           other 
##                             474                              12 
##               seeking treatment treatment disrupted by COVID-19 
##                              34                              50
hist(mydata$iou) #the hist command creates a histogram of the variable

hist(mydata$mfq_26)

hist(mydata$mfq_state)

hist(mydata$edeq12)

Univariate Normality

We analyzed the skew and kurtosis of our continuous variables and all were within the accepted range (-2/+2).true for lab, may not be true for homework!!!

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(mydata) # we use this to check univariate normality ... skew and kurtosis, (-2,+2) 
##            vars    n mean   sd median trimmed  mad min max range  skew kurtosis
## iou           1 1030 2.60 0.91   2.44    2.54 0.99 1.0   5   4.0  0.45    -0.66
## mfq_26        2 1030 4.30 0.67   4.35    4.31 0.67 1.8   6   4.2 -0.29     0.10
## mfq_state     3 1030 4.07 1.01   4.12    4.12 0.93 1.0   6   5.0 -0.51     0.08
## edeq12        4 1030 1.90 0.74   1.75    1.83 0.74 1.0   4   3.0  0.66    -0.58
## mhealth*      5 1030 4.58 1.49   5.00    4.79 0.00 1.0   8   7.0 -1.28     1.80
## treatment*    6 1030 2.70 1.08   3.00    2.57 1.48 1.0   6   5.0  1.35     2.50
##              se
## iou        0.03
## mfq_26     0.02
## mfq_state  0.03
## edeq12     0.02
## mhealth*   0.05
## treatment* 0.03

Bivariate Plots

Crosstabs

cross_cases(mydata, mhealth, treatment) #update variable 2 and variable 3 with your categorial variable names
 treatment 
 in treatment   no psychological disorders   not in treatment   other   seeking treatment   treatment disrupted by COVID-19 
 mhealth 
   anxiety disorder  22 2 68 2 9 18
   bipolar  1 2 2
   depression  5 1 16 1 5 1
   eating disorders  11 9 9
   none or NA  10 379 346 7 16 10
   obsessive compulsive disorder  5 1 16 3
   other  17 2 7 1 2 4
   ptsd  4 10 1 2 3
   #Total cases  75 385 474 12 34 50

Scatterplots

plot(mydata$iou, mydata$mfq_26,
     main="Scatterplot of iou and mfq_26",
     xlab = "iou",
     ylab = "mfq_26")

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

Boxplots

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

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