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 drop variable use NULL: let(mtcars, am = NULL) %>% head()
## 
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
## 
##     sort_by
## 
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
##  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

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

Univariate Plots: Histograms & Tables

table(d2$gender) #the table command shows us what the levels of this variable are, and how many participants are in each level
## 
##    f    m   nb 
## 1546  530   31
table(d2$age)
## 
## 1 between 18 and 25 2 between 26 and 35 3 between 36 and 45           4 over 45 
##                1940                 113                  37                  17
hist(d2$moa_independence) #the hist command creates a histogram of the variable 

hist(d2$swb)

hist(d2$support)

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 … and most were within the accepted range (-2/+2). However, one variable (moa_independence) was outside of the accepted range. For this analysis, we will use it anyway, but outside of this class this is bad practice.

describe(d2) #we use this to univariate normality ... skew and kurtosis, (-2/+2)
##                  vars    n mean   sd median trimmed  mad min max range  skew
## gender*             1 2107 1.28 0.48   1.00    1.21 0.00 1.0 3.0   2.0  1.36
## age*                2 2107 1.11 0.43   1.00    1.00 0.00 1.0 4.0   3.0  4.42
## moa_independence    3 2107 3.54 0.47   3.67    3.61 0.49 1.0 4.0   3.0 -1.49
## swb                 4 2107 4.43 1.33   4.50    4.49 1.48 1.0 7.0   6.0 -0.36
## support             5 2107 5.53 1.13   5.75    5.66 0.99 0.0 7.0   7.0 -1.10
## stress              6 2107 3.07 0.60   3.10    3.07 0.59 1.3 4.6   3.3 -0.01
##                  kurtosis   se
## gender*              0.74 0.01
## age*                21.17 0.01
## moa_independence     2.75 0.01
## swb                 -0.49 0.03
## support              1.35 0.02
## stress              -0.16 0.01

Bivariate Plots

Crosstabs

cross_cases(d2, gender, age) # update variable2 and variable3 with your categorical variable names
 age 
 1 between 18 and 25   2 between 26 and 35   3 between 36 and 45   4 over 45 
 gender 
   f  1439 68 27 12
   m  471 45 9 5
   nb  30 1
   #Total cases  1940 113 37 17

Scatterplots

plot(d2$moa_independence, d2$swb,
     main="Scatterplot of MOA_Independence and SWB",
     xlab = "MOA_Independence",
     ylab = "SWB")

plot(d2$support, d2$stress,
     main="Scatterplot of Support and Stress",
     xlab = "Support",
     ylab = "Stress")

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=d2, swb~age,
        main="Boxplot of Age and Satisfaction with Life Scale (SWB)",
        xlab = "Age",
        ylab = "SWB")

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