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

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$edu) #the table command shows us what the levels of this variable are, and how many participants are in each level, will replace with names of variables
## 
##      1 High school diploma or less, and NO COLLEGE 
##                                                 53 
##                             2 Currently in college 
##                                               2482 
## 3 Completed some college, but no longer in college 
##                                                 34 
##                   4 Complete 2 year College degree 
##                                                175 
##                       5 Completed Bachelors Degree 
##                                                136 
##                  6 Currently in graduate education 
##                                                133 
##                   7 Completed some graduate degree 
##                                                 56
table(d2$marriage5)
## 
##             are currently divorced from one another 
##                                                 715 
##                are currently married to one another 
##                                                2071 
##       never married each other and are not together 
##                                                 238 
## never married each other but are currently together 
##                                                  45
hist(d2$moa_independence) #the hist command creates a histogram of the variable

hist(d2$moa_maturity)

hist(d2$mindful)

hist(d2$stress)

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 categorical variables and most were within the accepted range (-2/+2). However, some variables (education and independence) 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 (-2/+2)
##                  vars    n mean   sd median trimmed  mad  min max range  skew
## edu*                1 3069 2.51 1.24   2.00    2.18 0.00 1.00 7.0  6.00  2.19
## marriage5*          2 3069 1.87 0.59   2.00    1.83 0.00 1.00 4.0  3.00  0.46
## moa_independence    3 3069 3.54 0.46   3.67    3.61 0.49 1.00 4.0  3.00 -1.44
## moa_maturity        4 3069 3.59 0.43   3.67    3.65 0.49 1.00 4.0  3.00 -1.20
## mindful             5 3069 3.71 0.84   3.73    3.71 0.79 1.13 6.0  4.87 -0.06
## stress              6 3069 3.05 0.60   3.00    3.05 0.59 1.30 4.7  3.40  0.04
##                  kurtosis   se
## edu*                 3.68 0.02
## marriage5*           1.49 0.01
## moa_independence     2.52 0.01
## moa_maturity         1.87 0.01
## mindful             -0.13 0.02
## stress              -0.16 0.01

Bivariate Plots

Crosstabs

cross_cases(d2, edu, marriage5) 
 marriage5 
 are currently divorced from one another   are currently married to one another   never married each other and are not together   never married each other but are currently together 
 edu 
   1 High school diploma or less, and NO COLLEGE  14 32 6 1
   2 Currently in college  586 1696 169 31
   3 Completed some college, but no longer in college  7 23 4
   4 Complete 2 year College degree  34 100 32 9
   5 Completed Bachelors Degree  33 89 14
   6 Currently in graduate education  29 91 10 3
   7 Completed some graduate degree  12 40 3 1
   #Total cases  715 2071 238 45

Scatterplots

plot(d2$mindful, d2$moa_maturity,
     main="Scatterplot of mindfulness and maturity",
     xlab = "mindful",
     ylab = "moa_maturity")

plot(d2$stress, d2$moa_independence,
     main="Scatterplot of stress and independence",
     xlab = "stress",
     ylab = "moa_independence")

Boxplots

# boxplots use ONE CATEGORICAL and ONE CONTINUOUS variable
# make sure you enter them in right order!
# categorical goes AFTER ~ continuous before
boxplot(data=d2, moa_maturity~edu,
        main="Boxplot of education and maturity",
        xlab = "edu",
        ylab = "moa_maturity")

boxplot(data=d2, stress~marriage5,
        main="Boxplot of marriage and stress",
        xlab = "marriage5",
        ylab = "stress")