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 modify variables or add new variables:
##              let(mtcars, new_var = 42, new_var2 = new_var*hp) %>% head()
## 
## 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)
## 
##      1 High school diploma or less, and NO COLLEGE 
##                                                 53 
##                             2 Currently in college 
##                                               2460 
## 3 Completed some college, but no longer in college 
##                                                 35 
##                   4 Complete 2 year College degree 
##                                                174 
##                       5 Completed Bachelors Degree 
##                                                135 
##                  6 Currently in graduate education 
##                                                132 
##                   7 Completed some graduate degree 
##                                                 56
table(d2$marriage5)
## 
##             are currently divorced from one another 
##                                                 704 
##                are currently married to one another 
##                                                2061 
##       never married each other and are not together 
##                                                 236 
## never married each other but are currently together 
##                                                  44
hist(d2$moa_independence)

hist(d2$moa_role)

hist(d2$mindful)

hist(d2$efficacy)

Univariate Normality

We analyzed the skew and kurtosis of our continuous variables and half were within the accepted range (-2/+2). However, two variables (edu and moa_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 (how centered) and kurtosis (height), (-2/+2)
##                  vars    n mean   sd median trimmed  mad  min max range  skew
## edu*                1 3045 2.51 1.25   2.00    2.18 0.00 1.00   7  6.00  2.18
## marriage5*          2 3045 1.88 0.59   2.00    1.84 0.00 1.00   4  3.00  0.46
## moa_independence    3 3045 3.54 0.46   3.67    3.61 0.49 1.00   4  3.00 -1.43
## moa_role            4 3045 2.97 0.72   3.00    3.00 0.74 1.00   4  3.00 -0.33
## mindful             5 3045 3.71 0.84   3.73    3.71 0.79 1.13   6  4.87 -0.07
## efficacy            6 3045 3.12 0.45   3.10    3.13 0.44 1.10   4  2.90 -0.25
##                  kurtosis   se
## edu*                 3.67 0.02
## marriage5*           1.50 0.01
## moa_independence     2.47 0.01
## moa_role            -0.84 0.01
## mindful             -0.14 0.02
## efficacy             0.48 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  574 1686 169 31
   3 Completed some college, but no longer in college  8 23 4
   4 Complete 2 year College degree  33 101 31 9
   5 Completed Bachelors Degree  33 89 13
   6 Currently in graduate education  29 91 10 2
   7 Completed some graduate degree  13 39 3 1
   #Total cases  704 2061 236 44

Scatterplots

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

plot(d2$mindful, d2$efficacy,
     main="Scatterplot of mindful and efficacy",
     xlab = "mindful",
     ylab = "efficacy")

Boxplots

# boxplots use one categorical and one continuous variable
# make sure you enter them in the right order
# continuous ~ categorical

boxplot(data=d2, mindful~edu,
        main="Boxplot of edu and mindful",
        xlab = "edu",
        ylab = "mindful")

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